【问题标题】:Using holoviews renderer with datashader in a script在脚本中使用带有数据着色器的 holoviews 渲染器
【发布时间】:2018-03-22 10:08:54
【问题描述】:

我正在尝试将 HoloViews + Datashaeder 用作 Python 脚本的一部分,而不是仅在笔记本中使用。

我可以创建绘图并在笔记本中查看它,但是当我按照常见问题解答中的示例在没有笔记本的情况下进行渲染时,我收到散景错误:

import holoviews as hv
import numpy as np
from holoviews.operation.datashader import datashade, dynspread
hv.extension('bokeh')

np.random.seed(1)
positions = np.random.multivariate_normal((0,0),[[0.1,0.1], [0.1,1.0]], (1000000,))
points = hv.Points(positions,label="Points")
plot = datashade(points) + dynspread(datashade(points))

renderer = hv.renderer('matplotlib').instance(fig='html')
renderer.save(plot,'testing')

Traceback (most recent call last):
  File "holoviews_test.py", line 21, in <module>
    renderer.save(plt_out, 'exampleFAQ2') 
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/renderer.py", line 465, in save
    plot = self_or_cls.get_plot(obj)
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/bokeh/renderer.py", line 112, in get_plot
    plot = super(BokehRenderer, self_or_cls).get_plot(obj, renderer)
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/renderer.py", line 177, in get_plot
    **plot_opts)
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/bokeh/raster.py", line 20, in __init__
    super(RasterPlot, self).__init__(*args, **kwargs)
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/bokeh/element.py", line 180, in __init__
    self.callbacks = self._construct_callbacks()
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/bokeh/element.py", line 216, in _construct_callbacks
    cbs.append(cb(self, cb_streams, source))
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/bokeh/callbacks.py", line 54, in __init__
    self.comm = self._comm_type(plot, on_msg=self.on_msg)
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/comms.py", line 210, in __init__
    self.manager = get_ipython().kernel.comm_manager
 AttributeError: 'NoneType' object has no attribute 'kernel'

当我尝试使用 matplotlib 制作相同的情节时:

hv.extension('matplotlib')
renderer = hv.renderer('matplotlib').instance(fig='png')
renderer.save(plot,'testing')

Traceback (most recent call last):
  File "holoviews_test.py", line 21, in <module>
    renderer.save(plt_out, 'exampleFAQ2') #, style=dict(Image=
    {'cmap':'jet'}))
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/renderer.py", line 465, in save
    plot = self_or_cls.get_plot(obj)
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/renderer.py", line 178, in get_plot
    plot.update(0)
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/mpl/plot.py", line 244, in update
    return self.initialize_plot()
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/mpl/plot.py", line 43, in wrapper
    return f(self, *args, **kwargs)
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/mpl/plot.py", line 1090, in 
    initialize_plot
    hspace=self.hspace*self.fig_scale)
  File "/anaconda2/lib/python2.7/site-
    packages/holoviews/plotting/mpl/util.py", line 113, in fix_aspect
    bbox = ax.get_tightbbox(fig.canvas.renderer)
AttributeError: 'FigureCanvasMac' object has no attribute 'renderer'

渲染示例代码适用于基本的 holoviews 元素,但在我添加多个数据着色器方法后会失败。任何帮助将不胜感激!

【问题讨论】:

    标签: python holoviews datashader


    【解决方案1】:

    这可能是一个错误,我已向 HoloViews here 提交了一个问题。这里发生的事情是datashade 将所谓的流附加到图上,只要轴范围发生变化,它就会更新图。您可以通过在 datashade 上设置 datashade.dynamic=False 手动禁用此行为,或将其直接传递给调用。

    这是您的示例的一个有效版本(另请注意,我已将 fig 格式更改为 'svg',因为 matplotlib 不会像您最初声明的那样原生呈现为 'html'

    import holoviews as hv
    import numpy as np
    from holoviews.operation.datashader import datashade, dynspread
    hv.extension('bokeh')
    
    #### Disable dynamic updating of plot
    datashade.dynamic = False
    
    np.random.seed(1)
    positions = np.random.multivariate_normal((0,0),[[0.1,0.1], [0.1,1.0]], (1000000,))
    points = hv.Points(positions,label="Points")
    plot = datashade(points, dynamic=False) + dynspread(datashade(points))
    
    renderer = hv.renderer('matplotlib').instance(fig='svg')
    renderer.save(plot,'testing')
    

    【讨论】:

    • 如果我添加动态关键字以及为 datashade 函数指定 x_range 和 y_range,绘图将使用 bokeh + html 保存。但是,这段带有 matplotlib 的代码给了我与上面相同的 FigureCanvasMac 错误。也许这是我的 matplotlib 版本?我目前正在使用 matplotlib 2.0.0、holoviews 1.8.3 和 datashader 0.4.0
    猜你喜欢
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2012-02-27
    • 2012-08-14
    相关资源
    最近更新 更多