简体   繁体   English

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

[英]Plotly chart not showing in Jupyter notebook

I have been trying to solve this issue for hours.我已经尝试解决这个问题好几个小时了。 I followed the steps on the Plotly website and 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)  

You need to change init_notebook_mode call and remove connected=True , if you want to work in offline mode.如果您想在离线模式下工作,您需要更改init_notebook_mode调用并删除connected=True

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 笔记本中:

我的例子

In case you want to use Jupyter lab, you will have to install the plotly jupyterlab extension:如果你想使用 Jupyter 实验室,你必须安装 plotly jupyterlab 扩展:

https://plotly.com/python/getting-started/#jupyterlab-support-python-35 https://plotly.com/python/getting-started/#jupyterlab-support-python-35

Simple solution: jupyter labextension install jupyterlab-plotly简单的解决方案: jupyter labextension install jupyterlab-plotly

Restart Jupyter Lab after installing the extension.安装扩展后重新启动 Jupyter Lab。

To use a plotly version below 5.0 in Jupyter Lab make sure you have ipywidgets and plotly installed and then run the following:要在 Jupyter Lab 中使用低于 5.0 的 plotly 版本,请确保您已安装 ipywidgets 和 plotly,然后运行以下命令:

jupyter labextension install jupyterlab-plotly

OPTIONAL: Jupyter widgets extension:可选:Jupyter 小部件扩展:

jupyter labextension install @jupyter-widgets/jupyterlab-manager plotlywidget

Source docs源文档

And here's the troubleshooting guide for plotly with Jupyter Lab.这是使用 Jupyter Lab 进行 plotly 的故障排除指南。

As of Plotly version 5.0, I am able to create a new conda environment with Python 3.9 and then pip install plotly jupyterlab , and run Jupyter Lab and render plots without any other package or extension installs.从 Plotly 5.0 版开始,我可以使用 Python 3.9 创建一个新的 conda 环境,然后pip install plotly jupyterlab ,运行 Jupyter Lab 并渲染绘图,而无需安装任何其他包或扩展。

Being new to Plotly, I had the same issue.作为 Plotly 的新手,我遇到了同样的问题。 I tried all of the above things but still got blank graph.我尝试了上述所有方法,但仍然得到空白图表。 Turns out, only installing the jupyterlab extensions is enough, but you need to shutdown and restart the jupyterlab itself.事实证明,只安装 jupyterlab 扩展就足够了,但是您需要关闭并重新启动 jupyterlab 本身。 Just restarting the kernel didn't help.只是重新启动内核没有帮助。

Assuming you are using JupyterLab, accordingly to Plotly Troubleshooting假设您正在使用 JupyterLab,根据Plotly Troubleshooting

In order to use plotly in JupyterLab, you must have the extensions installed as detailed in the Getting Started guide .为了在 JupyterLab 中使用 plotly,您必须按照入门指南中的详细说明安装扩展。 There are two extensions: jupyterlab-plotly for rendering figures with fig.show() and plotlywidget for the FigureWidget .有两个扩展:用于使用fig.show()渲染图形的jupyterlab-plotly和用于plotlywidgetFigureWidget

Assuming that you have installed all the libraries correctly (make sure you have ipywidgets and nodejs installed) and assuming one is using conda , access conda prompt for the environment one is working (the "Server" environment).假设您已正确安装所有库(确保已安装ipywidgetsnodejs )并假设其中一个正在使用conda ,请访问conda prompt以获取正在运行的环境(“服务器”环境)。

List the labs' extensions with列出实验室的扩展

jupyter labextension list

In my case I got就我而言,我得到了

JupyterLab v2.2.9
No installed extensions

Then I will need to install the extensions jupyterlab-plotly (the library nodejs will be required now)然后我需要安装扩展jupyterlab-plotly (现在需要库nodejs

jupyter labextension install jupyterlab-plotly@4.14.3

and plotlywidget [optional]plotlywidget [可选]

jupyter labextension install @jupyter-widgets/jupyterlab-manager plotlywidget@4.14.1

Now you'll be able to visualize your plots.现在您将能够可视化您的绘图。


Note笔记

If you use JupyterLab with multiple python environments, the extensions must be installed in the "server" environment, and the plotly python library must be installed in each "processing" environment that you intend to use.如果您将 JupyterLab 与多个 python 环境一起使用,则必须在“服务器”环境中安装扩展,并且必须在您打算使用的每个“处理”环境中安装 plotly python 库。

Those having trouble (even after installing extension) may try changing renderer.那些有问题的人(即使在安装扩展之后)可能会尝试更改渲染器。 It worked for me on JupyterLab on Chrome.它在 Chrome 上的 JupyterLab 上对我有用。

Note that this will create a iframe figure directory not pure html file.请注意,这将创建一个 iframe 图形目录而不是纯 html 文件。

If using jupyterlab install JupyterLab extension如果使用 jupyterlab 安装 JupyterLab 扩展

jupyter labextension install jupyterlab-plotly

Add this line before py.iplot or fig.show()在 py.iplot 或 fig.show() 之前添加这一行

import plotly.io as pio
pio.renderers.default = 'iframe'

If the other answers do not work for you then check if WebGL is enabled for your browser.如果其他答案对您不起作用,请检查您的浏览器是否启用了 WebGL。 You can check and enable WebGL in Chrome by following the below steps.您可以按照以下步骤在 Chrome 中检查和启用 WebGL。

  1. In the address bar, type chrome://flags/ and press ENTER在地址栏中,输入 chrome://flags/ 并按 ENTER
  2. Enable all the options with WebGL keyword使用 WebGL 关键字启用所有选项
  3. Click Relaunch Now.单击立即重新启动。 Google Chrome will restart and your new settings will be applied without closing other tabs谷歌浏览器将重新启动,您的新设置将在不关闭其他选项卡的情况下应用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM