@Bryan I was able to make a minimal, reproducible example. The following code snippet (180 lines) makes one figure, similar to how I make all the figures on my production web app (Snowpack Tracker). I simply grabbed some snippets of recent data and pasted them into the code, then making a Pandas dataframe and ColumnDataSource with this data exactly as I do in my full code.
Note lines 35-37, where I have:
y_range=DataRange1d(
only_visible=True,
min_interval=50.
)
My interpretation of the docs is that the min_interval
setting should not allow the y-axis to zoom in further than 50 data units. In this case, I am plotting relative humidity data with units of “%”, and the full range of the data is from approximately 88% to 100% (12 data units). So, with min_interval=50.
, I would expect the y-axis range to not reduce beyond 50 units (e.g. 50% to 100%). Would this require setting a minimum or maximum bound to have one end of the interval bounded? I tried playing with the bounds
and end
parameters, but did not see any changes.
My end goal is to be able to update my figures with the date range slider or radio buttons to various date ranges (which updates the CDS for each figure), and then let the y-axis auto-scale to the range of visible data for the glyphs that are toggled on in the legend. However, I would like to set a minimum range that the y-axis cannot “zoom” beyond (using min_interval
). I can also drop my date range slider and radio buttons into this example, but because the issue is present without these widgets, I did not include them.
If you put this code snippet in a file named main.py
, put in a directory and run with bokeh serve
, it should work.
bokeh==2.4.1
Thanks!
#!/usr/bin/env python
'''
min_span not working, minimal reproducible example
Author: P. Wright, Inversion Labs
Created: Nov 14, 2021
'''
import pandas as pd
from bokeh.models import Legend,DataRange1d,ColumnDataSource
from bokeh.plotting import figure, curdoc
from bokeh.layouts import gridplot
def make_rh_fig(panel_df, start, end):
'''
Create the relative humidity panel and plot data
CDS created in-place
Parameters
----------
panel_df: the pandas dataframe for this panel
start: the global start date
end: the global end date
Returns
-------
p: Bokeh Figure object
'''
panel_df['Date'] = panel_df.index.tz_localize(None)
cds = ColumnDataSource(data=panel_df)
p = figure(
title="Relative Humidity",
name="relative_humidity_fig",
x_axis_type='datetime',
x_range = (start,end),
y_axis_label='%',
y_range=DataRange1d(
only_visible=True,
min_interval=50.,
end=100.
)
)
legenddict={}
items=[]
tooltips=[("Date", "@Date_hover")]
legenddict['foo'] = p.line(x='Date', y='foo',
line_width=2, color='blue',
alpha=0.8, name='foo', source=cds)
items.append(('foo',[legenddict['foo']]))
legend1 = Legend(items=items[0:],)
p.add_layout(legend1, 'below')
return p
#---------------------------------------------
# TEST DATA
#---------------------------------------------
datetime = [
'2021-11-11 12:00:00',
'2021-11-11 13:00:00',
'2021-11-11 14:00:00',
'2021-11-11 15:00:00',
'2021-11-11 16:00:00',
'2021-11-11 17:00:00',
'2021-11-11 18:00:00',
'2021-11-11 19:00:00',
'2021-11-11 20:00:00',
'2021-11-11 21:00:00',
'2021-11-11 22:00:00',
'2021-11-11 23:00:00',
'2021-11-12 00:00:00',
'2021-11-12 01:00:00',
'2021-11-12 02:00:00',
'2021-11-12 03:00:00',
'2021-11-12 04:00:00',
'2021-11-12 05:00:00',
'2021-11-12 06:00:00',
'2021-11-12 07:00:00',
'2021-11-12 08:00:00',
'2021-11-12 09:00:00',
'2021-11-12 10:00:00',
'2021-11-12 11:00:00',
'2021-11-12 12:00:00',
'2021-11-12 13:00:00',
'2021-11-12 14:00:00',
'2021-11-12 15:00:00',
'2021-11-12 16:00:00',
'2021-11-12 17:00:00',
'2021-11-12 18:00:00',
'2021-11-12 19:00:00',
'2021-11-12 20:00:00',
'2021-11-12 21:00:00',
'2021-11-12 22:00:00',
'2021-11-12 23:00:00',
'2021-11-13 00:00:00',
'2021-11-13 01:00:00',
'2021-11-13 02:00:00',
'2021-11-13 03:00:00',
'2021-11-13 04:00:00',
'2021-11-13 05:00:00',
'2021-11-13 06:00:00',
'2021-11-13 07:00:00',
'2021-11-13 08:00:00',
'2021-11-13 09:00:00',
'2021-11-13 10:00:00',
'2021-11-13 11:00:00',
'2021-11-13 12:00:00']
rh = [
88.0,
88.0,
92.0,
93.0,
93.0,
94.0,
94.0,
93.0,
93.0,
93.0,
93.0,
94.0,
94.0,
94.0,
94.0,
94.0,
95.0,
95.0,
97.0,
98.0,
98.0,
98.0,
98.0,
99.0,
99.0,
99.0,
99.0,
99.0,
99.0,
99.0,
99.0,
99.0,
99.0,
99.0,
100.0,
99.0,
98.0,
98.0,
97.0,
99.0,
99.0,
100.0,
100.0,
100.0,
100.0,
99.0,
99.0,
99.0,
98.0]
df_dict = {'dt':datetime,'data':rh}
df_test = pd.DataFrame(data=df_dict['data'],
columns=['foo'],
index=pd.to_datetime(df_dict['dt'])
)
#---------------------------------------------
# MAKE FIGURE
#---------------------------------------------
global_start = df_test.index.min()
global_end = df_test.index.max()
relative_humidity_fig = make_rh_fig(
df_test,
global_start,
global_end
)
grid = gridplot(
children = [relative_humidity_fig],
ncols=1,
)
curdoc().add_root(grid)