0

I start with the usual imports and go to start an interactive plotting session:

# for viewing figures interactively
%matplotlib notebook
 
# To start plotting in matplotlib
import IPython
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("whitegrid")

# Other data libraries
import numpy as np
import pandas as pd

# 3 dimensional plotting
from mpl_toolkits import mplot3d

# enable three-dimensional axes
fig = plt.figure()
ax = plt.axes(projection='3d')

this results in the error:

Javascript Error: IPython is not defined

Then I read here to test some other things. I install IPython Widgets:

pip install ipywidgets 
jupyter nbextension enable --py widgetsnbextension

Now putting %matplotlib widgets instead of %matplotlib notebook at the beginning of the codes displays the interactive figure.

In another approach, I can go and install ipympl and enable that extension:

pip install ipympl
jupyter nbextension enable --py --sys-prefix ipympl

After that useing %matplotlib ipympl instead of %matplotlib notebook at the first line shows the figure interactively. But, the error for notebook magic command still persists. The thing is, this gets around the error and makes the code work but it does not solve the problem. I still have no clue why JavaScript cannot recognize IPyhton.

Any ideas for really solving the issue?

7
  • Perhaps this older question will help. Commented Dec 30, 2024 at 14:14
  • thanks @Pointy I read that and did try the solutions. my question here is how to make notebook command itself work, not the alternative solutions. Commented Dec 30, 2024 at 14:19
  • @rehaqds do you mean %matplotlib notebook? Commented Dec 30, 2024 at 14:20
  • 1
    github.com/jupyter/notebook/issues/7115 Commented Dec 30, 2024 at 14:27
  • 3
    @MaliheMahdavisefat Please note versions of software in your post because if you need to use %matplotlib notebook you would need to use NbClassic. See here about that and for more about the issue you have hit, see here and here and here. Commented Dec 30, 2024 at 17:26

1 Answer 1

0

So, the answer is even though matplotlib.org did not officially abandon %matplotlib notebook, they recommend to use %matplotlib ipympl so it is clear that ipympl needs to be installed. ipympl respects %matplotlib widget for better backend compatibility and performance.

jupyter notebook version: 7.2.2 matplotlib version: 3.10.0 seabor version: 0.13.2

Sign up to request clarification or add additional context in comments.

2 Comments

Please update your answer to be consistent with the current conventions. It is %matplotlib ipympl so that it is clear that ipympl needs to be installed. ipympl respects %matplotlin widgetto provide legacy support, but it is better to be explicit about things. See this fact mention under 'Basic Example' in the ipympl documentation, as "Alternatively you can use %matplotlib widget which will have the same effect."
This 'solution' should note versions of the specific Jupyter software as it is unclear and potentially confusing without that. (See the comments below the original post.)

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.