【问题标题】:How to convert a matplotlib.pyplot to a bokeh plot如何将 matplotlib.pyplot 转换为散景图
【发布时间】:2017-11-09 11:01:38
【问题描述】:

我今天一直在阅读有关如何在 Django 模板中渲染 matplotlib.pyplot。

我找到了散景库,并试图将我的 matplotib 在有效输入中转换为散景组件。我读到 .to_boke 方法已弃用

        datos = np.random.randn(1000)
        ## Discretizamos el conjunto de valores en n intervalos,
        ## en este caso 8 intervalos
        datosbin = np.histogram(datos,
                                bins=np.linspace(np.min(datos), np.max(datos), 9))[0]
        ## Los datos los queremos en tanto por ciento
        datosbin = datosbin * 100. / len(datos)
        ## Los datos los queremos en n direcciones/secciones/sectores,
        ## en este caso usamos 8 sectores de una circunferencia
        sect = np.array([90, 45, 0, 315, 270, 225, 180, 135]) * 2. * math.pi / 360.
        nombresect = ['E', 'NE', 'N', 'NW', 'W', 'SW', 'S', 'SE']
        ## Dibujamos la rosa de frecuencias
        plt.axes([0.1, 0.1, 0.8, 0.8], polar=True)
        plt.bar(sect, datosbin, align='center', width=45 * 2 * math.pi / 360.,
                facecolor='b', edgecolor='k', linewidth=2, alpha=0.5)
        plt.thetagrids(np.arange(0, 360, 45), nombresect, frac=1.1, fontsize=10)
        plt.title(u'Procedencia de las nubes en marzo')
        script, div = components(plt, CDN)
        return render(request, 'consulta/resultado/imprimir.html', {'variables': variables,
                                                                    'respuesta3': peticion3.content,
                                                                    'lugar': lugar,
                                                                    'hora_actual': hora_actual,
                                                                    'hora_siguiente': hora_siguiente,
                                                                    'dias': horas,
                                                                    'Variables': variables_posibles,
                                                                    'latitud':latitud,
                                                                    'longitud': longitud,
                                                                    "the_script": script,
                                                                    "the_div": div})

我有一个 valueError(显然 matplotlib.pyplot 不是有效输入):

我在这里堆。这是我第一次接触图书馆和 matplot。

感谢您的帮助。非常感谢。

PS:我已经编码并尝试打印的图:

【问题讨论】:

  • 自 Bokeh 12.5(2017 年 4 月)以来,support for Matplotlib has been deprecated,因此 mpl.to_bokeh() 不再可用。另请参阅 bryevdv 的 this Github comment
  • 我投票决定将此问题作为题外话结束,因为 Bokeh 不再提供 MPL 支持(并且很久没有提供)。如果 SO 不允许一个正确的答案来说明这一点(这只是唯一的答案),那么应该删除问题本身,以避免成为 Bokeh 用户混淆的来源。

标签: django python-3.x django-templates bokeh


【解决方案1】:

您所要求的内容不受支持且不存在。在 Bokeh 或 Matplotlib 中,没有将 Matplotlib 输出转换为 Bokeh 输出的特性或功能。因此,这个问题的答案是:

你所要求的是不可能的。

(作为 Bokeh 的共同创造者和主要维护者发言)重要的是,用户必须清楚明确地了解将 MPL 转换为 Bokeh 并没有“灵丹妙药”。其他任何内容都是错误信息

生成 Bokeh 输出的唯一选择是直接使用本机 Bokeh API,例如bokeh.plotting API。特别是,您可能想查看wedge glyph,但请注意,截至1.2.0,Bokeh 没有任何内置径向轴,因此您必须“手动绘制所有轴元素和标签” ”。

【讨论】:

  • 因此,鉴于 matplotlib 代码和示例的丰富性,我们应该继续使用 MPL .. ?
  • 这完全取决于您。当然可以使用 Bokeh API 在上图中绘制输出。对于您的特定情况,这样做是否值得完全取决于您的优先事项和需求。是只需要普通的静态图像还是需要 Bokeh 的交互功能。
猜你喜欢
  • 2021-02-28
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 2016-07-18
  • 1970-01-01
  • 2023-01-03
相关资源
最近更新 更多