在jupyter lab中,Plotly输出为空字段

25

我正在Jupyter Lab中使用Plotly,但输出为空白。我遇到了与此处描述的问题完全相同的问题:plotly.offline.iplot输出一个大的空白字段 - 为什么?

我尝试了他们在答案中建议的方法,但是没有起作用。

这是我使用的代码:

import pandas as pd
import numpy as np
%matplotlib inline

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
cf.go_offline()

df = pd.DataFrame(np.random.randn(100,4), columns='A B C D'.split())
df2 = pd.DataFrame({'category':['A','B','C'], 'values':[32,43,50]})

df.iplot(kind='scatter', x='A',y='B', mode='markers', size=10)

其中一个建议是将笔记本更改为“可信任”的状态。你知道我可以在jupyter lab中如何做到这一点吗?


观察:代码在Jupyter Notebook中运行良好。问题只出现在Jupyter Lab中。 - Leda Grasiele
2
这里有同样的“观察结果”。问题与JupyterLab的“labextension”相关。 - YaOzI
对于那些在 jupyter notebook 中遇到了相同的问题的人,一个对我有效的解决方案是调用 fig.show('notebook')。这个解决方案是在这里提出的:https://plotly.com/python/troubleshooting/#import-problems - Leda Grasiele
3
对于遇到类似问题的人来说,一个解决方案可能是尝试使用另一个浏览器。我曾经遇到过类似的问题,尝试排除故障,结果发现我的标准浏览器是问题的根源(不知道为什么),但切换到另一个浏览器解决了我的问题。 - LeSchwambo
4个回答

14

为了在JupyterLab中正确显示离线图形,需要进行以下步骤:

步骤1:首先需要安装JupyterLab的plotly-extension

$ jupyter labextension install @jupyterlab/plotly-extension

(请注意,上述步骤需要 Node.js >= 4,如果您的操作系统中没有安装 Node.js,请从其官方网站安装。)

第二步:检查安装@jupyterlab/plotly-extension后的状态:

$ jupyter labextension list 
JupyterLab v0.35.5
Known labextensions:
   app dir: /Users/yourname/anaconda3/share/jupyter/lab
        @jupyterlab/plotly-extension v0.18.2  enabled  OK

Build recommended, please run `jupyter lab build`:
    @jupyterlab/plotly-extension needs to be included in build

步骤三:按照建议重新构建带有新安装的labextensions的JupyterLab:

$ jupyter lab build

在这些操作完成后,重新启动JupyterLab,并在每个笔记本会话的开始处设置plotly.offline.init_notebook_mode(connected=True),然后plotly.offline.iplot应该可以正确地在笔记本中显示图形。


这对我来说是一个快速解决方案,使用了新扩展,因为之前的扩展已经被弃用。感谢@YaOzl! - Jason R Stevens CFA
2
这个包(@jupyterlab/plotly-extension)已被弃用,请使用由Plotly支持的jupyterlab-plotly(https://www.npmjs.com/package/jupyterlab-plotly)。 - Michael Belvedersky

13

@YaOzI的回答部分正确,但@jupyterlab/plotly-extension已被弃用,并且不受官方plotly团队支持,您可以在这里阅读到相关信息。

这可能会导致以下错误:

ValueError: The extension "@jupyterlab/plotly-extension" does not yet support the current version of JupyterLab.


Conflicting Dependencies:
JupyterLab                        Extension      Package
>=2.2.1 <2.3.0                    >=1.3.0 <2.0.0 @jupyterlab/rendermime-interfaces
See the log file for details:  /tmp/jupyterlab-debug-a3i3t9j4.log

>>> TL;DR

按照官方建议操作(确保您的内核已关闭,否则您需要运行jupyter lab build):

jupyter labextension install jupyterlab-plotly

如果你得到了像这样的东西:
ValueError: Please install Node.js and npm before continuing installation.

首先需要安装nodejs(感谢@Travis):

conda install -c conda-forge nodejs

完成。

如果您想确保它已经工作:

jupyter labextension list

可能会给你类似这样的东西:
JupyterLab v2.2.9
Known labextensions:
   app dir: /home/user/anaconda3/envs/your-py-env/share/jupyter/lab
        jupyterlab-dash v0.3.0  enabled  OK
        jupyterlab-plotly v4.14.1  enabled  OK


1
一切都很好,那是我的错。我会删除我的评论。 - Michael Belvedersky
1
那个解决方案对我有效!Jupyter lab build需要nodejs >= 10.0.0,因此我们应该确保在我们的环境中安装了nodejs - Travis

4

针对Jupyter Lab的扩展安装方式已经在版本 > 3.0中有所改变。

如何在Jupyter Lab中使用plotly,请查看官方文档中需要安装的软件包。


如果我之前看到这个,它本来可以省下我两个小时的时间...最佳解决方案。 - SilentCloud

0

我也遇到了同样的问题,但我的解决方案略有不同。

我正在使用jupyterhub,我的虚拟环境是jupyter内核,因此使用上述步骤没有帮助。

我必须:

  1. 登录到我的jupyterhub docker容器中 docker exec -it jupyterhub bash
  2. 确保正确的软件包已安装 pip install "jupyterlab>=3" "ipywidgets>=7.6"
  3. 安装plotly扩展 jupyter labextension install jupyterlab-plotly
  4. 构建jupyter jupyter lab build

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接