Skip to content

plt.subplots for already existing Figure #5139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
anntzer opened this issue Sep 25, 2015 · 14 comments
Closed

plt.subplots for already existing Figure #5139

anntzer opened this issue Sep 25, 2015 · 14 comments

Comments

@anntzer
Copy link
Contributor

anntzer commented Sep 25, 2015

The plt.subplots function in very practical for creating a large number of axes simultaneously, but (afaict) always puts them on a new figure, which is impractical e.g. when embedding matplotlib in a GUI, where a Figure object may be created early on and then populated with some axes later.
I would thus suggest providing the same functionality to already existing Figure objects, e.g. by moving the implementation of plt.subplots to a Figure.add_subplots (or Figure.set_subplots, as it may make sense to clear the figure before) method, and have the plt.subplots function be a thin wrapper that creates the Figure and then calls its add_subplots method.

@tacaswell
Copy link
Member

You can use gridspec + a for loop to do this.

@WeatherGod
Copy link
Member

I can see the point in this, but I feel that it goes against what we have
been trying to do, which is to decouple pyplot from the OO interface.

On Fri, Sep 25, 2015 at 8:47 AM, Thomas A Caswell notifications@github.com
wrote:

You can use gridspec + a for loop to do this.


Reply to this email directly or view it on GitHub
#5139 (comment)
.

@anntzer
Copy link
Contributor Author

anntzer commented Sep 25, 2015

I know about GridSpec, it is just a bit awkward to use especially when you also want to use share{x,y}.

@efiring
Copy link
Member

efiring commented Sep 25, 2015

Actually, moving the subplots functionality for generating a whole array in one whack into Figure is a step towards decoupling pyplot from the OO interface.

@tacaswell
Copy link
Member

On consideration, I now agree this is a good idea. This should be a pretty strait forward copy-paste job, @anntzer want to take a crack at it?

anntzer added a commit to anntzer/matplotlib that referenced this issue Sep 25, 2015
Also simplify the implementation a bit.

cf. matplotlib#5139.
@anntzer
Copy link
Contributor Author

anntzer commented Sep 26, 2015

I intended to name the new method Figure.add_subplots (by similarity with Figure.add_subplot) but @tacaswell prefers to keep it Figure.subplots (by similarity with plt.subplots). Any thoughts?

@efiring
Copy link
Member

efiring commented Sep 26, 2015

I can see arguments either way, but overall I agree with @tacaswell. It's shorter. Also, unlike add_subplot, subplots normally would be called only once for a given figure, unless it is cleared.

@anntzer
Copy link
Contributor Author

anntzer commented Sep 27, 2015

Sure, I'll change that. The other question was whether Figure.subplots should call clf first. @tacaswell suggested yes, but I'd rather not because (copy-pasting from diff discussion) IMO if you use the OO interface you know what you're doing and it's anyways easier for the caller to add a call to clf rather than to remove a call to clf if it's already there.

@efiring
Copy link
Member

efiring commented Sep 27, 2015

I don't know. @tacaswell, if clf is called inside, should observers be kept, or not? The danger of not including clf is that people might call subplots repeatedly on the same figure until they run out of memory, or something else goes haywire.

@tacaswell
Copy link
Member

My main reason for wanting to call clf is that I don't like the logic that will, if things line up just right, add_subplot will return back to you a reference to an existing instance, but sometimes gives you back overlapping plots.

On the other hand, the reason to not call clf is to add a bounding box input to Figure.subplots (which iirc can go in through the gridspec_kw?)

@efiring
Copy link
Member

efiring commented Sep 27, 2015

On 2015/09/26 3:56 PM, Thomas A Caswell wrote:

|add_subplot| will return back to you a reference to an existing
instance, but sometimes gives you back overlapping plots.

This is a real mis-feature. We need to get rid of it ASAP, which
unfortunately can't be all that soon.

@tacaswell
Copy link
Member

it also turns out that it (for what ever reason) does not do that in this case and just keeps layering new axes on.

@chanansh
Copy link

Was this added to the package?

@tacaswell
Copy link
Member

Yes, but it is currently on the master branch and not in a released version of the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants