jupyterlab interactive plot

江枫思渺然 提交于 2019-11-27 00:44:50

问题


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

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):

JavaScript output is disabled in JupyterLab

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

%matplotlib ipympl

But that just returns:

FigureCanvasNbAgg()

Inline plots:

%matplotlib inline

work just fine, but I want interactive plots.


回答1:


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.

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

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



回答2:


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

%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




回答3:


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



来源:https://stackoverflow.com/questions/50149562/jupyterlab-interactive-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!