简体   繁体   English

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

[英]KeyError: 'scale' for import matplotlib.pyplot as plt

I'm getting KeyError: 'scale' when I'm trying to run import matplotlib.pyplot当我尝试运行 import matplotlib.pyplot 时出现 KeyError: 'scale'

I redownloaded the matplotlib then ran the code but still getting the same error.我重新下载了 matplotlib 然后运行代码但仍然出现相同的错误。

I've also opened the new Jupyter notebook and only ran "import matplotlib.pyplot as plt" and I'm still getting the same error.我还打开了新的 Jupyter 笔记本,只运行了“import matplotlib.pyplot as plt”,我仍然遇到同样的错误。 I didn't get any error when I ran "import matplotlib" only.当我只运行“import matplotlib”时,我没有收到任何错误。

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'

It appears to be related to a version mismatch between mpl_toolkits and matplotlib .它似乎与mpl_toolkitsmatplotlib之间的版本不匹配有关。 I couldn't figure out how to upgrade mpl_toolkits , so I downgraded matplotlib , and the error went away.我不知道如何升级mpl_toolkits ,所以我降级matplotlib ,错误消失了。

pip3 install matplotlib==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.对于那些可能遇到与我相同错误的人,我通过将 Jupyter notebook 版本更新到最新版本来解决问题。

I had the same problem.我有同样的问题。 In my case this issue was solved by remove mpl-toolkits in appdata在我的例子中,这个问题是通过删除 appdata 中的 mpl-toolkits 解决的

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

It will be helpful remove this file删除这个文件会很有帮助

Uninstall the old Matplotlib first using the command:首先使用以下命令卸载旧的 Matplotlib:

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.如果不起作用卸载两次 matplotlib。因为两个版本 matplotlib 可能存在。

Like this method only, I solved the issue or update the anaconda to update the new version If you are using anaconda.只喜欢这个方法,我解决了这个问题或更新 anaconda 以更新新版本如果您使用的是 anaconda。

Hope this may work well.希望这可能运作良好。

I met with this problem, too.我也遇到了这个问题。 And the problem is solved by问题解决了

conda install matplotlib

Hope this working.希望这个工作。

In my case, using ipython and matplotlib installed in virtualenv, I had to install python-tk on my Ubuntu 20.04:在我的例子中,使用安装在 virtualenv 中的 ipython 和 matplotlib,我必须在我的 Ubuntu 20.04 上安装python-tk

sudo apt install python-tk

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

I had to pip3 uninstall matplotlib twice since I apparently had 2 versions installed.我不得不 pip3 uninstall matplotlib 两次,因为我显然安装了 2 个版本。 Then reinstall it.然后重新安装它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM