Pandas ImportError: matplotlib is required for plotting

Question:

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:usersxxxxxappdatalocalprogramspythonpython36libsite-packagespandasplotting_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.
Asked By: Nishat21

||

Answers:

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.

Answered By: Steffi Keran Rani J

Installing matplotlib before installing pandas again made it work.

Answered By: Nishat21

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.

Answered By: Billy Jhon

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!

Answered By: themefield

Just install matplotlib by running this in your terminal:

pip install matplotlib

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.

Answered By: MrE
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.