Python matplotlib: AttributeError: 'AxesSubplot' 对象没有属性 'add_axes'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37780100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:55:37  来源:igfitidea点击:

matplotlib: AttributeError: 'AxesSubplot' object has no attribute 'add_axes'

pythonmatplotlibaxes

提问by

Not sure exactly sure how to fix the following attribute error:

不确定如何修复以下属性错误:

AttributeError: 'AxesSubplot' object has no attribute 'add_axes'

The offending problem seems to be linked to the way I have set up my plot:

有问题的问题似乎与我设置情节的方式有关:

gridspec_layout = gridspec.GridSpec(3,3)
pyplot_2 = fig.add_subplot(gridspec_layout[2])

ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], wcs=wcs)
pyplot_2.add_axes(ax)

Does anybody know how to solve this? Many thanks.

有谁知道如何解决这个问题?非常感谢。

回答by ljetibo

There's not much details to go on in your question but I'll wager a guess. The error is pretty self-explanatory. You can't add_axesto pyplot_2because pyplot_2is a matplotlib.axes.AxesSubplotobject and they don't have an add_axesmethod defined.

您的问题没有太多细节可谈,但我打赌。该错误是不言自明的。您不能这样做add_axespyplot_2因为它pyplot_2是一个matplotlib.axes.AxesSubplot对象并且它们没有add_axes定义方法。

Only matplotlib.figure.Figureobjects have add_axesmethod defined on them.

只有matplotlib.figure.Figure对象add_axes定义了方法。

From what I got from a short browse through the WCSAxes official documentation their recommended approach would be:

根据我从 WCSAxes 官方文档的简短浏览中得到的信息,他们推荐的方法是:

wcs = astropy.wcs.WCS(....)
fig = matplotlib.pyplot.figure()
pyplot_2 = fig.add_subplot(gridspec_layout[2], projection=wcs)

回答by coldestlin

just downgrade matploblit to an old version would help. I downgrade it to 1.4.0 and it fix the problem.

将 matploblit 降级到旧版本会有所帮助。我将其降级到 1.4.0 并解决了问题。

回答by The Puternerd

You now need to use set_prop_cycle i.e. ax.set_prop_cycle(color=['red', 'green', 'blue'])

你现在需要使用 set_prop_cycle 即 ax.set_prop_cycle(color=['red', 'green', 'blue'])

Axes.set_color_cycle(clist) was depreciated since, version 1.5.

Axes.set_color_cycle(clist) 从 1.5 版开始折旧。

https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.axes.Axes.set_prop_cycle.html

https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.axes.Axes.set_prop_cycle.html