在Bokeh中重绘Mandelbrot?
我正在学习使用Bokeh这个工具来制作互动图表。我的问题有点像在Mandelbrot图表中,当你放大某个区域时,新的区域需要重新绘制。这就需要BokehJS把新的X和Y范围发送回Bokeh的后端,以便重新生成数据,然后再把这些数据发回BokehJS进行重新绘制。请问在Bokeh中怎么做到这一点呢?
1 个回答
2
我是Bokeh项目的负责人,但我建议你使用Holoviews,因为它是建立在Bokeh之上的。直接用Bokeh来做这个事情有点底层而且繁琐,因为Bokeh没有提供一个统一的范围更新功能,只能单独更新范围的开始和结束。这意味着如果你直接使用Bokeh,你需要写很多代码来控制和合并事件,以避免不必要的重复计算。而Holoviews作为一个更高级的工具,已经帮你处理好了这些问题。它甚至有一个交互式的Mandlebrot示例可以直接运行(就像任何标准的Bokeh应用一样)。代码(去掉了分形计算的部分)看起来是这样的:
def get_fractal(x_range, y_range):
(x0, x1), (y0, y1) = x_range, y_range
image = np.zeros((600, 600), dtype=np.uint8)
return hv.Image(create_fractal(x0, x1, -y1, -y0, image, 200),
bounds=(x0, y0, x1, y1))
# Define stream linked to axis XY-range
range_stream = RangeXY(x_range=(-1., 1.), y_range=(-1., 1.))
# Create DynamicMap to compute fractal per zoom range and
# adjoin a logarithmic histogram
dmap = hv.DynamicMap(get_fractal, label='Manderbrot Explorer',
streams=[range_stream]).hist(log=True)
# Define styling options
options = hv.Store.options('bokeh')
options.Image = {
'style': Options(cmap='fire'),
'plot' : Options(logz=True, height=600, width=600,
xaxis=None, yaxis=None)
}
options.Histogram = {
'norm': Options(framewise=True),
'plot': Options(logy=True, width=200)
}
doc = BokehRenderer.server_doc(dmap)
doc.title = 'Mandelbrot Explorer'