Bokeh 创建子图与布局

Bokeh 创建子图与布局

https://docs.bokeh.org/en/latest/docs/user_guide/layout.html


Bokeh包括几个用于绘图和小部件的布局选项。通过这些,您可以排列多个组件以创建交互式仪表板和数据应用程序。

布局函数允许您构建绘图和小部件的网格。在一个布局中,可以有任意多个打印行、列或网格。Bokeh布局还允许许多大小选择或模式。这些模式允许绘图和小部件调整大小以适应浏览器窗口。

Basic Layout

Column Layout

要垂直显示绘图或小部件,请使用column() 函数。

from bokeh.io import output_notebook, show
from bokeh.layouts import column
from bokeh.plotting import figure

output_notebook()

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

# create three plots
s1 = figure(width=250, height=250, background_fill_color="#fafafa")
s1.circle(x, y0, size=12, color="#53777a", alpha=0.8)

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

s3 = figure(width=250, height=250, background_fill_color="#fafafa")
s3.square(x, y2, size=12, color="#d95b43", alpha=0.8)

# put the results in a column and show
show(column(s1, s2, s3))

在这里插入图片描述

Row Layout

要水平显示绘图或小部件,请使用 row() 函数。

from bokeh.io import output_notebook, show
from bokeh.layouts import row
from bokeh.plotting import figure

output_notebook()

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

# create three plots
s1 = figure(width=250, height=250, background_fill_color="#fafafa")
s1.circle(x, y0, size=12, color="#53777a", alpha=0.8)

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

s3 = figure(width=250, height=250, background_fill_color="#fafafa")
s3.square(x, y2, size=12, color="#d95b43", alpha=0.8)

# put the results in a row and show
show(row(s1, s2, s3))

在这里插入图片描述

Grid Layout for plots

使用gridplot() 函数在网格中排列 Bokeh 图。此功能还可以将所有打印工具合并到单个工具栏中。网格中的每个绘图都具有相同的活动工具。

通过将 None 而不是 plot 对象传递给网格单元,可以将网格单元留空。

from bokeh.io import output_notebook, show
from bokeh.layouts import gridplot
from bokeh.plotting import figure

output_notebook()

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

# create three plots
s1 = figure(background_fill_color="#fafafa")
s1.circle(x, y0, size=12, alpha=0.8, color="#53777a")

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

s3 = figure(background_fill_color="#fafafa")
s3.square(x, y2, size=12, alpha=0.8, color="#d95b43")

# make a grid
grid = gridplot([[s1, s2], [None, s3]], plot_width=250, plot_height=250)

show(grid)

在这里插入图片描述

为方便起见,您还可以传递一个绘图列表,并指定网格中所需的列数。例如:

gridplot([s1, s2, s3], ncols=2)

还可以传入 plot_widthplot_height 。这些尺寸将应用于所有绘图。

默认情况下,gridplot() 将所有子打印工具合并到一个父网格工具栏中。若要禁用此行为,请将 merge_tools 设置为False

from bokeh.io import output_notebook, show
from bokeh.layouts import gridplot
from bokeh.plotting import figure

output_notebook()

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

# create three plots
s1 = figure(background_fill_color="#fafafa")
s1.circle(x, y0, size=12, alpha=0.8, color="#53777a")

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

s3 = figure(background_fill_color="#fafafa")
s3.square(x, y2, size=12, alpha=0.8, color="#d95b43")

# make a grid
grid = gridplot([s1, s2, s3], ncols=2, plot_height=250, plot_width=250)

show(grid)

在这里插入图片描述

General grid Layout

可以使用layout()函数将绘图和小部件排列到网格中。这会自动生成适当的row()columns()布局,从而节省时间。请尝试以下代码以了解此函数的工作原理。

sliders = column(amp, freq, phase, offset)

layout([
    [bollinger],
    [sliders, plot],
    [p1, p2, p3],
])

For complete code see examples/howto/layouts/dashboard.py.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值