python - 在 jupyter 笔记本上显示 plotly

标签 python jupyter-notebook plotly

我有一个数据框“国家”,我正在使用 jupyter 笔记本绘制它。

它在 Kaggle 的笔记本上绘制得很好,但当我使用 jupyter 笔记本时拒绝显示..

我在 StackOverflow 上读到过类似的问题,并且我都尝试过:

init_notebook_mode(connected=True)

&

py.offline.init_notebook_mode(connected=True)

请在下面找到完整的代码:

import plotly.offline as py
from plotly.offline import iplot
py.offline.init_notebook_mode(connected=True)
import plotly.graph_objs as go

trace1 = go.Bar(
    x= country.index,
    y= country['Avg. Points'],
    name='Avg. Points'
)

trace2= go.Bar(
    x= country.index,
    y= country['Avg. Price'],
    name='Avg. Price'
)

data=[trace1, trace2]
layout=go.Layout(
    barmode='stack')

fig=go.Figure(data=data, layout=layout)
py.iplot(fig, filename='stacked-bar')

下图是它在 Kaggle 上的外观。 enter image description here

最佳答案

我尝试使用你的代码,用 Plotly 的一些数据替换你的数据 example数据。我无法复制您遇到的问题。假设您已使用 $ pip installplotly 正确安装了plotly,并且数据没有问题,则以下内容应该有效。

fromplotly.offline import iplot 不是必需的,因为您使用py.iplot

import plotly.offline as py
import plotly.graph_objs as go

py.init_notebook_mode(connected=True)

trace1 = go.Bar(
    x= country.index,
    y= country['Avg. Points'],
    name='Avg. Points'
)

trace2= go.Bar(
    x= country.index,
    y= country['Avg. Price'],
    name='Avg. Price'
)

data=[trace1, trace2]
layout=go.Layout(barmode='stack')

fig=go.Figure(data=data, layout=layout)
py.iplot(fig, filename='stacked-bar')

关于python - 在 jupyter 笔记本上显示 plotly ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50051230/

相关文章:

python - PyQt5 相当于 WPF StackPanel

python - Dash by Plotly vs Jupyter Dashboards 的优缺点是什么?

python - 如何将功能移动到类? Python

python - 在 ipython 笔记本上创建包含管道的表

python - MovieWriter imagemagick 不可用

python - 将直方图从 matplotlib 导入到plotly

plotly - Plotly 中的 float 棒图

python - 如何更新所有 Plotly 图表的全局字体颜色?

python - 在 Pandas 中找到第一个非 NaN 值

python - 在没有基于模型的数据库的情况下使用 Django View 和表单