AttributeError: 'str' object has no attribute 'draw_path'

AttributeError: 'str' object has no attribute 'draw_path'

我正在将为 Python 2.7Matplotlib 1.3.1(遗留 MacOS Python 实现)编写的过时的 Matplotlib 脚本转换为 Python 3Matplotlib 3.5.1。不幸的是,我无法提供可重现的示例,因为代码是专有的(我试图从头开始重现错误,但未能成功。)因此,对于缺少 MVCE 表示歉意。

在面积图上将绘图发送到 savefig 时出现以下错误:

Traceback (most recent call last):
  File "chart_area.py", line 323, in <module>
    chart_tools.save(logger=LOG)
  File "/Users/Dave/PycharmProjects/matplotlib/chart_tools.py", line 1053, in save
    plt.savefig(
  File "/Users/Dave/Library/Python/3.8/lib/python/site-packages/matplotlib/pyplot.py", line 859, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/Users/Dave/Library/Python/3.8/lib/python/site-packages/matplotlib/figure.py", line 2311, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/Users/Dave/Library/Python/3.8/lib/python/site-packages/matplotlib/backend_bases.py", line 2210, in print_figure
    result = print_method(
  File "/Users/Dave/Library/Python/3.8/lib/python/site-packages/matplotlib/backend_bases.py", line 1639, in wrapper
    return func(*args, **kwargs)
  File "/Users/Dave/Library/Python/3.8/lib/python/site-packages/matplotlib/backends/backend_agg.py", line 509, in print_png
    FigureCanvasAgg.draw(self)
  File "/Users/Dave/Library/Python/3.8/lib/python/site-packages/matplotlib/backends/backend_agg.py", line 407, in draw
    self.figure.draw(self.renderer)
  File "/Users/Dave/Library/Python/3.8/lib/python/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/Users/Dave/Library/Python/3.8/lib/python/site-packages/matplotlib/figure.py", line 1862, in draw
    self.patch.draw(renderer)
  File "/Users/Dave/Library/Python/3.8/lib/python/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/Users/Dave/Library/Python/3.8/lib/python/site-packages/matplotlib/patches.py", line 587, in draw
    draw_path(tpath, affine,
  File "/Users/Dave/Library/Python/3.8/lib/python/site-packages/matplotlib/patheffects.py", line 100, in draw_path
    path_effect.draw_path(self._renderer, gc, tpath, affine,
AttributeError: 'str' object has no attribute 'draw_path'

savefig 调用是:

plt.savefig(f"{payload['p_dict']['chartPath']}{payload['p_dict']['fileName']}", **payload['k_dict']['k_plot_fig'])

保存路径是 (type = string) 并且 kwargs 非常简单 (type = dict):

{'bbox_extra_artists': None, 'bbox_inches': None, 'format': None, 'frameon': None, 'orientation': None, 'pad_inches': None, 'papertype': None, 'transparent': True}

matplotlib 的 draw_path.py 中的相关行是:

def draw_path(self, gc, tpath, affine, rgbFace=None):             # 98
    for path_effect in self._path_effects:                        # 99
        path_effect.draw_path(self._renderer, gc, tpath, affine,  # 100
                              rgbFace)

我完全不知道为什么 path_effect 对象会以字符串形式出现。

同一个项目有一个纯文本图表,它使用相同的 savefig 方法调用并且不会产生错误。据我所知,整个项目中没有动画。我已经尝试过使用和不使用 kwargs 并得到相同的错误。可能是 plt.rcParams 或 sheet 中的某种东西,但我怀疑(例如不知道)有东西被传递给 axessubplot 那就是罪魁祸首。我想也有可能堆栈跟踪是一个红色鲱鱼,并且某处存在 bytes/strings 问题。我被难住了。

更新 1:

我修改了 patheffects.py 以检查来自 draw_path() 方法的负载:

object type value
self._path_effects list ['[]']
path_effect string '[]'

我尝试使用 literal_eval 将 path_effect 对象转换为列表对象,但随后得到以下回溯:

AttributeError: 'list' object has no attribute 'draw_path'

我开始怀疑我使用的 matplotlib 版本可能存在错误 3.5.1

原来错误的来源是样式 sheet 条目。 matplotlib 期望列表在没有 [] 的情况下传递(并且以逗号分隔)。当 matplotlib 遇到 [] 时,它不会将其解释为列表。空列表将表示为:

path.effects:

而不是我正在使用的东西:

path.effects: []

格式正确的列表参数为:

some.thing: 1, 2, 3, 4, 5