Bokeh and gridplot

I am trying the following code to produce 4 plots in two columns using the gridplot function.

from bokeh.plotting import figure
from import output_file, save, show
from bokeh.models import ColumnDataSource, Legend
from bokeh.layouts import gridplot, layout

outputfilename = r'c:\testing\test.html'

n_plots = 4
x = [i for i in range(10)]
y = [i for i in range(10)]
df = {'x': x, 'y': y}
list_plots = []
source = ColumnDataSource(df)
for i in range(n_plots):
    p = figure(tools=TOOLS)
    ts = p.line('x', 'y', source=source)
    legend = Legend(items=[('test', [ts])], location="center")
    legend.click_policy = 'hide'
    p.add_layout(legend, 'right')

new_layout = []
for i in range(int(n_plots/2)):
    new_layout.append([list_plots[i * 2 + 1], list_plots[i * 2]])

grid = gridplot(new_layout)

I have two issues with the code above:

  1. the toolbar does not show the xpan and the ypan
  2. there is only one toolbar. How can I show an individual toolbar for each plot using gridplot?

Thanks in advance

I did some research and by adding merge_tools=False when I define the variable grid solves both of the issues I have

1 Like

@giovanni Sometimes I find helpful combining row and column to build my grid as it is more intuitive for me. FWIW, I use my layout for a 2x2 grid as:

html_layout= column(row(figure_1_1, figure_1_2), row(figure_2_1,  figure_2_2))

Your mileage might vary.

Thanks for that, I also tried the option with layout, but in my case I am forced to use griplot as I would like to leave some empty spaces and apparently this is possible only with griplot. If I try:

column(row(None, plot1), row(plot2, None)))

this throws the error:

ValueError: Only LayoutDOM items can be inserted into a row. Tried to insert: None of type <class 'NoneType'>

This is because None is not a LayoutDOM item. But if I do the same with griplot everything works OK.

1 Like