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
jupyterlab interactive plot
提问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
完成步骤
- Install
nodejs
, e.g.conda install nodejs
. - Install
ipympl
, e.g.pip install ipympl
. - [Optional, but recommended; update JupyterLab, e.g.
pip install --upgrade jupyterlab
.] - [Optional, but recommended; for a local user installation, run:
export JUPYTERLAB_DIR="$HOME/.local/share/jupyter/lab"
.] Install extensions:
jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter labextension install jupyter-matplotlib
Enable widgets:
jupyter nbextension enable --py widgetsnbextension
.- Restart JupyterLab.
- Decorate with
%matplotlib widget
.
- 安装
nodejs
,例如conda install nodejs
。 - 安装
ipympl
,例如pip install ipympl
。 - [可选,但推荐;更新 JupyterLab,例如
pip install --upgrade jupyterlab
。] - [可选,但推荐;对于本地用户安装,请运行:
export JUPYTERLAB_DIR="$HOME/.local/share/jupyter/lab"
.] 安装扩展:
jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter labextension install jupyter-matplotlib
启用小部件:
jupyter nbextension enable --py widgetsnbextension
.- 重启 JupyterLab。
- 用 装饰
%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