简体   繁体   中英

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

With the following I can set an initial x axis zoom range of the last 4 hours. Ideally I wish to use the wheel zoom to scroll out on the x axis only and view the whole plot but remain bound on the y axis by the range set. The following does not allow this and setting either of the following also does not:

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

Can anyone assist in how I can allow an initial view on the x axis, allow zoom out on the x axis, but set bounds on the y axis such that the user cannot zoom out beyond the plot boundaries of min y=0 and (in the example) max y=8?

Example code with bounds statically set on y axis as per docs here :

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)

You have simply to change the wheel zoom tool. Use tools='xwheel_zoom' .

Example Please use my minimal example and give me some feedback if you have still this issue.

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)
Inital zoom Zoomed Out with wheel_zoom Zoomed Out with xwheel_zoom
初始缩放 缩小 缩小 xwheel_zoom
选择栏 You have to select the wheel zoom tool by mouse click.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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