Say that I have two figures in matplotlib, with one plot per figure:
import matplotlib.pyplot as plt f1 = plt.figure() plt.plot(range(0,10)) f2 = plt.figure() plt.plot(range(10,20))
Then I show both in one shot
plt.show()
Is there a way to show them separately, i.e. to show just f1
?
Or better: how can I manage the figures separately like in the following 'wishful' code (that doesn't work):
f1 = plt.figure() f1.plot(range(0,10)) f1.show()
The easiest way to display multiple images in one figure is use figure(), add_subplot(), and imshow() methods of Matplotlib. The approach which is used to follow is first initiating fig object by calling fig=plt. figure() and then add an axes object to the fig by calling add_subplot() method.
Sure. Add an Axes
using add_subplot
. (Edited import
.) (Edited show
.)
import matplotlib.pyplot as plt f1 = plt.figure() f2 = plt.figure() ax1 = f1.add_subplot(111) ax1.plot(range(0,10)) ax2 = f2.add_subplot(111) ax2.plot(range(10,20)) plt.show()
Alternatively, use add_axes
.
ax1 = f1.add_axes([0.1,0.1,0.8,0.8]) ax1.plot(range(0,10)) ax2 = f2.add_axes([0.1,0.1,0.8,0.8]) ax2.plot(range(10,20))
With Matplotlib prior to version 1.0.1, show()
should only be called once per program, even if it seems to work within certain environments (some backends, on some platforms, etc.).
The relevant drawing function is actually draw()
:
import matplotlib.pyplot as plt plt.plot(range(10)) # Creates the plot. No need to save the current figure. plt.draw() # Draws, but does not block raw_input() # This shows the first figure "separately" (by waiting for "enter"). plt.figure() # New window, if needed. No need to save it, as pyplot uses the concept of current figure plt.plot(range(10, 20)) plt.draw() # raw_input() # If you need to wait here too... # (...) # Only at the end of your program: plt.show() # blocks
It is important to recognize that show()
is an infinite loop, designed to handle events in the various figures (resize, etc.). Note that in principle, the calls to draw()
are optional if you call matplotlib.ion()
at the beginning of your script (I have seen this fail on some platforms and backends, though).
I don't think that Matplotlib offers a mechanism for creating a figure and optionally displaying it; this means that all figures created with figure()
will be displayed. If you only need to sequentially display separate figures (either in the same window or not), you can do like in the above code.
Now, the above solution might be sufficient in simple cases, and for some Matplotlib backends. Some backends are nice enough to let you interact with the first figure even though you have not called show()
. But, as far as I understand, they do not have to be nice. The most robust approach would be to launch each figure drawing in a separate thread, with a final show()
in each thread. I believe that this is essentially what IPython does.
The above code should be sufficient most of the time.
PS: now, with Matplotlib version 1.0.1+, show()
can be called multiple times (with most backends).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With