Skip to content
This repository

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

Closed
yannpaul opened this Issue · 5 comments

5 participants

Yann Thomas A Caswell Eric Firing c00kiemonster Phil Elson
Yann

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 0.99.1.1, but looking at the current code in github, it appears the same behavior exists.

Eric Firing
Owner

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?

c00kiemonster

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.

Phil Elson
Collaborator
pelson commented

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
False
Thomas A Caswell
Collaborator

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.

Thomas A Caswell tacaswell closed this
Phil Elson
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.