【问题标题】:How to render charts in Databricks using Plotly?如何使用 Plotly 在 Databricks 中呈现图表?
【发布时间】:2019-11-07 23:52:09
【问题描述】:

我正在尝试使用 Databricks 中的 Plotly 库来呈现图表。但是,没有渲染图像。例如,我使用以下语句:

from plotly.offline import init_notebook_mode, iplot
from plotly import graph_objs as go

    # Initialize plotly
    init_notebook_mode(connected=True)

    daily_df=df

    def plotly_df(df, title=''):
        """Visualize all the dataframe columns as line plots."""
        common_kw = dict(x=df.index, mode='lines')
        data = [go.Scatter(y=df[c], name=c, **common_kw) for c in df.columns]
        layout = dict(title=title)
        fig = dict(data=data, layout=layout)
        iplot(fig, show_link=False)

    plotly_df(daily_df)

没有输出。为什么?

【问题讨论】:

    标签: python-3.x matplotlib plotly databricks


    【解决方案1】:

    如果您使用plotly 4.2 版,则不再需要import plotly.offline,您只需在使用Plotly Express 或go.Figure() 创建的图形上调用fig.show()。新的渲染器框架有一个 Databricks 特定的渲染器:)

    此处的完整文档:https://plot.ly/python/renderers/ 和 https://plot.ly/python/creating-and-updating-figures/

    【讨论】:

    • 您好,我收到 plotly 4.8.2 的“Mime 类型渲染需要 nbformat>=4.2.0 但未安装”
    【解决方案2】:

    编辑:Plotly 已更新其库以支持在 Databricks 中更好地呈现。请参阅上面的答案。

    通过查看the docs,您必须指定output_type='div' 并将绘图对象传递给displayHTML()。这是 Python 笔记本或单元格中的一个工作示例:

    ## Install & import plotly
    !pip install plotly
    from plotly.offline import plot
    import plotly.graph_objs as go
    import numpy as np
    
    x = np.random.randn(2000)
    y = np.random.randn(2000)
    
    # Instead of simply calling plot(...), store your plot as a variable and pass it to displayHTML().
    # Make sure to specify output_type='div' as a keyword argument.
    # (Note that if you call displayHTML() multiple times in the same cell, only the last will take effect.)
    
    p = plot(
      [
        go.Histogram2dContour(x=x, y=y, contours=dict(coloring='heatmap')),
        go.Scatter(x=x, y=y, mode='markers', marker=dict(color='white', size=3, opacity=0.3))
      ],
      output_type='div'
    )
    
    displayHTML(p)
    

    【讨论】:

      猜你喜欢
      • 2012-09-06
      • 1970-01-01
      • 2020-06-27
      • 2011-01-06
      • 1970-01-01
      • 2023-01-18
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      相关资源
      最近更新 更多