首页 > 解决方案 > Is there a way to group graph properties such as xlabels for all subplots in Bokeh?

问题描述

I am trying to plot some subplots in the column format of the Bokeh package. The properties of the graphs are equal (xlabel, ylabel, legend location etc.). Now I have to set them manually like this:

g1 = figure(plot_width=700, plot_height=450, title = 'Disp in x-direction', tools = '')
g2 = figure(plot_width=700, plot_height=450, title = 'Disp in y-direction', tools = '')

g1.legend.location = 'top_left'
g1.title.align = 'center'
g1.xaxis.axis_label = "t [s]"
g1.yaxis.axis_label = "Relative displacement [m]"
g1.toolbar.logo = None
g1.legend.click_policy="hide"
g1.legend.label_text_font_size = "8pt"

g2.legend.location = 'top_left'
g2.title.align = 'center'
g2.xaxis.axis_label = "t [s]"
g2.yaxis.axis_label = "Relative displacement [m]"
g2.toolbar.logo = None
g2.legend.click_policy="hide"
g2.legend.label_text_font_size = "8pt"

show(column(g1,g2))

The data is irrelevant. Is there a way to group all graphs (g1, g2.. gn) so that it goes automatically independent on the number of graphs?

标签: pythonbokeh

解决方案


You can just iterate over the figures:

for p in [g1, g2]:
    p.legend.location = 'top_left'
    p.title.align = 'center'
    ...

Alternatively, you should be able to use themes: https://docs.bokeh.org/en/latest/docs/reference/themes.html#bokeh.themes.Theme


推荐阅读