我的Bokeh版本是0.12.13和Python 3.6.0我修改了这里可用的示例代码:https://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html我刚刚尝试添加一个额外的y范围。
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)
虽然原始代码运行良好,但我修改后的代码没有。我无法弄清楚我在做什么错误。我对散景有点新意,所以请指导我正确的方向。编辑:添加第三个y轴时没有输出。但它只能在左侧使用2个轴。
问题是你没有添加另一个y范围 - 通过将新词典重新分配给p.extra_y_ranges
,你完全取代旧词典。当您添加的轴期望"foo1"
范围存在时,这会导致问题,但是您已经将它吹走了。以下代码按预期工作:
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)