35

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.

asked Jun 12, 2016 at 23:29

5 Answers 5

6

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

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

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

answered Aug 8, 2019 at 22:39
5

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)
answered Jun 13, 2016 at 4:20
1

I did a conda update pandas and the error went away. Nothing else I could find worked.

Mine was a slightly different flavor of this error (not 'add_axes'). It happened when I was trying to use pandas.DataFrame.plot within matplotlib subplots.

I was on pandas 0.25.3 before and after, and a number of packages were upgraded/downgraded.

answered Sep 6, 2021 at 7:10
0

I encountered this same error several days ago on both a Macbook and a Lenovo laptop. I used Anaconda for my python installation on both computers.

This is a problem with the Matplotlib package management. So the solution for me was to remove Matplotlib and reinstall it. Then the error is gone.

answered Jan 25, 2022 at 18:00
-6

Just downgrading matplotlib to an old version would help. I downgraded it to 1.4.0 and it fixed the problem.

jtlz2
8,51711 gold badges73 silver badges128 bronze badges
answered May 19, 2018 at 11:49
2
  • 129
    Downgrading a package should never be a solution. Commented Jun 19, 2018 at 10:34
  • 1
    @yeliabsalohcin you could fork it, if you so desire Commented Jan 11, 2021 at 0:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.