Matplotlib: how to set the current figure?

Solution 1:

You can simply set figure f1 as the new current figure with:

pl.figure(f1.number)

Another option is to give names (or numbers) to figures, which might help make the code easier to read:

pl.figure("Share values")
# ... some plots ...
pl.figure("Profits")
# ... some plots ...

pl.figure("Share values")  # Selects the first figure again

In fact, figure "numbers" can be strings, which are arguably more explicit that simple numbers.

PS: The pyplot equivalent of pylab.figure() is matplotlib.pyplot.figure().

Solution 2:

Give each figure a number:

f1 = pl.figure(1)
f2 = pl.figure(2)
# use f2
pl.figure(1) # make f1 active again