revoltman
revoltman

Reputation: 131

Why isnt Bokeh generating extra y ranges here?

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.

Upvotes: 2

Views: 2998

Answers (1)

bigreddot
bigreddot

Reputation: 34628

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)

enter image description here

Upvotes: 7

Related Questions