ModuleNotFoundError: No module named 'matplotlib.pyplot'
Solution 1
I had the same problem and found a solution! Matplotlib was installed on another python installation I have.
Put the following snippet in a cell and execute it, and you should be good to go:
import sys
!{sys.executable} -m pip install matplotlib
Solution 2
This is an indication that matplotlib lib/module is not installed. So all you have to do is install this module by running the code below in the cell previous to referring matplotlib:
!pip install matplotlib
Hope it helps!
Solution 3
You don't need to use %matplotlib inline
as other answers here suggest. This is optional and not using it should not prevent importing pyplot.
What should work is the following:
You may decide to use %matplotlib inline
in which case you don't have to call plt.show()
.
You may also use %matplotlib notebook
, which gives you an interactive plot.
Finally, you may use %matplotlib tk
to get a windowed figure like you would in PyCharm.
All of those options require to have imported matplotlib.pyplot
. Importing matplotlib
alone is not helpful. Also, if you experience any problems, start a new kernel first (don't try something new in line 27 of your notebook).
Solution 4
if you are using Anaconda CMD the use this command,
conda install matplotlib
If you are using Normal CMD then use command,
pip install matplotlib
or
pip3 install matplotlib
Solution 5
if you are using jupyter notebook in anaconda, matplotlib should be installed to the environment.
go to Environments -> the environment you are using -> change the droplist to not installed -> search matplotlib, and install
Comments
-
Psyduck over 2 years
When making a plot, I used both Jupyter Notebook and Pycharm with the same set of code and packages. The code is:
import pandas as pd import numpy as np import matplotlib.pyplot as plt # as in Pycharm import matplotlib as plt # as in Jupyter df = pd.read_csv("/home/kunal/Downloads/Loan_Prediction/train.csv") df['ApplicantIncome'].hist(bins=50) plt.show() #this only in Pycharm not in Jupyter.
In Pycharm, the code works well. But in Jupyter Notebook, it has error:
I wish someone can help me solve this problem
-
arnold almost 7 yearsAre you sure that you have the
matplotlib
module in yourJupyter Notebook
environment? -
Psyduck almost 7 years@arnold I am pretty sure I did, cuz the modules I used for both Jupyter and Pycharm are at the same location. And the same code works in Pycharm so I don't think there's anything wrong with Module.
-
Paul H almost 7 yearsvoting to close. you mispelled
pyplot
aspyploty
-
-
Psyduck almost 7 yearshi man, I followed your suggestion and add the line on the very top cell, and it works. I am wondering what's the difference here that I can do
import matplotlib as plt
while in Pycharm I have to writeimport matplotlib.pyplot as plt
? I tried in Pycharm to useimport matplotlib as plt
but it wont work -
Eliethesaiyan almost 7 yearsJupiter works in interactive way,same as when you type in terminal,python,or ipython,it keeps the python shell waiting for the another command ,the key part of the that part is the sign %,this is called magic function..it allows you access other commands or other python scripts and leaves the results available in the shell.for example,if you have a script tmy.py that has some variable defined,you can run it using %python my.py ,if there is any variable defined in that file,you can access it in jupiter,or i haven't used pycharm but i suppose that you are not running your program interactively
-
Psyduck almost 7 yearsI updated my question, can you take a look and tell me where is wrong. thx
-
ImportanceOfBeingErnest almost 7 yearsI updated the answer with an image. Is this clear enough?
-
Psyduck almost 7 yearsHi I am sorry for the typo, but with the correct spelling the same error remains as in the original image in the question. I realized I had matplotlib package in both my miniconda\site-packages and the default python36\site-packages folder, I tried to uninstall the one in the python36\site-packages and the ModuleNotFound error disappears. Thanks.
-
Paul H almost 7 years@Noob. did you install matplotlib with
conda install matplotlib
? -
A Neto almost 4 yearsThis is the precise answer for the problem and the easiest because you do it from your notebook. Dont need to get the terminal
-
Anice Jahanjoo over 3 yearswhere I should add these lines?
-
Victor Oliveira over 3 years@AniceJahanjoo open a new cell anywhere in your notebook and put this code to execute. You can delete the cell afterwards