python - Matplotlib 属性错误 : module 'matplotlib.cbook' has no attribute '_define_aliases'

标签 python matplotlib jupyter-notebook

当尝试使用 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,但仍然出现相同的错误。也许有人有同样的问题?

最佳答案

我遇到了这个确切的错误。问题是 2 个 matplotlib 包由 conda 安装,一个由 pip 安装

对此进行测试:

$conda 列表 matplotlib

matplotlib 2.0.2 np113py35_0 matplotlib 2.1.1

问题!修复:

$ pip 卸载 matplotlib

强制 matplotlib 升级到 pip 想要的版本可能是个好主意:

$ conda install matplotlib=2.1.1

关于python - Matplotlib 属性错误 : module 'matplotlib.cbook' has no attribute '_define_aliases' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53421905/

相关文章:

python - 如何将这种形式的字典更改为 pandas 数据框?

python - Keras:如何保存模型或权重?

docker - 在 pyspark-notebook docker 容器上运行 magellan

python - Python 内置最大/最小函数的默认键

python - 使用 requirements.txt 中的相对路径通过 pip 安装 tar.gz 文件

python - Matplotlib:将网格线保留在图形后面,但 y 轴和 x 轴在上面

python - 几分钟后 numpy plot 创建图形失败 idel

python - 如何创建传递自定义半径的地理热图

github - 如何在 github 上显示 .ipynb 文件?

python - Pandas :TypeError: float() argument must be a string or a number, 不是 'pandas._libs.interval.Interval'