Home > Software design >  matplotlib pyplot error in jupyter notebook
matplotlib pyplot error in jupyter notebook

Time:02-19

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.

CodePudding user response:

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.

CodePudding user response:

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

  • Related