下面是一个使用Bokeh库创建分组轴和范围选择工具的示例代码:
from bokeh.io import show
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource, RangeTool
from bokeh.plotting import figure
# 创建一个包含数据的列数据源
source = ColumnDataSource(data=dict(x=[], y=[]))
# 创建两个绘图工具
p1 = figure(plot_height=300, plot_width=800, tools="xpan", toolbar_location=None,
x_axis_type="datetime", x_axis_location="above",
background_fill_color="#efefef", x_range=(0, 100), y_range=(0, 10))
p1.line('x', 'y', source=source)
p1.yaxis.axis_label = 'Value'
p2 = figure(plot_height=100, plot_width=800, tools="xpan", toolbar_location=None,
x_axis_type="datetime", x_axis_location="above",
background_fill_color="#efefef", x_range=(0, 100), y_range=(0, 10))
p2.line('x', 'y', source=source)
p2.yaxis.axis_label = 'Value'
# 创建范围选择工具
range_tool = RangeTool(x_range=p1.x_range)
range_tool.overlay.fill_color = "navy"
range_tool.overlay.fill_alpha = 0.2
# 将范围选择工具应用于第二个绘图工具
p2.add_tools(range_tool)
p2.toolbar.active_multi = range_tool
# 创建一个网格布局并显示图形
p = gridplot([[p1], [p2]])
show(p)
这个示例代码创建了两个绘图工具,一个用于显示主要的数据图形,另一个用于显示范围选择工具。范围选择工具可以通过拖动和缩放选择一个特定的时间范围。选定的范围会自动应用于主绘图工具的x轴范围。
需要注意的是,此示例使用了Bokeh的网格布局(gridplot)来将两个绘图工具组合在一起。
下一篇:Bokeh - 更新滑动条函数