为什么 MacOS 上的 Matplotlib 3.6.0 在显示绘图时会抛出 `AttributeError` ?

我有以下简单的代码:

import matplotlib.pyplot as plt
x = [1,2,3,4]
y = [34, 56, 78, 21]
plt.plot(x, y)
plt.show()

但是将我的 MacBook Pro 更改为 M1 芯片后,我收到以下错误:

Traceback (most recent call last):
  File "/Users/freddy/PycharmProjects/TPMetodosNoParametricos/main.py", line 291, in <module>
    plt.plot(x, y)
  File "/Users/freddy/PycharmProjects/TPMetodosNoParametricos/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 2728, in plot
    return gca().plot(
  File "/Users/freddy/PycharmProjects/TPMetodosNoParametricos/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 2225, in gca
    return gcf().gca()
  File "/Users/freddy/PycharmProjects/TPMetodosNoParametricos/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 830, in gcf
    return figure()
  File "/Users/freddy/PycharmProjects/TPMetodosNoParametricos/venv/lib/python3.8/site-packages/matplotlib/_api/deprecation.py", line 454, in wrapper
    return func(*args, **kwargs)
  File "/Users/freddy/PycharmProjects/TPMetodosNoParametricos/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 771, in figure
    manager = new_figure_manager(
  File "/Users/freddy/PycharmProjects/TPMetodosNoParametricos/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 346, in new_figure_manager
    _warn_if_gui_out_of_main_thread()
  File "/Users/freddy/PycharmProjects/TPMetodosNoParametricos/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 336, in _warn_if_gui_out_of_main_thread
    if (_get_required_interactive_framework(_get_backend_mod()) and
  File "/Users/freddy/PycharmProjects/TPMetodosNoParametricos/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 206, in _get_backend_mod
    switch_backend(dict.__getitem__(rcParams, "backend"))
  File "/Users/freddy/PycharmProjects/TPMetodosNoParametricos/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 266, in switch_backend
    canvas_class = backend_mod.FigureCanvas
AttributeError: module 'backend_interagg' has no attribute 'FigureCanvas'

为什么代码会抛出这个错误?

我的 matplotlib 版本是 3.6.0

stack overflow Why does Matplotlib 3.6.0 on MacOS throw an `AttributeError` when showing a plot?
原文答案
author avatar

接受的答案

我今天在同一 matplotlib 版本的另一台机器上遇到了同样的问题。我降级到 3.5.0 版,现在它可以工作了。


答案:

作者头像

是的,我通过使用 pip install matplotlib==3.5.0 解决了这个问题,它现在可以工作了