与bokeh交互突出显示时间序列的特定范围

2024-03-29 07:43:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的时间序列图。在

在时间序列图的顶部,我需要一个突出显示特定范围的图层。我脑海中浮现了一些字形,如下所示:

fig.quad(top=[10], bottom=[0], left=[0], right=[100], color="red", fill_alpha = 0.2)

现在用户应该能够添加/删除这样的glyph并调整它们的left和{}参数(最好是通过拖动/移动左右边框)。bottom和{}参数不重要,应分别为-Inf和{}。服务器的调整应该保存下来。在

我想到了使用矩形字形的^{},但是我错过了编辑left和{}参数的功能。(我知道我可以删除并添加一个glyph来更改左/右参数,但这不是一个选项,因为需要放大和调整左/右参数)。在

有没有办法交互调整左右参数?或者是一种完全不同的方法来突出特定的范围?在

PS I还注意到BoxEditTool在较大的时间序列上非常慢/没有响应。在


Tags: right图层参数top时间figredleft
1条回答
网友
1楼 · 发布于 2024-03-29 07:43:20

0.13版本引入了RangeTool

import numpy as np

from bokeh.io import show
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, RangeTool
from bokeh.plotting import figure
from bokeh.sampledata.stocks import AAPL

dates = np.array(AAPL['date'], dtype=np.datetime64)
source = ColumnDataSource(data=dict(date=dates, close=AAPL['adj_close']))

p = figure(plot_height=300, plot_width=800, tools="", toolbar_location=None,
           x_axis_type="datetime", x_range=(dates[1500], dates[2500]))

p.line('date', 'close', source=source)
p.yaxis.axis_label = 'Price'

select = figure(plot_height=150, plot_width=800, y_range=p.y_range,
                x_axis_type="datetime", y_axis_type=None,
                tools="", toolbar_location=None)

range_rool = RangeTool(x_range=p.x_range)
range_rool.overlay.fill_color = "navy"
range_rool.overlay.fill_alpha = 0.2

select.line('date', 'close', source=source)
select.ygrid.grid_line_color = None
select.add_tools(range_rool)
select.toolbar.active_multi = range_rool

show(column(p, select))

enter image description here

相关问题 更多 >