Python jupyterlab 交互式绘图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50149562/
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 19:24:24  来源:igfitidea点击:

jupyterlab interactive plot

pythonmatplotlibjupyter-lab

提问by Albatross

I'm getting into using Jupyterlab from Jupyter notebooks. In notebooks I used to use:

我正在从 Jupyter notebooks 开始使用 Jupyterlab。在我曾经使用的笔记本中:

import matplotlib.pyplot as plt
%matplotlib notebook
plt.figure()
x = [1,2,3]
y = [4,5,6]
plt.plot(x,y)

for interactive plots. Which now gives me (in jupyterlab):

用于交互式绘图。现在给了我(在 jupyterlab 中):

JavaScript output is disabled in JupyterLab

I have also tried the magic (with jupyter-matplotlibinstalled):

我也尝试了魔法(安装了jupyter-matplotlib):

%matplotlib ipympl

But that just returns:

但这只是返回:

FigureCanvasNbAgg()

Inline plots:

内联图:

%matplotlib inline

work just fine, but I want interactive plots.

工作得很好,但我想要交互式情节。

采纳答案by Albatross

As per Georgy's suggestion, this was caused by Node.js not being installed.

根据Georgy 的建议,这是由于未安装 Node.js 造成的。

回答by Mateen Ulhaq

Complete steps

完成步骤

  1. Install nodejs, e.g. conda install nodejs.
  2. Install ipympl, e.g. pip install ipympl.
  3. [Optional, but recommended; update JupyterLab, e.g.
    pip install --upgrade jupyterlab.]
  4. [Optional, but recommended; for a local user installation, run:
    export JUPYTERLAB_DIR="$HOME/.local/share/jupyter/lab".]
  5. Install extensions:

    jupyter labextension install @jupyter-widgets/jupyterlab-manager
    jupyter labextension install jupyter-matplotlib
    
  6. Enable widgets: jupyter nbextension enable --py widgetsnbextension.

  7. Restart JupyterLab.
  8. Decorate with %matplotlib widget.
  1. 安装nodejs,例如conda install nodejs
  2. 安装ipympl,例如pip install ipympl
  3. [可选,但推荐;更新 JupyterLab,例如
    pip install --upgrade jupyterlab。]
  4. [可选,但推荐;对于本地用户安装,请运行:
    export JUPYTERLAB_DIR="$HOME/.local/share/jupyter/lab".]
  5. 安装扩展:

    jupyter labextension install @jupyter-widgets/jupyterlab-manager
    jupyter labextension install jupyter-matplotlib
    
  6. 启用小部件:jupyter nbextension enable --py widgetsnbextension.

  7. 重启 JupyterLab。
  8. 用 装饰%matplotlib widget


Not recommended, but to blindly get the widget extension working in Anaconda, you can run the following in a terminal window:

不推荐,但要盲目地让小部件扩展在 Anaconda 中工作,您可以在终端窗口中运行以下命令:

conda install -y nodejs
pip install ipympl
pip install --upgrade jupyterlab
jupyter labextension install @jupyter-widgets/jupyterlab-manager
jupyter labextension install jupyter-matplotlib
jupyter nbextension enable --py widgetsnbextension

回答by John

To enable the jupyter-matplotlib backend, use the matplotlib Jupyter magic:

要启用 jupyter-matplotlib 后端,请使用 matplotlib Jupyter 魔法:

%matplotlib widget
import matplotlib.pyplot as plt
plt.figure()
x = [1,2,3]
y = [4,5,6]
plt.plot(x,y)

More info here jupyter-matplotlib on GitHub

更多信息请参阅 GitHub 上的 jupyter-matplotlib

Screenshot Jupyter Lab

截图 Jupyter 实验室

回答by Aray Karjauv

This solutionworks in jupyterlab

解决方案适用于 jupyterlab

import numpy as np
import matplotlib.pyplot as plt
from IPython.display import clear_output


n = 10
a = np.zeros((n, n))
plt.figure()

for i in range(n):
    plt.imshow(a)
    plt.show()
    a[i, i] = 1
    clear_output(wait=True)