ModuleNotFoundError: No module named 'matplotlib.pyplot'
Asked Answered
S

8

8

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:enter image description here

I wish someone can help me solve this problem

Sidon answered 24/5, 2017 at 4:40 Comment(3)
Are you sure that you have the matplotlib module in your Jupyter Notebook environment?Poolroom
@Poolroom 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.Sidon
voting to close. you mispelled pyplot as pyplotyUnruffled
L
16

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
Landlocked answered 7/1, 2019 at 23:17 Comment(3)
where I should add these lines?Tsuda
@AniceJahanjoo open a new cell anywhere in your notebook and put this code to execute. You can delete the cell afterwardsLandlocked
may I ask where this was installed? It worked, but just curious as to why. My virtualenv was already installed with matplotlib but wasn't recognizing it at first.Bron
B
8

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!

Brahear answered 26/4, 2018 at 16:23 Comment(3)
This is the precise answer for the problem and the easiest because you do it from your notebook. Dont need to get the terminalDocent
It should be %pip install matplotlib these days. The magic command was added to insure the installation occurs in the environment backing the kernel underlying the notebook. Use of the exclamation point alone doesn't insure that. See here about the modern %pip install magic command (and related %conda install magic command for those using conda for package management.)Rojo
<continued> The exclamation alone doesn't do that, as explained at the top here.Rojo
N
6

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:

enter image description here

You may decide to use %matplotlib inline in which case you don't have to call plt.show().

enter image description here

You may also use %matplotlib notebook, which gives you an interactive plot.

enter image description here

Finally, you may use %matplotlib tk to get a windowed figure like you would in PyCharm.

enter image description here

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).

Novick answered 24/5, 2017 at 7:14 Comment(4)
I updated my question, can you take a look and tell me where is wrong. thxSidon
I updated the answer with an image. Is this clear enough?Novick
Hi 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.Sidon
@Noob. did you install matplotlib with conda install matplotlib?Unruffled
T
3

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

Tenderfoot answered 30/9, 2018 at 7:2 Comment(0)
F
3

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
Fluorescein answered 8/9, 2019 at 9:56 Comment(0)
M
2

add %matplotlib inline on top of your codes,it makes matplotlib execute in interactive way

Mushroom answered 24/5, 2017 at 4:43 Comment(2)
hi 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 write import matplotlib.pyplot as plt? I tried in Pycharm to use import matplotlib as plt but it wont workSidon
Jupiter 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 interactivelyMushroom
S
1

This code worked for me

%pip install matplotlib-inline
Schinica answered 7/8, 2021 at 17:13 Comment(0)
B
0
pip install matplotlib-inline

works for me after trying other lots of methods. Matplotlib-inline can be thought as a single module.

Backwash answered 30/8, 2024 at 3:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.