'KeyError: 'scale' for import matplotlib.pyplot as plt

I'm getting KeyError: 'scale' when I'm trying to run import matplotlib.pyplot

I redownloaded the matplotlib then ran the code but still getting the same error.

I've also opened the new Jupyter notebook and only ran "import matplotlib.pyplot as plt" and I'm still getting the same error. I didn't get any error when I ran "import matplotlib" only.

I would be grateful if you help me out find what's causing the error.

import matplotlib.pyplot as plt
%matplotlib inline

city_pay_top50.plot(kind='bar', x='customer_city', y='payment_value')
plt.title('Cities with top 50 revenues')

plt.show()
KeyError                                  Traceback (most recent call last)
<ipython-input-6-07805b46bb32> in <module>
----> 1 import matplotlib.pyplot as plt
      2 #%matplotlib inline
      3 
      4 plt.bar(city_pay_top50['customer_city'], city_pay_top50['payment_value'])
      5 #city_pay_top50.plot(kind='bar', x='customer_city', y='payment_value')

~/opt/anaconda3/lib/python3.7/site-packages/matplotlib-3.2.1+1847.gfc8a2fa24-py3.7-macosx-10.9-x86_64.egg/matplotlib/pyplot.py in <module>
     41 from matplotlib import docstring
     42 from matplotlib.backend_bases import FigureCanvasBase, MouseButton
---> 43 from matplotlib.figure import Figure, figaspect
     44 from matplotlib.gridspec import GridSpec
     45 from matplotlib import rcParams, rcParamsDefault, get_backend, rcParamsOrig

~/opt/anaconda3/lib/python3.7/site-packages/matplotlib-3.2.1+1847.gfc8a2fa24-py3.7-macosx-10.9-x86_64.egg/matplotlib/figure.py in <module>
     15 
     16 import matplotlib as mpl
---> 17 from matplotlib import docstring, projections
     18 from matplotlib import __version__ as _mpl_version
     19 

~/opt/anaconda3/lib/python3.7/site-packages/matplotlib-3.2.1+1847.gfc8a2fa24-py3.7-macosx-10.9-x86_64.egg/matplotlib/projections/__init__.py in <module>
      2 from .geo import AitoffAxes, HammerAxes, LambertAxes, MollweideAxes
      3 from .polar import PolarAxes
----> 4 from mpl_toolkits.mplot3d import Axes3D
      5 
      6 

~/opt/anaconda3/lib/python3.7/site-packages/mpl_toolkits/mplot3d/__init__.py in <module>
----> 1 from .axes3d import Axes3D

~/opt/anaconda3/lib/python3.7/site-packages/mpl_toolkits/mplot3d/axes3d.py in <module>
     40 
     41 
---> 42 class Axes3D(Axes):
     43     """
     44     3D axes object.

~/opt/anaconda3/lib/python3.7/site-packages/mpl_toolkits/mplot3d/axes3d.py in Axes3D()
     50     def __init__(
     51             self, fig, rect=None, *args,
---> 52             azim=-60, elev=30, zscale=None, sharez=None, proj_type='persp',
     53             **kwargs):
     54         """

~/opt/anaconda3/lib/python3.7/site-packages/matplotlib-3.2.1+1847.gfc8a2fa24-py3.7-macosx-10.9-x86_64.egg/matplotlib/docstring.py in dedent_interpd(func)
     78     """Dedent *func*'s docstring, then interpolate it with ``interpd``."""
     79     func.__doc__ = inspect.getdoc(func)
---> 80     return interpd(func)

~/opt/anaconda3/lib/python3.7/site-packages/matplotlib-3.2.1+1847.gfc8a2fa24-py3.7-macosx-10.9-x86_64.egg/matplotlib/docstring.py in __call__(self, func)
     38     def __call__(self, func):
     39         if func.__doc__:
---> 40             func.__doc__ %= self.params
     41         return func
     42 

KeyError: 'scale'


Solution 1:[1]

It appears to be related to a version mismatch between mpl_toolkits and matplotlib. I couldn’t figure out how to upgrade mpl_toolkits, so I downgraded matplotlib, and the error went away.

pip3 install matplotlib==2.*

Solution 2:[2]

For those who may face the same error as me, the problem was solved by updating the version of Jupyter notebook to the latest version in my case.

Solution 3:[3]

I had the same problem. In my case this issue was solved by remove mpl-toolkits in appdata

~/opt/anaconda3/lib/python3.7/site-packages/mpl_toolkits/

It will be helpful remove this file

Solution 4:[4]

I met with this problem, too. And the problem is solved by

conda install matplotlib

Hope this working.

Solution 5:[5]

In my case, using ipython and matplotlib installed in virtualenv, I had to install python-tk on my Ubuntu 20.04:

sudo apt install python-tk

based on:
https://matplotlib.org/3.1.1/faq/virtualenv_faq.html

Solution 6:[6]

Uninstall the old Matplotlib first using the command:

sudo pip3 uninstall matplotlib

Install it again using:

sudo pip3 install matplotlib

Check whether it is working or not. If not working Uninstall twice the matplotlib. Because two version matplotlib may present.

Like this method only, I solved the issue or update the anaconda to update the new version If you are using anaconda.

Hope this may work well.

Solution 7:[7]

I had to pip3 uninstall matplotlib twice since I apparently had 2 versions installed. Then reinstall it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 colllin
Solution 2 yasitski
Solution 3 HongJae Kwak
Solution 4 ???
Solution 5 domino_pl
Solution 6 Emma
Solution 7 user290865