繁体   English   中英

散景初始 x 轴缩放范围并允许在 y_range 范围内完全缩小 x 轴

[英]Bokeh initial x axis zoom range and allow full zoom out on x axis with y_range bound

通过以下内容,我可以设置过去 4 小时的初始 x 轴缩放范围。 理想情况下,我希望使用滚轮缩放仅在 x 轴上滚动并查看整个 plot 但仍由设置的范围限制在 y 轴上。 以下不允许这样做,并且设置以下任何一项也不允许:

y_range=DataRange1d(0, y_max, bounds="auto")
y_range=DataRange1d(0, y_max) # i.e. bounds=None as per docs

任何人都可以帮助我如何允许在 x 轴上进行初始视图,允许在 x 轴上缩小,但在 y 轴上设置边界,以便用户不能缩小超出最小 y=0 和(在示例)最大 y=8?

根据此处的文档,在 y 轴上静态设置边界的示例代码:

import pandas as pandas
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, DataRange1d, Range1d

output_file('stackoverflowq.html')

list1 = [['2020-12-03 09:20:03.175453','5'],['2020-12-04 09:20:03.175453','7'],['2020-12-05 09:20:03.175453','3'],['2020-12-05 09:30:03.175453','4'],['2020-12-05 09:40:03.175453','5'],['2020-12-05 09:50:03.175453','6'],['2020-12-05 10:00:03.175453','4'],['2020-12-05 10:10:03.175453','1'],['2020-12-05 10:20:03.175453','2'],['2020-12-05 10:30:03.175453','8'],['2020-12-05 10:40:03.175453','2'],['2021-01-03 09:20:03.175453','5'],['2021-01-04 09:20:03.175453','7'],['2021-01-05 09:20:03.175453','3'],['2021-01-20 09:30:03.175453','4'],['2021-01-21 01:40:03.175453','5'],['2021-01-21 02:50:03.175453','6'],['2021-01-21 06:00:03.175453','4'],['2021-01-21 07:10:03.175453','1'],['2021-01-21 08:20:03.175453','2'],['2021-01-21 09:30:03.175453','8'],['2021-01-21 10:40:03.175453','2']]

cols = ['DateTime','vals']
df = pandas.DataFrame(list1,columns=cols)
df['DateTime'] = pandas.to_datetime(df['DateTime'])
df = df.set_index('DateTime')

y_max = int(df.vals.unique().max())*1.2

x_max = df.index.unique().max()
x_min = x_max - pandas.Timedelta('4h')

p = figure(x_axis_type="datetime", y_range=DataRange1d(0, y_max, bounds=(0, y_max)), x_range=(x_min, x_max))

p.line(x='DateTime', y='vals', color='blue', source=df)

p.xaxis.axis_label = 'Date Time'
p.yaxis.axis_label = 'vals'

show(p)

您只需更改滚轮缩放工具。 使用tools='xwheel_zoom'

示例请使用我的最小示例,如果您仍有此问题,请给我一些反馈。

import pandas as pd # version 1.1.4
from bokeh.plotting import figure, output_notebook, show # version 2.2.3
from bokeh.models import ColumnDataSource, DataRange1d, Range1d
output_notebook()
date_times = pd.Timestamp.now()
x_max = date_times
x_min = date_times - pd.Timedelta('4h')
x = [x_max - pd.Timedelta(f'{t}h') for t in range(5,0,-1)]
y = [2, 5, 8, 2, 7]

other_tools = "pan, box_zoom, save, reset, help,"
p = figure(x_axis_type="datetime", tools="xwheel_zoom,"+other_tools, x_range=(x_min,x_max))
p.circle(x, y, size=10)
show(p)
初始缩放 使用wheel_zoom 使用xwheel_zoom
初始缩放 缩小 缩小 xwheel_zoom
选择栏 您必须通过鼠标单击 select 滚轮缩放工具。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM