I am having trouble limiting the min/max zoom level in bokeh using holoviews datashader. A similar question (w/o the datashader tho) is answered here.
Groundworks (I just copied from my ipynb so apologies for the out-of-proper-order code):
import bokeh
import datashader as ds
import holoviews as hv
from holoviews import opts
from holoviews.operation.datashader import datashade, shade, dynspread, spread, rasterize
hv.extension('bokeh')
As said and solved in the other SO thread, limiting zoom works fine w/o any datashader opereation.
When I create a custom mock dataframe (see N
), bokeh alone won't -reasonably- work well.
import numpy as np
import pandas as pd
N = int(10e6)
x_r = (0,100)
y_r = (100,2000)
z_r = (0,10e8)
x = np.random.randint(x_r[0]*1000,x_r[1]*1000,size=(N, 1))
y = np.random.randint(y_r[0]*1000,y_r[1]*1000,size=(N, 1))
z = np.random.randint(z_r[0]*1000,z_r[1]*1000,size=(N, 1))
z2 = np.ones((N,1)).astype(int)
df = pd.DataFrame(np.column_stack([x,y,z,z2]), columns=['x','y','z','z2'])
df[['x','y','z']] = df[['x','y','z']].div(1000, axis=0)
df
So I employ the help of holoviews datashader
# infini-zoom still possible with x_range as rasterize param
from matplotlib.cm import get_cmap
palette = get_cmap('viridis')
# palette_inv = palette.reversed()
p=hv.Points(df,['x','y'], ['z','z2'])
# Points of course doesnt have a x_range attr
# p.x_range.min_interval = 1
# p.x_range.max_interval = 10
P=rasterize(p, aggregator=ds.sum("z2"),x_range=(0,100)).opts(cmap=palette)
P.opts(tools=["hover"]).opts(height=500, width=500,xlim=(0,100),ylim=(100,2000))
rasterize
seems to ignore any x_range argument. But this leaves me with a plot where you can infinitely(?) zoom in our out. Which I want to limit. For now I'm out of ideas.