如何在更改轴大小时修复散景 vbar 图表的大小?

how to fix Bokeh vbar charts' size while changing axis size?

我在散景中使用 "column" 布局将 2 个 vbar 图表相互堆叠。这 2 个 vbar 图表共享相同的 x 轴和 x,y 范围。

TOOLTIPS_1=[("Name","@Names"), ("Total Balance","@weekly{($ 0.00 a)}")]
p1 = figure(x_range=names, plot_width=1000, plot_height=250, title="Purchases in Past 7 Days", tools="pan,wheel_zoom, reset", tooltips=TOOLTIPS_1, sizing_mode = "fixed")
p1.vbar(x='Names', top='weekly', width=1, source=source, line_color="white", color = Spectral5[0])
# x-axis for p1 is set off
p1.xaxis.visible = False
p1.yaxis[0].formatter = NumeralTickFormatter(format="[=10=].00a")
#set p1 vertical range
max_range = merge.monthly.max()
p1.y_range = Range1d(0, max_range+1000000)

TOOLTIPS_2=[("Name","@Names"), ("Total Balance","@monthly{($ 0.00 a)}")]
p2 = figure(x_range=p1.x_range, y_range = p1.y_range, plot_width=1000, plot_height=250, title="Purchases in Past 30 Days", tools="pan,wheel_zoom, reset", tooltips=TOOLTIPS_2, sizing_mode = "fixed")
p2.vbar(x='Names', top='monthly', width=1, source=source, line_color="white", color = Spectral5[1])
# p2 has a x-axis and it's the same as p1's, although p1's x-axis is turned off
p2.xaxis.major_label_orientation = 1.2
p2.yaxis[0].formatter = NumeralTickFormatter(format="[=10=].00a")

layout = column(p1,p2)
show(layout)

尽管 p1 和 p2 具有相同的范围和绘图 width/height,但我们有不同的条形图(p1 的条形图更大),因为一个有轴标签而另一个没有。现在,如何将p1、p2的条形图设置为相同大小?

从 Bokeh 1.1 开始,没有直接指定内部图框大小的直接方法,只能指定整体外部 canvas 大小。但是,您可以指定例如min_border_bottom 以确保图框下方的 space(space 通常由轴刻度和标签占据)始终至少是某个最小尺寸:

p = figure(min_border_bottom=80, ...)

因此您可以:

  • 将相同的 min_border_bottom 传递给两个地块,以确保两个地块始终在图框下方保留相同数量的 space (是否有是否有轴)。

  • 将合适的 min_border_bottom 传递给带轴的图,并从不带轴的图的 plot_height 中减去相同的值。