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

user3125347 picture user3125347 · Jun 13, 2016 · Viewed 90.1k times · Source

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.

Answer

ljetibo picture ljetibo · Jun 13, 2016

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_axes to pyplot_2 because pyplot_2 is a matplotlib.axes.AxesSubplot object and they don't have an add_axes method defined.

Only matplotlib.figure.Figure objects have add_axes method defined on them.

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

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