3

I have tried to find solutions to this problem on this site, for example this post, this post, and this one.

I work in engineering, and spend a lot of time plotting data, re-plotting it, adjusting this and that to see how to extract the most information from it in a python shell. However, it's a real pain to each time re-create the plot I want as a new figure. What I want is to be able to re-open a closed figure. Actually, I want to be able to open a figure at all in the pyplot interface.

Typically my workflow might look something like this. I might create a new figure and axes with a call to subplots():

fig, ax = plt.subplots()

Then, I create my plots, perhaps just a simple line plot:

ax.plot(someExistingData)

Now, I will show the plot, using the only way I know how:

plt.show()

Now, after inspecting the plot, I notice that the range is not what I would like, or I want to change the linewidth, or I want to display with circle markers instead of a continuous line, or any of a million things. What I would like to do at this point, after closing the original plot (hitting the red X on the figure that pops up on my computer), is something like:

plt.open(fig)
plt.show()

My understanding is that matplotlib's state interface is only able to find recently-created plots. From the matplotlib documentation about what plt.show() does:

"Display all open figures."

Great, but how do I open a figure in the first place without creating it from scratch? This seems like a simple task. I have an existing figure -> I want to add it to the list of figures matplotlib thinks is "open", and then I want to show that figure with plt.show(). However, I can't seem to find a way to "re-open" an existing figure, or even explicitly "open" a figure at all. I know the figure object still exists, and that figure object still contains my old axis that I want to display, I just want to add that figure object to whatever interface matplotlib uses for displaying it.

Jordan Edmunds
  • 131
  • 1
  • 7
  • 2
    Does this answer your question? [Matplotlib: how to set the current figure?](https://stackoverflow.com/questions/7986567/matplotlib-how-to-set-the-current-figure) – Kaia Sep 04 '20 at 22:37
  • I think this might help: https://stackoverflow.com/a/7987462/1275942. Basically, if you call plt.figure() with a number or tag, you can select it again by calling plt.figure() with the same number. – Kaia Sep 04 '20 at 22:39
  • @Keon When I attempt to call plt.figure(fig.number) the figure that 'attaches' to the pyplot interface has zero Axes, while the fig object still has 1. Showing that with plt.show() gives an empty axes. – Jordan Edmunds Sep 08 '20 at 15:26
  • What backend are you using? I've had some issues where the inline backend flushes plots after ploting them – Kaia Sep 08 '20 at 18:45

0 Answers0