undefined behavior of figure.add_subplot() once subplot is modified. #429

yannpaul opened this Issue Aug 19, 2011 · 6 comments

6 participants


Modifying a subplot using change_geometry() method does not update the corresponding key for the subplot in the figure. So when you call add_subplot(), you need to call it with the original args and *kwargs not the modified ones, if you want to access the already existing subplot.

This was identified in matplotlib version, but looking at the current code in github, it appears the same behavior exists.

Matplotlib Developers member

I am inclined to think that the whole business of keeping a key and allowing add_subplot() to return an existing axes if the key is matched is a mistake--extra complexity for minimal gain. Is there a major use case for this that would be substantially more difficult to handle without it?


I agree with efiring, wouldn't it be better to simply separate the {add,get}_subplot functionality? Whenever I use add_subplot I need to put in extra logic to check that new axes haven't been created. It doesn't exactly lead to nice looking code.

Matplotlib Developers member

I agree.

More evidence that keeping track of the axes via hash, at all, is hard (and error prone):

>>> import matplotlib.pyplot as plt
>>> ax = plt.subplot(121, projection='polar')
>>> ax_via_gca = plt.gca(projection='polar')
>>> ax_via_gca is ax
Matplotlib Developers member

I am closing this because it seems to be fixed (@pelson 's example now returns True). Please re-open if this is still an issue.

@tacaswell tacaswell closed this Jan 16, 2014
Matplotlib Developers member

Cool. Thanks for spotting @tacaswell - I think I might have fixed this some time ago between v1.1 and v1.2 to aid in the Cartopy interfacing.


This issue doesn't seem to be fixed... Or it cropped back up since this one is pretty old. See this stackoverflow link for an example. http://stackoverflow.com/questions/31600726/matplotlib-add-subplot-change-geometry-bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment