ImportError: cannot import name 'rcParams' from 'matplotlib'

I have tried to uninstall and reinstall matplotlib in my tf-gpu enviornment I made but I keep getting this error:

ImportError: cannot import name 'rcParams' from 'matplotlib'

This is the entire output I am getting on jupyter notebook:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-9-8d7d44d0fddd> in <module>
     17 # import matplotlib.pyplot as plt
     18 from sklearn.metrics import confusion_matrix
---> 19 from mlxtend.plotting import plot_confusion_matrix
     20 
     21 # Removes annoying warning messages in tensorflow and python

~\AppData\Local\Continuum\anaconda3\envs\tf-gpu\lib\site-packages\mlxtend\plotting\__init__.py in <module>
      5 # License: BSD 3 clause
      6 
----> 7 from .decision_regions import plot_decision_regions
      8 from .learning_curves import plot_learning_curves
      9 from .plot_confusion_matrix import plot_confusion_matrix

~\AppData\Local\Continuum\anaconda3\envs\tf-gpu\lib\site-packages\mlxtend\plotting\decision_regions.py in <module>
      8 
      9 from itertools import cycle
---> 10 import matplotlib.pyplot as plt
     11 import numpy as np
     12 from mlxtend.utils import check_Xy, format_kwarg_dictionaries

~\AppData\Local\Continuum\anaconda3\envs\tf-gpu\lib\site-packages\matplotlib\pyplot.py in <module>
     30 from cycler import cycler
     31 import matplotlib
---> 32 import matplotlib.colorbar
     33 import matplotlib.image
     34 from matplotlib import rcsetup, style

~\AppData\Local\Continuum\anaconda3\envs\tf-gpu\lib\site-packages\matplotlib\colorbar.py in <module>
     25 
     26 import matplotlib as mpl
---> 27 import matplotlib.artist as martist
     28 import matplotlib.cbook as cbook
     29 import matplotlib.collections as collections

~\AppData\Local\Continuum\anaconda3\envs\tf-gpu\lib\site-packages\matplotlib\artist.py in <module>
     10 
     11 import matplotlib
