149

I have the latest/updated Anaconda package. Everytime I try to plot something using python 3.6.6 I get the following error in JupyterLab...

Javascript Error: IPython is not defined

When I run the same code in Spyder using an ipython kernel, it works just fine. Have been looking all over online but cant seem to figure out what is going on. Any help would be appreciated.

nick
  • 1,090
  • 1
  • 11
  • 24
AntMan
  • 1,493
  • 2
  • 8
  • 5
  • 2
    Installing the [jupyter-matplotlib extension](https://github.com/matplotlib/jupyter-matplotlib) is the path forward, as described inTomNorway's answer, which I think should become the accepted answer. For a quick workaround, do `%matplotlib inline`. – nealmcb Aug 02 '19 at 15:27
  • For anyone working with jupyterlab 3, please check this https://stackoverflow.com/a/68496287 – Lynd Jan 31 '23 at 03:45

6 Answers6

126

Jupyter Lab does support interactive matplotlib through the jupyter-matplotlib extension. The installation procedure is slightly more involved, but works fine. Since the ipympl Jupyter Lab version requires NodeJS, and NodeJS requires Windows 8.1, ipympl also has this requirement.

As before, it is important to invoke the iPython magic command before plotting:

Usage:

%matplotlib widget

Installation:

Note: If using this extension with Jupyter Lab, it's recommended to use a version >= 3. For more detailed instructions on installing an old extension than below, see the instructions on ipympl github.

Using conda

conda install -c conda-forge ipympl

# If using JupyterLab 2
conda install nodejs
jupyter labextension install @jupyter-widgets/jupyterlab-manager
jupyter lab build

# Later, if updating a previous Lab install:
conda install ipympl
jupyter lab build

Using pip

pip install ipympl

# If using JupyterLab 2
pip install nodejs-bin
jupyter labextension install @jupyter-widgets/jupyterlab-manager
jupyter labextension install jupyter-matplotlib
ankostis
  • 8,579
  • 3
  • 47
  • 61
TomNorway
  • 2,584
  • 1
  • 19
  • 26
  • 2
    On my machine it prints `Error displaying widget` and doesn't show the graph. `jupyter labextension list` command prints: `@jupyter-widgets/jupyterlab-manager v0.38.1 enabled OK jupyter-matplotlib v0.3.0 enabled OK` – Khan Jul 03 '19 at 10:19
  • You may need to update to jupyter lab v 1.0, which has been released in the past few days. jupyter-matplotlib v0.3.1 was also just released today. Try installing those, running the above jupyter labextension commands, and report back. – TomNorway Jul 04 '19 at 15:57
  • 1
    Thank you, i've been using jupyterlab `v0.35`, now everything works great with `v1.0.2` – Khan Jul 10 '19 at 12:05
  • 1
    anyone else who gets the "npm dependencies failed to install" in ubuntu with jupyterlab 1.1.1 ? – KFkf Apr 02 '20 at 09:35
  • There were issues once ipympl added support for jupyterlab 1.2. I suggest you either update jupyterlab or install the older ipympl version 3.3 (not sure if any later versions will work). `conda install ipympl=0.3.3` or `pip install ipympl==0.3.3` – TomNorway Apr 03 '20 at 09:25
  • 3
    And also enable that extension with `jupyter nbextension enable --py widgetsnbextension`. And restart whole notebook/lab – Appaji Chintimi Jul 28 '20 at 12:52
  • Update from the newest ipyml version (https://github.com/matplotlib/ipympl): Starting from ipympl `0.5.6`, you do not need to manually install the JupyterLab extension, but you still need to install the JupyterLab widget manager. `jupyter labextension install @jupyter-widgets/jupyterlab-manager` `# If you already installed the @jupyter-widgets/jupyterlab-manager extension, you will still need to rebuild JupyterLab after you installed ipympl` `jupyter lab build` – Axel Sep 12 '20 at 16:35
  • 1
    It seems that nodejs is no longer needed to be installed manually. – racoon_lord Feb 25 '21 at 12:14
  • Just a heads-up for users of JupyterLab 3.0.12 (to the least): all of a sudden, `%matplotlib widget` stopped working, with the above error. I had to manually uninstall (`jupyter labextension uninstall`, `conda uninstall`…) and then reinstall all the packages as per TomNorway’s answer. – Marco B Apr 02 '21 at 17:42
  • Using current conda version (v4.10, 06/2021) with a python 3.9.5 environment, `conda install nodejs` installs nodejs v6.X but `jupyter labextension install [...]` requires nodejs >=v12.0. Workaround is to install nodejs with `conda install nodejs -c conda-forge --repodata-fn=repodata.json`. [Source](https://stackoverflow.com/questions/62325068/cannot-install-latest-nodejs-using-conda-on-mac) – saper0 Jun 01 '21 at 12:03
80

Jupyter Lab does support interactive matplotlib through the jupyter-matplotlib extension. See TomNorway's answer.

Alternatively, you can configure matplotlib to use inline instead. inline is not as powerful as interactive, but it can save you.

To use inline, add this line before plot the graph:

    %matplotlib inline

More Info

Thomas
  • 174,939
  • 50
  • 355
  • 478
palhares
  • 1,663
  • 17
  • 13
  • 6
    Note that the quote and link are from a different project:`nteract`, not Jupyter Lab. And this is a workaround. It appears that the [jupyter-matplotlib extension](https://github.com/matplotlib/jupyter-matplotlib) is the path forward, as described in a separate answer, which I think should become the accepted answer But thanks - it is indeed a quick, helpful fix! – nealmcb Aug 02 '19 at 15:25
  • 2
    This has not worked for me in jupyter, jupyter lab, and now jupyter hub – Kermit Sep 19 '19 at 13:56
  • 1
    Not work jupyterlab. – huang Oct 17 '21 at 13:31
6
  1. Jupyterlab supports jpympl.

  2. You must put %matplotlib widget in the very beginning of the jupyterlab.

You can change to %matplotlib inline in specific cell, and active %matplotlib widget again if needed.

Otherwise, no matter how many times you reinstall the package, you will still get the errors.

Ulysses
  • 141
  • 2
  • 2
3

I could solve the same problem by installing ipympl:

pip install ipympl

And then add %matplotlib ipympl before plot.

Keivan
  • 1,300
  • 1
  • 16
  • 29
2

Encountered similar issue when using backtrader. This fixed it for me:

cerebro.plot(iplot = False)
0

I was getting Ipython not defined in jupyter notebook when I tried to display a html formated content in my jupyter notebook, I just imported the function and it worked

from IPython.core.display import display, HTML # my imports

annot = coco_dataset.display_image(21, use_url=False) #my function return a html page

HTML(annot) # used for displaying the page
burhan rashid
  • 444
  • 5
  • 11