178

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.

2
  • 4
    Installing the jupyter-matplotlib extension is the path forward, as described inTomNorway's answer, which I think should become the accepted answer. For a quick workaround, do %matplotlib inline. Commented Aug 2, 2019 at 15:27
  • For anyone working with jupyterlab 3, please check this stackoverflow.com/a/68496287 Commented Jan 31, 2023 at 3:45

9 Answers 9

144

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
Sign up to request clarification or add additional context in comments.

12 Comments

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
Thank you, i've been using jupyterlab v0.35, now everything works great with v1.0.2
anyone else who gets the "npm dependencies failed to install" in ubuntu with jupyterlab 1.1.1 ?
And also enable that extension with jupyter nbextension enable --py widgetsnbextension. And restart whole notebook/lab
It seems that nodejs is no longer needed to be installed manually.
|
96

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

4 Comments

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 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!
This has not worked for me in jupyter, jupyter lab, and now jupyter hub
Not work jupyterlab.
This works on Jupyter Lab I was using the notebook backend, changing it to inline worked. This was after installing ipympl and getting the same error.
11

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

cerebro.plot(iplot = False)

1 Comment

This works finally. %matplotlib inline; cerebro.plot(iplot = False)
10

I could solve the same problem by installing ipympl:

pip install ipympl

And then add %matplotlib ipympl before plot.

1 Comment

In jupyterlab, this results in "Loading widget..." text and no plot
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.

3 Comments

Prints "Loading widget...", no plot
This is a bit outdated. The current, specific package and syntax for modern Jupyter Notebook 7+ and current JupyterLab is ipympl and %matplotlib ipympl. (Note, ipympl respects the legacy %matplotlib widget but it is better to be explicit.) ipympl is an install doing a lot of things. It is best to turn everything off and restart everything after installing it. The machine and all involved programs. Plus do a hard refresh on any page involving use of it, until things are totally established on your system. See the basic example here ...
<continued> for more details and code you can test it with. (Note that this is along the lines of what Kevian wrote in this thread earlier, but lacked some of the context.)
1

just remove %matplotlib notebook because in Jupyter notebook this line give error. when i run some code i get same error "Javascript Error: IPython is not defined in JupyterLab" but when i just see one things in my code i write %matplotlib notebook that is use in colab notebook not in jupyter notebook

1 Comment

It would probably be best not to confuse things in this old thread by citing outdated approaches & without citing versions. As myself and others have pointed out in this thread, installing and using ipympl is the current way with modern JupyterLab (late 3 and 4 forward) and Jupyter Notebook 7+. Colab is outdated and somewhat patched Jupyter and often confuses things. The original post was clearly about the true Jupyter ecosystem.
-1

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

Comments

-1

Install Anaconda and start notebook from anaconda navigator. It resolves my problem.(The solution above didn't work for me. )

1 Comment

This did not work for me and I always start from the Anaconda Navigator. I just started getting this error as well in a Notebook I've been using for weeks.
-1

After installing ipympl, then reinstalling jupyter-lab the way to get it to work was by importing like:

%matplotlib ipympl

1 Comment

I'm not seeing how your information differs from this one already posted here by Keivan? Also the current highest scoring one in this thread also already inlcudes this information, especially if you read the documentation where the basic Example says, "To activate the ipympl backend all you need to do is include the %matplotlib ipympl magic in the notebook. Alternatively you can use %matplotlib widget which will have the same effect.".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.