---> 12 from . import cbook, docstring, rcParams
     13 from .path import Path
     14 from .transforms import (Bbox, IdentityTransform, Transform, TransformedBbox,

ImportError: cannot import name 'rcParams' from 'matplotlib' (unknown location)

I was not able to find a solution online, so I wanted to start a thread on this again.

UPDATE:

I tried to uninstall matplotlib using conda/pip but it still shows up in conda list output:

# Name                    Version                   Build  Channel
_tflow_select             2.1.0                       gpu
absl-py                   0.8.1                    py37_0
asn1crypto                1.0.0                    pypi_0    pypi
astor                     0.8.0                    py37_0
attrs                     19.3.0                     py_0
backcall                  0.1.0                    py37_0
blas                      1.0                         mkl
bleach                    3.1.0                    py37_0
bzip2                     1.0.8                he774522_0
ca-certificates           2019.11.27                    0
censusdata                1.5                      pypi_0    pypi
censusgeocode             0.4.3.post1              pypi_0    pypi
certifi                   2019.11.28               py37_0
cffi                      1.12.3                   pypi_0    pypi
chardet                   3.0.4                    pypi_0    pypi
click                     7.0                      py37_0
click-plugins             1.1.1                      py_0
cligj                     0.5.0                    py37_0
colorama                  0.4.3                      py_0
cryptography              2.7                      pypi_0    pypi
cudatoolkit               10.0.130                      0
cudnn                     7.6.5                cuda10.0_0
curl                      7.67.0               h2a8f88b_0
decorator                 4.4.1                      py_0
defusedxml                0.6.0                      py_0
entrypoints               0.3                      py37_0
et-xmlfile                1.0.1                    pypi_0    pypi
expat                     2.2.5                he025d50_0
fiona                     1.8.4            py37h22081e2_0
freexl                    1.0.5                hfa6e2cd_0
gast                      0.3.2                      py_0
gdal                      2.3.3            py37hdf43c64_0
geopandas                 0.6.1                      py_0
geos                      3.7.1                h33f27b4_0
grpcio                    1.16.1           py37h351948d_1
h5py                      2.9.0            py37h5e291fa_0
haversine                 2.1.2                    pypi_0    pypi
hdf4                      4.2.13               h712560f_2
hdf5                      1.10.4               h7ebc959_0
icc_rt                    2019.0.0             h0cc432a_1
icu                       58.2                 ha66f8fd_1
idna                      2.8                      pypi_0    pypi
importlib_metadata        1.3.0                    py37_0
intel-openmp              2019.4                      245
ipfn                      1.3.0                    pypi_0    pypi
ipykernel                 5.1.3            py37h39e3cac_0
ipython                   7.11.1           py37h39e3cac_0
ipython_genutils          0.2.0                    py37_0
ipywidgets                7.5.1                      py_0
jdcal                     1.4.1                    pypi_0    pypi
jedi                      0.15.2                   py37_0
jinja2                    2.10.3                     py_0
joblib                    0.14.0                   pypi_0    pypi
jpeg                      9b                   hb83a4c4_2
jsonschema                3.2.0                    py37_0
jupyter                   1.0.0                    py37_7
jupyter_client            5.3.4                    py37_0
jupyter_console           6.1.0                      py_0
jupyter_core              4.6.1                    py37_0
kealib                    1.4.7                h07cbb95_6
keras                     2.2.4                         0
keras-applications        1.0.8                      py_0
keras-base                2.2.4                    py37_0
keras-preprocessing       1.1.0                      py_1
krb5                      1.16.4               hc04afaa_0
libboost                  1.67.0               hd9e427e_4
libcurl                   7.67.0               h2a8f88b_0
libgdal                   2.3.3                h10f50ba_0
libiconv                  1.15                 h1df5818_7
libkml                    1.3.0                he5f2a48_4
libnetcdf                 4.6.1                h411e497_2
libpng                    1.6.37               h2a8f88b_0
libpq                     11.2                 h3235a2c_0
libprotobuf               3.11.2               h7bd577a_0
libsodium                 1.0.16               h9d3ae62_0
libspatialindex           1.9.3                h33f27b4_0
libspatialite             4.3.0a              hc36aec2_19
libssh2                   1.8.2                h7a1dbc1_0
libtiff                   4.1.0                h56a325e_0
libxml2                   2.9.9                h464c3ec_0
m2w64-gcc-libgfortran     5.3.0                         6
m2w64-gcc-libs            5.3.0                         7
m2w64-gcc-libs-core       5.3.0                         7
m2w64-gmp                 6.1.0                         2
m2w64-libwinpthread-git   5.0.0.4634.697f757               2
markdown                  3.1.1                    py37_0
markupsafe                1.1.1            py37he774522_0
matplotlib                3.1.1                    pypi_0    pypi
mistune                   0.8.4            py37he774522_0
mkl                       2019.4                      245
mkl-service               2.3.0            py37hb782905_0
mkl_fft                   1.0.15           py37h14836fe_0
mkl_random                1.1.0            py37h675688f_0
mlxtend                   0.17.0                   pypi_0    pypi
more-itertools            8.0.2                      py_0
msys2-conda-epoch         20160418                      1
munch                     2.5.0                      py_0
nbconvert                 5.6.1                    py37_0
nbformat                  4.4.0                    py37_0
networkx                  2.4                      pypi_0    pypi
notebook                  6.0.2                    py37_0
numpy                     1.18.1           py37h93ca92e_0
numpy-base                1.18.1           py37hc3f5095_1
openpyxl                  3.0.2                    pypi_0    pypi
openssl                   1.1.1d               he774522_3
pandas                    0.25.3           py37ha925a31_0
pandoc                    2.2.3.2                       0
pandocfilters             1.4.2                    py37_1
parso                     0.5.2                      py_0
patsy                     0.5.1                    py37_0
pcre                      8.43                 ha925a31_0
pickleshare               0.7.5                    py37_0
pip                       19.3.1                   py37_0
plotly                    4.1.1                    pypi_0    pypi
proj4                     5.2.0                ha925a31_1
prometheus_client         0.7.1                      py_0
prompt_toolkit            3.0.2                      py_0
protobuf                  3.11.2           py37h33f27b4_0
pycparser                 2.19                     pypi_0    pypi
pygments                  2.5.2                      py_0
pyopenssl                 19.0.0                   pypi_0    pypi
pyproj                    1.9.6            py37h6782396_0
pyqt                      5.9.2            py37h6538335_2
pyreadline                2.1                      py37_1
pyrsistent                0.15.6           py37he774522_0
python                    3.7.6                h60c2a47_2
python-dateutil           2.8.1                      py_0
pytz                      2019.3                     py_0
pywin32                   227              py37he774522_1
pywinpty                  0.5.7                    py37_0
pyyaml                    5.2              py37he774522_0
pyzmq                     18.1.0           py37ha925a31_0
qt                        5.9.7            vc14h73c81de_0
qtconsole                 4.6.0                      py_1
requests                  2.22.0                   pypi_0    pypi
requests-toolbelt         0.9.1                    pypi_0    pypi
retrying                  1.3.3                    pypi_0    pypi
rtree                     0.8.3                    py37_0
scipy                     1.3.2            py37h29ff71c_0
seaborn                   0.9.0                    pypi_0    pypi
send2trash                1.5.0                    py37_0
setuptools                44.0.0                   py37_0
shapely                   1.6.4            py37h222a598_0
sip                       4.19.8           py37h6538335_0
six                       1.13.0                   py37_0
sklearn                   0.0                      pypi_0    pypi
sqlite                    3.30.1               he774522_0
statsmodels               0.10.1           py37h8c2d366_0
tensorboard               1.14.0           py37he3c9ec2_0
tensorflow                1.14.0          gpu_py37h5512b17_0
tensorflow-base           1.14.0          gpu_py37h55fc52a_0
tensorflow-estimator      1.14.0                     py_0
tensorflow-gpu            1.14.0               h0d30ee6_0
termcolor                 1.1.0                    py37_1
terminado                 0.8.3                    py37_0
testpath                  0.4.4                      py_0
time-series               0.2                      pypi_0    pypi
tk                        8.6.8                hfa6e2cd_0
tornado                   6.0.3            py37he774522_0
traitlets                 4.3.3                    py37_0
urllib3                   1.25.6                   pypi_0    pypi
vc                        14.1                 h0510ff6_4
vs2015_runtime            14.16.27012          hf0eaf9b_1
wcwidth                   0.1.7                    py37_0
webencodings              0.5.1                    py37_1
werkzeug                  0.16.0                     py_0
wheel                     0.33.6                   py37_0
widgetsnbextension        3.5.1                    py37_0
wincertstore              0.2                      py37_0
winpty                    0.4.3                         4
wrapt                     1.11.2           py37he774522_0
xerces-c                  3.2.2                ha925a31_0
xlrd                      1.2.0                    py37_0
xz                        5.2.4                h2fa13f4_4
yaml                      0.1.7                hc54c509_2
zeromq                    4.3.1                h33f27b4_3
zipp                      0.6.0                      py_0
zlib                      1.2.11               h62dcd97_3
zstd                      1.3.7                h508b16e_0

Solution 1:

Try the following:

from pylab import rcParams

Solution 2:

I came across this Error after reinstalling matplotlib twice after facing other Errors, which are:

ValueError: setting an array element with a sequence

ValueError: 'transform' must be an instance of 'matplotlib.transform.Transform'

I was able to solve the above issues by reinstalling matplotlib using

conda install -f matplotlib

But surprisingly I was able to solve this (ImportError: cannot import name 'rcParams' from 'matplotlib') just by restarting the Spyder(Python 3.7) from File Menu Restart option.

I restarted and the run the code, without any modifications code worked well.

Solution 3:

I'm not certain if this corresponds to a recent change in how matplotlib works or if it was always so, but when I encountered the same error, it seemed that the issue was that rcParams cannot be directly imported with an import statement. You must first import matplotlib, thus causing the module to be loaded into memory, cached, and possibly generate some dynamic objects. Then you can access rcParams.

Case in point, this fails:

import matplotlib.rcParams as rcp
Traceback (most recent call last):
  File "/home/nc/miniconda3/envs/pybnn/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3417, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-d4bca3da3c19>", line 1, in <module>
    import matplotlib.rcParams as rcp
  File "/snap/pycharm-community/211/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'matplotlib.rcParams'

However, this works:

import matplotlib as mpl
rcp = mpl.rcParams
Backend Qt5Agg is interactive backend. Turning interactive mode on.
WARNING: QApplication was not created in the main() thread.
type(rcp)
Out[5]: matplotlib.RcParams

I'm using matplotlib 3.3.1.