Bokeh包含用于绘图和小部件的多种布局选项,可以快速创建交互式数据应用程序。 布局的核心是三个核心对象是Row,Column和WidgetBox。 虽然可以直接使用这些模型,但建议使用布局函数row(),column()和widgetbox()。
为了使用布局获得最佳结果,需要记住两点:
所有项目必须具有相同的大小模式。
小工具应该放在小工具箱内。
Columns
要以垂直方式显示绘图或小部件,请使用column()函数:
from bokeh.io import output_file, show
from bokeh.layouts import column
from bokeh.plotting import figure
output_file("layout.html")
x = list(range(11))
y0 = x
y1 = [10 - i for i in x]
y2 = [abs(i - 5) for i in x]
# 创建一个图表
s1 = figure(plot_width=250, plot_height=250, title=None)
s1.circle(x, y0, size=10, color="navy", alpha=0.5)
# 创建另一个图表
s2 = figure(plot_width=250, plot_height=250, title=None)
s2.triangle(x, y1, size=10, color="firebrick", alpha=0.5)
# 再创建一个图表
s3 = figure(plot_width=250, plot_height=250, title=None)
s3.square(x, y2, size=10, color="olive", alpha=0.5)
# 垂直展示结果
show(column(s1, s2, s3))
Cows
要水平显示图形,请使用row()函数。
from bokeh.io import output_file, show
from bokeh.layouts import row
from bokeh.plotting import figure
output_file("layout.html")
x = list(range(11))
y0 = x
y1 = [10 - i for i in x]
y2 = [abs(i - 5) for i in x]
# 创建一个图表
s1 = figure(plot_width=250, plot_height=250, title=None)
s1.circle(x, y0, size=10, color="navy", alpha=0.5)
# 创建另一个图表
s2 = figure(plot_width=250, plot_height=250, title=None)
s2.triangle(x, y1, size=10, color="firebrick", alpha=0.5)
# 再创建一个图表
s3 = figure(plot_width=250, plot_height=250, title=None)
s3.square(x, y2, size=10, color="olive", alpha=0.5)
# 水平展示结果
show(row(s1, s2, s3))
Widgets
使用widgetbox()函数来布局一组小部件,相当于添加一个带按钮的工具箱。
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models.widgets import Button, RadioButtonGroup, Select, Slider
output_file("layout_widgets.html")
# 添加一些小部件
slider = Slider(start=0, end=10, value=1, step=.1, title="Slider")
button_group = RadioButtonGroup(labels=["Option 1", "Option 2", "Option 3"], active=0)
select = Select(title="Option:", value="foo", options=["foo", "bar", "baz", "quux"])
button_1 = Button(label="Button 1")
button_2 = Button(label="Button 2")
# 展示结果
show(widgetbox(button_1, slider, button_group, select, button_2, width=300))
领取专属 10元无门槛券
私享最新 技术干货