第一步 6: 组合绘图#

之前的第一步指南中,您创建了单独的图表。

在本节中,您将把多个图表组合成不同类型的布局。

创建行、列和网格#

组合单个图表的最简单方法是将它们分配到行或列中。

例如:

要将多个图表组合成水平行布局,首先需要导入 row。然后在调用 show() 时使用 row() 函数:

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

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

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

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

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

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

要在一个垂直列布局中显示多个图表,请使用 column() 函数代替。

在Bokeh中安排元素的一种更灵活的方法是使用gridplot()函数。

另请参阅

有关row()column()gridplot()的更多信息,请参阅用户指南中的网格和布局

定义尺寸行为#

您可以使用函数 row(), column(), 和 gridplot() 并附加参数来定义 Bokeh 如何缩放各个图表。请参阅 sizing_mode 以获取 Bokeh 支持的所有缩放模式的列表。

例如:为了使一行中的所有绘图响应式地填充浏览器窗口的可用宽度,将 scale_width 分配给 sizing_mode

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

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

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

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

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

# put the results in a row that automatically adjusts
# to the browser window's width
show(row(children=[s1, s2, s3], sizing_mode="scale_width"))

另请参阅

有关尺寸模式的更多信息,请参阅用户指南中的 尺寸模式