Anna Ignashkina
Anna Ignashkina

Reputation: 497

Fixing boundaries for bokeh zoom-in and zoom-out

I am struggling with zooming-in and zooming-out of a bokeh candlestick graph. It goes too much into detail with the zoom-in, and I want it to stop somewhere with at least 5-7 bars when zooming-in (depending on the position of the cursor), and go back to the original graph when zooming-out (regardless of the position of the cursor, and return to the original graph).

I tried playing around with match_aspect=True and bm.DataRange1d and I still don't get how the those.

So far it zooms in up to milliseconds and zooms out very far and not according to the aspect ratio of the initial graph.

import pandas as pd
import bokeh.models as bm
from bokeh.io import show, output_file
from bokeh.plotting import figure
from bokeh.sampledata.stocks import MSFT

df = pd.DataFrame(MSFT)[:51]

inc = df.close > df.open
dec = df.open > df.close

p = figure( tools='xpan, xwheel_zoom', active_scroll='xwheel_zoom', 
       plot_width=1000, plot_height=500,  title = "MSFT", x_range=bm.DataRange1d(bounds='auto'),
       active_drag='xpan')
# map dataframe indices to date strings and use as label overrides
p.xaxis.major_label_overrides = {
i: date.strftime('%b %d') for i, date in 
p.xaxis.bounds = (0, df.index[-1])

p.segment(df.index, df.high, df.index, df.low, color="black")
p.vbar(df.index[inc], 0.5, df.open[inc], df.close[inc], fill_color="#D5E1DD", line_color="black")
p.vbar(df.index[dec], 0.5, df.open[dec], df.close[dec], fill_color="#F2583E", line_color="black")

show(p)

Ideally, I would like to get a zoom similar to the tradingview website

Upvotes: 3

Views: 1630

Answers (1)

bigreddot
bigreddot

Reputation: 34568

The DataRange1d ranges have min_interval and max_interval properties you can set to prevent the range from ever being zoomed in smaller or larger than that:

p.x_range.min_interval = 1
p.x_range.max_interval = 10 

Upvotes: 3

Related Questions