Why isnt Bokeh generating extra y ranges here?

你。 提交于 2019-12-20 05:28:06

问题


My Bokeh version is 0.12.13 and Python 3.6.0 I modified the example code available here:

https://docs.bokeh.org/en/latest/docs/user_guide/plotting.html

I have just tried to add one extra y range.

from bokeh.plotting import output_file, figure, show
from bokeh.models import LinearAxis, Range1d

x = [1,2,3,4,5]
y = [1,2,3,4,5]
y2 = [10,9,8,7,6]
y3 = [23,24,25,26,27]

output_file("twin_axis.html")

p = figure(x_range=(0,6), y_range=(0,6))

p.circle(x, y, color="red")

p.extra_y_ranges = {"foo1": Range1d(start=0, end=11)}
p.circle(x, y2, color="blue", y_range_name="foo1")
p.add_layout(LinearAxis(y_range_name="foo1"), 'left')

p.extra_y_ranges = {"foo2": Range1d(start=21, end=31)}
p.circle(x, y3, color="green", y_range_name="foo2")
p.add_layout(LinearAxis(y_range_name="foo2"), 'right')

p.toolbar_location ="above"
show(p)

While the original code works well, my modified code doesnt. I am not able to figure out what mistake I am doing.I am a bit new to bokeh so please guide me in the right direction. EDIT: There is no output when I add the third y axis. But it works with only 2 axes on the left side.


回答1:


The problem is that you are not adding another y-range — by reassigning a new dictionary to p.extra_y_ranges, you are replacing the old one, entirely. This causes problems when the axis you add expects the "foo1" range to exist, but you've blown it away. The following code works as expected:

from bokeh.plotting import output_file, figure, show
from bokeh.models import LinearAxis, Range1d

x = [1,2,3,4,5]
y = [1,2,3,4,5]
y2 = [10,9,8,7,6]
y3 = [23,24,25,26,27]

output_file("twin_axis.html")

p = figure(x_range=(0,6), y_range=(0,6))

p.circle(x, y, color="red")

p.extra_y_ranges = {"foo1": Range1d(start=0, end=11)}
p.circle(x, y2, color="blue", y_range_name="foo1")
p.add_layout(LinearAxis(y_range_name="foo1"), 'left')

# CHANGES HERE: add to dict, don't replace entire dict
p.extra_y_ranges["foo2"] = Range1d(start=21, end=31)

p.circle(x, y3, color="green", y_range_name="foo2")
p.add_layout(LinearAxis(y_range_name="foo2"), 'right')

p.toolbar_location ="above"
show(p)



来源:https://stackoverflow.com/questions/47891531/why-isnt-bokeh-generating-extra-y-ranges-here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!