At the end of (a blocking) show() the figure is closed and thus unregistered from pyplot.
Am I using plt.show wrong? Is there another command that doesn’t close the figures? Does it have to be this way? Would a keyword argument be a possible feature request like plt.show(clear_figures=False)?
Hmm, I did toy around with interactive mode a little, but I can’t seem to figure out if that’s really a solution?
If I turn on interactive mode with plt.ion(), then plt.show() would return right away, no? So I would need something like plt.pause which doesn’t work because I don’t want to wait a specific duration, and plt.waitforbuttonpress is waiting indefinitely, but reacting to anything I do to the window, like zooming in.
However, what I am looking for is exactly this behavior, but without having to repeat the whole previous plot:
plt.plot([0, 1], [0, 1])
plt.show()
plt.plot([0, 1], [0, 1]) # I don't like that I have to repeat the previous plot here
plt.plot([0, 1], [1, 0])
plt.show()
Interactive mode would not require pausing or waiting; the figure is opened immediately when it’s first created and remains responsive until closed. plt.show will return immediately because it does nothing in that case.
Why not use fig, ax reference?
Thanks to plt.ion() the fig should show immediately and update after each call of ax.plot(…). At least it works for me using Spyder with Preferences->IPython console->graphics->backend set to auto.
Thanks for the reply. Yes, the figure is opened immediately (or at least I think that’s what’s happening, because it’s only there for a very short time), but then closed again, right away.
import matplotlib.pyplot as plt
plt.ion()
plt.plot([0, 1], [0, 1])
# At this point, I want to view first plot, then press ESC in plot window to close it
plt.plot([0, 1], [1, 0])
# I want to view both first plot and second plot in the window now, then ESC to quit program
That’s why I said I need plt.pause. Otherwise the window is there for only a fraction of a second - also showing both plots right away.
My problems would be completely resolved by something like this:
import matplotlib.pyplot as plt
# plt.ion() # no interactive mode
plt.plot([0, 1], [0, 1])
plt.show(clear_figures=False)
plt.plot([0, 1], [1, 0])
plt.show(clear_figures=False)
Thanks for the reply. I’m not using Spyder or IPython console. For me, if I run the aforementioned code from my terminal, the plot window pops up for the fraction of a second and that’s it. Which makes me think that interactive mode plt.ion() either needs something extra that I haven’t figured out yet, or it’s not suitable in my case.
However, as mentioned above, I was thinking that something like
import matplotlib.pyplot as plt
# plt.ion() # no interactive mode
plt.plot([0, 1], [0, 1])
plt.show(clear_figures=False)
plt.plot([0, 1], [1, 0])
plt.show(clear_figures=False)
I’m not really sure to be honest? I’ve researched a bit and found Backends — Matplotlib 3.9.2 documentation so I’ve created a fresh venv with most recent matplotlib and added to my script on top the following:
import matplotlib
matplotlib.use("TkAgg")
Is that what you mean? It results in the same issue. I’ve never even considered it to be a bug. Because which call would be blocking? Or is it running in a separate thread/process?
Oh, I see. I apologize, I have to keep the first window open, switch to my terminal window, confirm the input prompt, then switch back to the plot window.
You’re right, it does work. However, I’m sorry to say, but the constant switching of windows is not very practical… I sometimes want to inspect a plot chain of up to 50 plots. So far (with duplicating plots), I was able to simply move to the next plotting step by pressing q to close the window. I’m looking for a solution where I don’t have to give up this level of comfort, I’m afraid.