Matplotlib 属性错误:模块 'matplotlib.cbook' 没有属性 '_define_aliases'。

17

当我尝试使用pyplot在jupyter上绘制图形时,运行以下代码:

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()

这里返回以下错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-16-51b004b519a9> in <module>()
----> 1 get_ipython().run_line_magic('matplotlib', 'inline')
      2 
      3 
      4 import matplotlib.pyplot as plt
      5 plt.plot([1,2,3,4])

c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\IPython\core\interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2129                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2130             with self.builtin_trap:
-> 2131                 result = fn(*args,**kwargs)
   2132             return result
   2133 

<decorator-gen-108> in matplotlib(self, line)

c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\IPython\core\magics\pylab.py in matplotlib(self, line)
     97             print("Available matplotlib backends: %s" % backends_list)
     98         else:
---> 99             gui, backend = self.shell.enable_matplotlib(args.gui)
    100             self._show_matplotlib_backend(args.gui, backend)
    101 

c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\IPython\core\interactiveshell.py in enable_matplotlib(self, gui)
   3049                 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
   3050 
-> 3051         pt.activate_matplotlib(backend)
   3052         pt.configure_inline_support(self, backend)
   3053 

c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\IPython\core\pylabtools.py in activate_matplotlib(backend)
    308     matplotlib.rcParams['backend'] = backend
    309 
--> 310     import matplotlib.pyplot
    311     matplotlib.pyplot.switch_backend(backend)
    312 

c:\program files (x86)\microsoft visual studio\shared\python36_64\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

c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\matplotlib\colorbar.py in <module>()
     28 import matplotlib.artist as martist
     29 import matplotlib.cbook as cbook
---> 30 import matplotlib.collections as collections
     31 import matplotlib.colors as colors
     32 import matplotlib.contour as contour

c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\matplotlib\collections.py in <module>()
     17 
     18 import matplotlib as mpl
---> 19 from . import (_path, artist, cbook, cm, colors as mcolors, docstring,
     20                lines as mlines, path as mpath, transforms)
     21 

c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\matplotlib\lines.py in <module>()
    206 
    207 
--> 208 @cbook._define_aliases({
    209     "antialiased": ["aa"],
    210     "color": ["c"],

AttributeError: module 'matplotlib.cbook' has no attribute '_define_aliases'

我的Matplotlib在没有使用Jupyter的情况下一直运作正常。我已经尝试彻底重新安装了Matplotlib、Jupyter和Python,但仍然遇到相同的错误。也许有人曾经遇到过同样的问题?


在重新安装matplotlib后,您是否重新启动了IPython? - ImportanceOfBeingErnest
1
当然!但是它并没有帮助。:/ - Daniel Wlazło
可能是IPython尝试使用另一个/不同/旧版本的matplotlib,而不是您安装的那个。 - ImportanceOfBeingErnest
3个回答

14

我曾遇到这个精确的错误。问题是安装了两个matplotlib软件包,一个通过conda安装,另一个通过pip安装。

要测试这个问题:

$ conda list matplotlib

matplotlib 2.0.2 np113py35_0 matplotlib 2.1.1

问题!解决方案:

$ pip uninstall matplotlib

可能更好的想法是强制升级matplotlib到pip想要的版本:

$ conda install matplotlib=2.1.1


1
对我来说,这是在networkx2.2和matplotlib3.2.1下,仅仅在conda侧拥有两者是不够的。我必须升级到networkx 2.4才行。 - sigurdb
你为什么没有使用 conda uninstall matplotlib 呢? - Dataman
1
@Dataman,因为需要删除的包是 pip 中的一个。最终只会留下一个 matplotlib(即升级后的 conda 包)。 - Jan
我也遇到了两个竞争版本的matplotlib的问题,出现了以下错误(希望能帮到其他人):AttributeError: module 'matplotlib.cbook' has no attribute '_Stack'。 - undefined

1

你可以做的一件事情是删除matplotlib文件夹来保证卸载。 在Windows中,你可以在以下位置找到:

C:\Users\YOUR_USER\AppData\Local\Programs\Python\YOUR_PYTHON_VERSION\Lib\site-packages

如果您有一个名为“matplotlib”的文件夹,请删除它,然后重新安装该软件包。


1

我在安装scikit后也遇到了完全相同的错误。解决方法是在安装任何其他包之前首先使用以下命令升级pip。

!pip install pip --upgrade

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接