I'm not sure how to word my question more clearly. Basically, is PyPlot limited to one instance/window? Any hack or workaround I try either causes my program to freeze or for the second pyplot window to be queued until the first one is closed.
Asked
Active
Viewed 1e+01k times
4 Answers
95
Sure, just open a new figure:
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.figure()
plt.plot(range(10), 'ro-')
plt.figure(), plt.plot(...)
plt.show() # only do this once, at the end
If you're running this in the default python interpreter, this won't work, as each figure needs to enter the gui's mainloop. If you want to run things in an interactive shell, look into IPython. If you just run this normally (i.e. put it into a file and call python filename.py
) it will work fine, though.

smci
- 32,567
- 20
- 113
- 146

Joe Kington
- 275,208
- 71
- 604
- 463
-
9It is implicit in this answer (and I missed it) that you should only call `plt.show()` one time at the end. This is discussed further at http://stackoverflow.com/a/2399978/527489. If you call plt.show() multiple times, then it still does the queueing that the OP mentions (blocks each window until the previous window is closed). – sage Dec 03 '15 at 18:32
-
Does figure have a separate interactive mode like `fig.ion()` or is `ion()` applied globally? – CMCDragonkai Oct 14 '16 at 15:53
-
3Is there a more object oriented way to work with multiple figures? – Pat Niemeyer Nov 01 '18 at 19:11
-
You can also do `plt.figure(name)` if you want the window to have a name! – George Ogden Jan 08 '21 at 09:38
14
Use plt.figure()
and use a certain number so that the window is fixed:
plt.figure(200)
plt.plot(x)
plt.show()
and for another plot, use a different number:
plt.figure(300)
plt.plot(y)
plt.show()

j35t3r
- 1,254
- 2
- 19
- 53
-
6If the number is not important, one can simply call `plt.figure()` without arguments, and a new figure will be assigned. See [docs](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.figure.html). Also, using multiple calls to `show()` is probably not what you really want, see https://stackoverflow.com/a/2399978/2436175 – Antonio Feb 27 '19 at 21:09
6
The answer to your question is no. You can have as many windows as you want. Firstly, just type
plt.figure(n) #n must be a different integer for every window
for every new figure you want. Secondly, write
plt.show()
only once (!) at the end of everything you want to plot. Here is an example for two histograms:
plt.figure(1)
plt.hist(dataset1)
plt.figure(2)
plt.hist(dataset2)
plt.show()

MiLe
- 183
- 2
- 4
-
This answers misses the fact that the window number is optional (a new unique number will be assigned). See [docs](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.figure.html). – Antonio Feb 27 '19 at 21:16
5
You can do it with the pyplot.show
properties
Example:
plt.show(block=False)

Frightera
- 4,773
- 2
- 13
- 28

Mario Zambrano
- 61
- 1
- 1