Ava Fasciano
Ava Fasciano

Reputation: 11

matplotlib pyplot error in jupyter notebook

Within Jupyter notebook I can import matplotlib but not the pyplot module:

import matplotlib  % works
import matplotlib.pyplot as plt  % The kernel appears to have died.  It will restart automatically.

An alternative test also fails:

import matplotlib
matplotlib.pyplot    % AttributeError: module 'matplotlib' has no attribute 'pyplot'

However, I can import the pyplot module from the conda command prompt with no errors:

import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()

I got the same results in both the "base" environment and a virtual environment that I created.

Does anyone know what the problem is? I've tried uninstalling and reinstalling the maplotlib package, as well as Jupyter notebook, and conda itself.

Upvotes: 1

Views: 3627

Answers (3)

Shoval Sadde
Shoval Sadde

Reputation: 1202

Not that I understand what the problem was, but what worked for me is uninstalling matplotlib with conda, and re-installing it with pip. Oftentimes, the conda versions are not the latest, and there is probably a problem with the specific version. As of matplotlib 3.6.1 pyplot does work in jupyter (I tried with base), though reinstallation of kiwisolver might be needed as well.

Upvotes: 0

muhammad saad
muhammad saad

Reputation: 94

import matplotlib.pyplot as plt % The kernel appears to have died. It will restart automatically. This means you have to restart your juypter there will be a cmd window that you could have closed I recommend you to restart the juypter and then

import matplotlib.pyplot as plt

if it does not work open juypter cmd from search bar and write pip install matplotlib after the task is done in jupyter cmd reopen juypter and than use import matplotlib.pyplot as plt

Upvotes: 0

Anurag Dhadse
Anurag Dhadse

Reputation: 1873

This imports matplotlib module/library and then you are trying looks for an attribute or variable defined in matplotlib library named as pyplot; which does not exist. pyplot is just an interface for you to call other relevant interactive state-based functions to plot.

import matplotlib
matplotlib.pyplot  

In my opinion you should stick to naming/importing convention defined in documentations of libraries for coherent code.

import matplotlib.pyplot as plt

This is all you need.

Upvotes: 3

Related Questions