27

Pandas does not recognize installed matplotlib library

here is the code

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
ts.plot()

error is

c:\users\xxxxx\appdata\local\programs\python\python36\lib\site-packages\pandas\plotting\_core.py in _raise_if_no_mpl()
     55     # TODO(mpl_converter): remove once converter is explicit
     56     if not _HAS_MPL:
---> 57         raise ImportError("matplotlib is required for plotting.")
     58 
     59    
ImportError: matplotlib is required for plotting.
8

6 Answers 6

26

Installing matplotlib before installing pandas again made it work.

1
  • 4
    Jupyter notebook : Install matplotlib .Restart kernal. and import matplotlib before pandas. from matplotlib import pyplot as plt; %matplotlib inline
    – LMSharma
    Jun 28, 2019 at 10:42
17

I got the same error in Jupyter Lab. The solution is (after install of matplotlib):

Click Restart the Kernel button in the toolbar;

or

Choose menu item Kernel=>Restart Kernel and Run All Cells..., and click Restart in the confirmation dialog

Done!

11

You need to install matplotlib for that.

You can type and run the following command in your python shell to install matplotlib

python3 -mpip install matplotlib

If you are using Anaconda IDE, you can run the following command in the Conda command prompt

conda install matplotlib

Once installed, re-run the program.

Refer this page for installing matplotlib.

1
  • I do have matplotlib installed. It doesn't get recognized by pandas.
    – Nishat21
    Oct 8, 2018 at 12:01
10

All you had to do is quit Pandas after installing matplotlib and start it again. This way it would see fresh installation. No need to reinstall Pandas.

1
  • 2
    if you are using Jupyter Notebook you need to go the Running tab in the main console and then press the Shutdown button for the notebook that needs to use .plot. This step is required after installing matplotlib in the desired environment
    – bosch
    May 21, 2019 at 13:09
6

Just install matplotlib by running this in your terminal:

pip install matplotlib
0

What I found is that pandas does its own importing using importlib, and it hides issues that are not related to importing matplotlib.

Instead what is really likely happening is that you have missmatched versions of pandas and matplotlib. In my case I had an old Pandas 1.3.5 and matplotlib 3.5.1 and downgrading matplotlib to 3.3.0 did the trick.

So it's not about re-installing matplotlib as much as re-installing the proper version.

You can do the tesrt yourself, by running what the pandas code does

import importlib
importlib.load_module("pandas.plotting._matplotlib")

and see what kind of error pops up, which in turns is pivked up as an ImportError because it is in a try except block.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.