matplotlib is required for plotting when the default backend "matplotlib" is selected.
Package:
pandas
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
Links to the raise (1)
https://github.com/pandas-dev/pandas/blob/b3e335254f46a526ee3ce9bb757eac4011d9d1fe/pandas/plotting/_core.py#L1802Ways to fix
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.
Add a possible fix
Please authorize to post fix