You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Bokeh中按比例调整绘图高度,使底部图为顶部的25%?

解决Bokeh绘图高度比例分配问题

要实现第一个绘图占75%窗口高度、第二个绘图占25%且整体填满窗口的效果,你可以利用Bokeh布局的flex属性控制组件尺寸比例,具体修改如下:

# 运行命令: bokeh serve --show test.py
from bokeh.layouts import column
from bokeh.plotting import figure, curdoc

x = list(range(11))
y0 = x
y1 = [10 - i for i in x]

# 移除固定width和height,让布局控制尺寸
s1 = figure(background_fill_color="#fafafa")
s1.circle(x, y0, size=12, color="#53777a", alpha=0.8)

s2 = figure(background_fill_color="#fafafa")
s2.triangle(x, y1, size=12, color="#c02942", alpha=0.8)

# 在column中通过flex参数设置高度比例:s1占3份,s2占1份(即s2高度是s1的25%)
# sizing_mode="stretch_both"让整个布局填满窗口
curdoc().add_root(column(
    s1, s2,
    sizing_mode="stretch_both",
    # 为每个子元素指定flex值,控制尺寸分配权重
    flex=[3, 1]
))

关键说明:

  • 移除figure构造中的固定width和height,让绘图尺寸由父布局column控制。
  • column的flex参数接受列表,每个元素对应子组件的尺寸权重。这里[3,1]表示第一个组件占总可用空间的75%,第二个占25%,正好满足“底部绘图高度为第一个的25%”的需求。
  • sizing_mode="stretch_both"确保整个布局自动填满浏览器窗口宽高,窗口缩放时绘图会自适应调整。

内容的提问来源于stack exchange,提问作者Jase

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 02:06:35