Python 绘图图表未显示在 Jupyter 笔记本中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52771328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:10:49  来源:igfitidea点击:

Plotly chart not showing in Jupyter notebook

pythonjupyter-notebookplotly

提问by Liz

I have been trying to solve this issue for hours. I followed the steps on the Plotly websiteand the chart still doesn't show in the notebook.

几个小时以来,我一直试图解决这个问题。我按照Plotly 网站上的步骤操作,但图表仍然没有显示在笔记本中。

This is my code for the plot:

这是我的情节代码:

colorway = ['#f3cec9', '#e7a4b6', '#cd7eaf', '#a262a9', '#6f4d96', '#3d3b72', '#182844']

data = [
    go.Scatter(
        x = immigration.columns,
        y = immigration.loc[state],
                   name=state) for state in immigration.index]

layout = go.Layout(
    title='Immigration',
    yaxis=dict(title='Immigration %'),
    xaxis=dict(title='Years'),
    colorway=colorway,
    font=dict(family='Courier New, monospace', size=18, color='#7f7f7f')
)

fig = go.Figure(data=data, layout=layout)
iplot(fig)

And this is everything I have imported into my notebook:

这是我导入到笔记本中的所有内容:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot

init_notebook_mode(connected=True)  

回答by Oysiyl

You need to change init_notebook_modecall, if you want to work in offline mode.

init_notebook_mode如果您想在离线模式下工作,则需要更改通话。

Such that:

这样:

# Import the necessaries libraries
import plotly.offline as pyo
import plotly.graph_objs as go
# Set notebook mode to work in offline
pyo.init_notebook_mode()
# Create traces
trace0 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[10, 15, 13, 17]
)
trace1 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[16, 5, 11, 9]
)
# Fill out data with our traces
data = [trace0, trace1]
# Plot it and save as basic-line.html
pyo.iplot(data, filename = 'basic-line')

Output should be shown in your jupyter notebook:

输出应显示在您的 jupyter 笔记本中:

My example

我的例子

回答by Hammer. Wang

In case you want to use Jupyter lab, you will have to install the plotly jupyterlab extension: https://github.com/jupyterlab/jupyter-renderers/tree/master/packages/plotly-extension.

如果您想使用 Jupyter 实验室,则必须安装 plotly jupyterlab 扩展:https: //github.com/jupyterlab/jupyter-renderers/tree/master/packages/plotly-extension

Update 2020-01-07

更新 2020-01-07

See the new link: https://www.npmjs.com/package/@jupyterlab/plotly-extension

查看新链接:https: //www.npmjs.com/package/@jupyterlab/plotly-extension

回答by jeffhale

To use Plotly in Jupyter Lab make sure you have ipywidgets and plotly installed and then run the following:

要在 Jupyter Lab 中使用 Plotly,请确保您已安装 ipywidgets 和 plotly,然后运行以下命令:

jupyter labextension install [email protected]

jupyter labextension install [email protected]

# OPTIONAL: Jupyter widgets extension

# OPTIONAL: Jupyter widgets extension

jupyter labextension install @jupyter-widgets/jupyterlab-manager [email protected]

jupyter labextension install @jupyter-widgets/jupyterlab-manager [email protected]

Change the version number from 4.7.1 if you are reading this in the future.

如果您将来阅读本文,请将版本号从 4.7.1 更改。

Source docs

源文档

And here'sthe troubleshooting guide for Plotly with Jupyter Lab.

这里的故障排除指南Plotly与Jupyter实验室。