Skip to content
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

[ENH]: Reusing the same GUI window for showing multiple figures #28259

Closed
vale981 opened this issue May 19, 2024 · 3 comments
Closed

[ENH]: Reusing the same GUI window for showing multiple figures #28259

vale981 opened this issue May 19, 2024 · 3 comments
Labels
Community support Users in need of help. New feature

Comments

@vale981
Copy link

vale981 commented May 19, 2024

Problem

Often I find myself hacking on a function that creates a new Figure object, evaluating it over and other.
It would be very nice to have the option of reusing the same GUI window rather than closing the old one and opening a new one all the time.

Proposed solution

Currently, I'm using decorators that close the last created figure. Maybe one could add a method to the pyplot API that instructs it to re-use the currently open interactive GUI window. A little like what can be emulated with plt.cla() and plt.clf()...

@timhoffm
Copy link
Member

Have you considered using jupyter notebooks? They are well suited for

hacking on a function that creates a new Figure object, evaluating it over and other.

While a „persistent window mode“ would make window management more usable, the iterative editing in a shell (and I‘m assuming you’re working from there) is still more cumbersome than in a notebook. - I‘m sceptical on investing in a feature to improve a workflow that’ll still be inferior to an alternative. But maybe I‘m not fully understanding how you want to work. Please describe your intended workflow in more detail and also the desired new API/commands with a concrete example and expected effect (including when exactly you expect the old figure to end life and the new figure be connected to the gui window).

@timhoffm timhoffm added the status: needs clarification Issues that need more information to resolve. label May 19, 2024
@tacaswell
Copy link
Member

I think all of the API you need from Matplotlib exist

fig = plt.gcf()  # get the currently active figure
fig.clf()        # clear the figure to start fresh
ax_arr = fig.subplots(2, 2)

If you do the above on your function it will always reuse the most recently used figure (or create it if it has to)

fig = plt.figure('some name')    # create a figure by name
fig2 = plt.figure('some name')   # get it back
assert fig is fig2

Using the above you can give a name to your figure so everytime the function run it works on the same window (creating it if it has to)

def some_function(a, b, c, *, fig=None):
    if fig is None:
        fig = plt.figure()
    ...

In your function you should make it possible to pass in a Figure and only have a fallback to plt if it is not passed (so if you change your mind about how to do figure management later you don't have to change the function, only how you call it).

@vale981
Copy link
Author

vale981 commented May 19, 2024

@tacaswell Thank you! That's precisely it. I feel a bit stupid now that I didn't think of fig.subplots. I've built a little decorator that now wraps all my plotting functions to do precisely what you suggested.

@vale981 vale981 closed this as completed May 19, 2024
@rcomer rcomer added Community support Users in need of help. and removed status: needs clarification Issues that need more information to resolve. labels May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community support Users in need of help. New feature
Projects
None yet
Development

No branches or pull requests

4 participants