使用制表符和多个 y 轴时在 Bokeh 中对齐子图轴

Align subplot axes in Bokeh when using Tabs and multiple y axes

我正在尝试在具有多个子图的 Bokeh 中创建一个图形。一些子图利用了标签功能。其他子图只是普通图形,但有一个额外的 y 轴。我无法让 Figure 对象和 Tab 对象的右轴在视觉上对齐。第二个 y 轴实质上使子图变窄,其他子图也相应缩小,但带有制表符的子图不会。

目前,我正在使用 gridplot 将所有内容放在一起。这个问题一定与 Tab 和 Figure 对象的混合有关,但我不知道如何正确地使这个布局工作。我可以通过添加“sizing_mode='stretch_width”作为网格图参数来捏造它,但这并不理想。

这是一个示例代码和输出的屏幕截图:

from bokeh.models import ColumnDataSource, LabelSet, HoverTool, Span, Range1d, LinearAxis
from bokeh.models import Panel, Tabs
from bokeh.plotting import figure, show, output_file, gridplot
from bokeh.io import curdoc
from bokeh.layouts import row, column

import numpy as np

x = list(range(0,10))
y = list(range(0,10))
plot_height=200

fig1 = figure(plot_width=1250, plot_height=plot_height, title='Figure 1')
fig1.step(x,y, mode='after')
fig1.extra_y_ranges = {"y*10": Range1d(start=0, end=100)}
fig1.add_layout(LinearAxis(y_range_name="y*10", axis_label='y*10'), 'right')
fig1.step(x, np.array(y)*10, color='black', legend_label='y*10', y_range_name='y*10')

fig2a = figure(plot_width=1250, plot_height=plot_height, title='Figure 2')
fig2a.step(x,y)

fig2b = figure(plot_width=1250, plot_height=plot_height, title='Figure 2')
fig2b.step(x,y, line_width=4)

tabs = [Panel(child=fig2a, title='Normal'), Panel(child=fig2b, title='Bold')]
fig2 = Tabs(tabs=tabs)

fig3 = figure(plot_width=1250, plot_height=plot_height, title='Figure 3')
fig3.step(x,y)

fig4 = figure(plot_width=1250, plot_height=plot_height, title='Figure 4')
fig4.step(x,y)

o = gridplot([[fig1], [fig2], [fig3], [fig4]])

show(o)

enter image description here

注意:FWIW 将绘图以外的任何东西称为“无花果”(图的缩写)是很不寻常的。

我可以声明,Bokeh 中没有任何东西可以自动对齐嵌套深度超过一层的事物的轴。事实上,我不认为在网格图中放置选项卡是一个曾经被明确考虑过的用例。我认为您最好的选择是手动向选项卡中的图提供 min_border_right 值,以强制右侧占用更多空 space.

fig2a = figure(..., min_border_right=80)
fig2b = figure(..., min_border_right=80)

上面的值“非常好”,但您只需试验一下,看看什么最适合您。

请注意,顶部工具栏将永远不会在此配置中对齐,因为您添加了一个轴。它必须向右移动以适应轴。您可以考虑将工具栏移动到绘图的顶部。