3
votes

Is it possible to display a single figure multiple times in matplotlib?

I am looking for a solution that can roughly resemble the following:

fig1 = plt.figure()
fig2 = plt.figure()

ax1 = fig1.add_subplot(111)
ax2 = fig2.add_subplot(111)

ax1.imshow(a)
ax2.imshow(b)

fig1.show()
fig2.show()
fig1.show()

This seems to work in IDLE, figures stay active until closed and I can type in IDLE to open a new figure while the old one is still active. This does not work though when I run the script. Does this have anything to do with the interactive mode?

I also tried to pause the plot when running the script:

fig1.show()
plt.pause(10)

but this displays immediately both figures. Why is IDLE capable of displaying fig1.show() and waiting till it's closed, but while running the script fig1.show() closes immediately unless paused? And why does it display all the figures at the same time instead of just the one instance?

1

1 Answers

-1
votes

If you run matplotlib inside, for example ipython, you should use fig1.show() to show the figure and at the same time avoid blocking the ipython terminal. However if you want to run the script from a cmd, i.e. with python script.py, you should instead use plt.show() (note that it's enough with plt.show() once even if you have multiple figures), which will keep the figures up.