votes up 4

matplotlib is required for plotting when the default backend "matplotlib" is selected.

Package:
pandas
github stars 30911
Exception Class:
ImportError

Raise code

end == "matplotlib":
        # Because matplotlib is an optional dependency and first-party backend,
        # we need to attempt an import here to raise an ImportError if needed.
        try:
            import pandas.plotting._matplotlib as module
        except ImportError:
            raise ImportError(
                "matplotlib is required for plotting when the "
                'default backend "matplotlib" is selected.'
            ) from None

        _backends["matplotlib"] = module

    if backend in _backends:
        retu
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

Ways to fix

votes up 3 votes down

As a default pandas uses matplotlib but doesn't hard require it in order to run. Therefore, matplotlib has to be installed in order for pandas to use it. so the fix is to pip install it.

Code to reproduce (WRONG):

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

how to fix:

pip uninstall pandas
pip install matplotlib
pip install pandas

or, using pip3:

pip3 uninstall pandas
pip3 install matplotlib
pip3 install pandas

Working version(fixed)

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

and you should no longer have this exception raised.

Jun 22, 2021 huffnagledean answer

Add a possible fix

Please authorize to post fix