0

I would like to add multiple y axes to a bokeh plot (similar to the one achieved using matplotlib in the attached image).

Would this also be possible using bokeh? The resources I found demonstrate a second y axis.

Thanks in advance!

Best Regards, Pranit Iyengar

mosc9575
  • 5,618
  • 2
  • 9
  • 32
Pranit
  • 5
  • 3

1 Answers1

0

Yes, this is possible. To add a new axis to the figure p use p.extra_y_ranges["my_new_axis_name"] = Range1d(...). Do not write p.extra_y_ranges = {"my_new_axis_name": Range1d(...)} if you want to add multiple axis, because this will overwrite and not extend the dictionary. Other range objects are also valid, too.

Minimal example

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import LinearAxis, Range1d
output_notebook()

data_x = [1,2,3,4,5]
data_y = [1,2,3,4,5]
color = ['red', 'green', 'magenta', 'black']
p = figure(plot_width=500, plot_height=300)
p.line(data_x, data_y, color='blue')

for i, c in enumerate(color, start=1):
    name = f'extra_range_{i}'
    lable = f'extra range {i}'
    p.extra_y_ranges[name] = Range1d(start=0, end=10*i)

    p.add_layout(LinearAxis(axis_label=lable, y_range_name=name), 'left')
    p.line(data_x, data_y, color=c, y_range_name=name)
show(p)

Output

multiple axis example

Official example

See also the twin axis example (axis) on the official webpage. This example uses the same syntax with only two axis. Another example is the twin axis example for models.

mosc9575
  • 5,618
  • 2
  • 9
  • 32