AttributeError:模块“matplotlib.cbook”没有属性“_Stack”

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

提问人:Nora 提问时间:11/10/2023 最后编辑:Trenton McKinneyNora 更新时间:11/12/2023 访问量:130

问:

我在pycharm中运行以下代码:

import matplotlib.pyplot as plt
import numpy as np

# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

fig, ax = plt.subplots()
ax.plot(t, s)

ax.set(xlabel='time (s)', ylabel='voltage (mV)',
       title='About as simple as it gets, folks')
ax.grid()

fig.savefig("test.png")
plt.show()

我以前已经让它工作了。突然,它返回以下错误:

Traceback (most recent call last):
  File "C:\Users\NoraBuggy\PycharmProjects\pythonProject\main.py", line 8, in <module>
    fig, ax = plt.subplots()
  File "C:\Users\NoraBuggy\miniconda3\lib\site-packages\matplotlib\pyplot.py", line 1598, in subplots
    fig = figure(**fig_kw)
  File "C:\Users\NoraBuggy\miniconda3\lib\site-packages\matplotlib\pyplot.py", line 934, in figure
    manager = new_figure_manager(
  File "C:\Users\NoraBuggy\miniconda3\lib\site-packages\matplotlib\pyplot.py", line 465, in new_figure_manager
    return _get_backend_mod().new_figure_manager(*args, **kwargs)
  File "C:\Users\NoraBuggy\miniconda3\lib\site-packages\matplotlib\backend_bases.py", line 3396, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "C:\Users\NoraBuggy\miniconda3\lib\site-packages\matplotlib\backend_bases.py", line 3401, in new_figure_manager_given_figure
    return cls.FigureCanvas.new_manager(figure, num)
  File "C:\Users\NoraBuggy\miniconda3\lib\site-packages\matplotlib\backend_bases.py", line 1785, in new_manager
    return cls.manager_class.create_with_canvas(cls, figure, num)
  File "C:\Users\NoraBuggy\miniconda3\lib\site-packages\matplotlib\backend_bases.py", line 2680, in create_with_canvas
    return cls(canvas_class(figure), num)
  File "C:\Users\NoraBuggy\miniconda3\lib\site-packages\matplotlib\backends\backend_qt.py", line 522, in __init__
    super().__init__(canvas, num)
  File "C:\Users\NoraBuggy\miniconda3\lib\site-packages\matplotlib\backend_bases.py", line 2654, in __init__
    self.toolbar = self._toolbar2_class(self.canvas)
  File "C:\Users\NoraBuggy\miniconda3\lib\site-packages\matplotlib\backends\backend_qt.py", line 669, in __init__
    NavigationToolbar2.__init__(self, canvas)
  File "C:\Users\NoraBuggy\miniconda3\lib\site-packages\matplotlib\backend_bases.py", line 2844, in __init__
    self._nav_stack = cbook._Stack()
  File "C:\Users\NoraBuggy\miniconda3\lib\site-packages\matplotlib\_api\__init__.py", line 217, in __getattr__
    raise AttributeError(
AttributeError: module 'matplotlib.cbook' has no attribute '_Stack'

Process finished with exit code 1

Matplotlib 今天早些时候工作正常。这个错误是在我尝试使用 seaborn 绘制东西后开始的,这给了我一个类似的错误,所以我卸载了 seaborn。现在,当我尝试绘制任何内容时,我被此错误消息卡住了。

我看到一篇类似的帖子,建议确保您没有安装多个版本的 matplotlib。

我在pycharm终端中运行了以下内容:

conda list matplotlib

这给了我这个:

# packages in environment at C:\Users\NoraBuggy\miniconda3:
#
# Name                    Version                   Build  Channel
matplotlib                3.6.2                    pypi_0    pypi
matplotlib-base           3.8.0            py39h4ed8f06_0

我尝试卸载matplotlib-base,然后给我一个错误,说没有安装matplotlib。import matplotlib.pyplot as plt

蟒蛇 matplotlib pycharm miniconda

评论

0赞 simryang 11/10/2023
卸载 matplotlib 并重新安装 matplotlib 怎么样?
0赞 Trenton McKinney 11/10/2023
不要使用 pip 从 conda 安装/删除软件包。如果 conda 通道中存在包,则应始终使用 .除非您使用的是嵌入式设备(树莓派),或者其他高清和内存有限的设备,否则请使用 Anaconda 而不是 minicondaconda install package
0赞 Trenton McKinney 11/12/2023
此问题不可重现。这是您的环境的安装问题。

答:

0赞 Hassan Safari 11/12/2023 #1

unistall matplotlib 并尝试以下操作:

pip install matplotlib==2.2.3

试试这个:

pip install networkx --upgrade