IPython not recognized when plotting with matplotlib notebook

Hello! I am currently viewing a python instructional videos series offered by my college and completing the respective “assignments” on jupyternotebook in preparation for the coming school year. However, I have suddenly encountered the issue depicted above. This method of plotting worked for several days, then suddenly produced this error last night. I am using the most current version of ipython, jupyternotebook, and matplotlib. Do you know what the issue might be, or any possible fixes? Thank you

The fix is straightforward, I think, if you are actually using the current version of Jupyter Notebook, which is 7+:

You likely need an installation here and to specify the correct backend for matplotlib to use, see here.

Install ipympl first, following here. If you are using Anaconda/conda, install with that. You can use %conda install -c conda-forge ipympl in a new cell in your notebook. Otherwise use pip. You can also use that in your notebook with %pip install ipympl.

Let it run completely to finish the install with whatever method you use. Importantly, that won’t fix everything yet because this installation is complex and you need to restart everything to propagate the changes to a new session. So shut down, Jupyter, your browser, and even your entire system and start everything back up.

Next start things back up. In the new Jupyter session with your notebook open, do the following before running your sympy plotting code:

Get rid of the matplotlib notebook cell entirely. (That was not explicit and the one that is causing an issue as it is not compatible with current Jupyter Notebook 7+ or JupyterLab.)

Replace that cell with a cell that says:

%matplotlib ipympl

That is based on here. (Note I am using the doubly- explicit %matplotlib ipympl command that specifies clearly it is magic command and matches with what you need to install for using the interactive features of matplotlib, which sympy.plotting uses; explicit is always better than not in Python.)

Now run your code and the plot should work and you should no longer see Javascript Error: IPython is not defined.


As for this part below:

“I am currently viewing a python instructional videos series offered by my college and completing the respective “assignments” on jupyternotebook in preparation for the coming school year. However, I have suddenly encountered the issue depicted above.”

Do you know how long ago these videos were made? Jupyter Notebook 7+ came out exactly a year ago this week (see the resources referenced in the middle of here for more about that, particularly Announcing Jupyter Notebook 7.

If they pre-date that, then they are likely not matching with current usage of Jupyter Noteboook 7+ since you say you are using the most current version of Jupyter Notebook 7+.

This is not consistent with much of what you’ve said prior. Either you were using current Jupyter Notebook 7+ and the code you show wouldn’t have worked. Or something got updated and now you are using the current Jupyter Notebook 7+.


In the future, always include code as code text in fenced block. Help those trying to help you. (More about this just below the code block.) For the record here is the code aside from the %matplotlib ipympl that would be placed in a cell at the top of the notebook:

from sympy import *
x=symbols('x')
f =(sqrt(x)*1)/(x**-1)
xval=[0.9, 0.99,0.999, 0.9999, 1.1, 1.01, 1.001, 1.0001]
yval=[f.subs(x,I) for i in xval]
plot(f,(x,0,5));

Please read Getting good answers to your questions. Pay particular attention to ’ DO NOT post images of code, data, error messages, etc’, under the section ‘Help others reproduce the problem’, in the link to How do I ask a good question? at the bottom there. In your case, because you are describing something along the lines of a behavior, a screenshot is justified as part of what is included. See here under ’ Block code formatting’ on how you would paste the code in to have it show with syntax and formatting on Discourse.

I believe it was updated, but it would be odd for it to be running an older version in the first place. The videos themselves are at least 3 years old, and I suspected that might be the issue. I will give this a try when I have the chance, thank you so much!

I did as you instructed, however, it now reads “error displaying widget”. I have shut everything down and restarted, installed/updated ipympl, etc. Do you know what might still be the issue? Thank you

First, did you fully clear the page and reload it when you tried again. In Chrome, I need to do Command + Shift + R or Shift + R. (I’m not at a machine where I can try it right now.) Browsers are set up to remember so many things that getting to a ‘fresh’ place after a complex install can be a challenge.
One idea is if you have more than one modern, updated browser installed on your machine is to try it in a browser you typically haven’t used with Jupyter.
I would try that first and if it still doesn’t work…

Well, I know from my earlier tests that the code I pasted in this thread works from sessions launched using the ‘launch binder’ badge here, after I installed sympy in the running session. The packages installed are listed here. Fortunately, it isn’t at all small list. However, since it mentions ‘widget’ in the error, have you tried installing and/or updating ipywidgets? You should do the whole shutdown everything and reload the browser page again as this is another of the installs that changes a lot of things at many levels.


If that doesn’t help, one thing I might suggest is installing the Anaconda Distribution if you haven’t already. This adds another level of project management you need to be concerned with going forward, but the base environment also comes with ipywidgets at least already installed and working. I think it may come with ipympl, too. You should be able to know by running %conda list in a new cell in a new notebook after installing the Distribution to see if ipympl is already there.