ModuleNotFoundError: No module named 'matplotlib.pyplot'

32,334

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:

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

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

Share:
32,334
Psyduck
Author by

Psyduck

Headache...

Updated on August 09, 2021

Comments

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

    I wish someone can help me solve this problem

    • arnold
      arnold almost 7 years
      Are you sure that you have the matplotlib module in your Jupyter Notebook environment?
    • Psyduck
      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
      Paul H almost 7 years
      voting to close. you mispelled pyplot as pyploty
  • Psyduck
    Psyduck almost 7 years
    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 work
  • Eliethesaiyan
    Eliethesaiyan almost 7 years
    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 interactively
  • Psyduck
    Psyduck almost 7 years
    I updated my question, can you take a look and tell me where is wrong. thx
  • ImportanceOfBeingErnest
    ImportanceOfBeingErnest almost 7 years
    I updated the answer with an image. Is this clear enough?
  • Psyduck
    Psyduck almost 7 years
    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.
  • Paul H
    Paul H almost 7 years
    @Noob. did you install matplotlib with conda install matplotlib?
  • A Neto
    A Neto almost 4 years
    This 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
    Anice Jahanjoo over 3 years
    where I should add these lines?
  • Victor Oliveira
    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