This is hopefully a simple question but I can’t figure it out at the moment. I want to use matplotlib to show 2 figures and then use them interactively. I create the figures with:
import matplotlib import pylab as pl f1 = pl.figure() f2 = pl.figure()
and can use the MATLAB-like pyplot interface to plot and draw in both figures. With
current_figure = pl.gcf()
I can determine the currently active figure for the pyplot interface, depending on which figure I clicked in. Now I want to draw something to the first figure with the pyplot interface but the current figure can be either of them. So is there something like
pl.set_current_figure(figure)
or any workaround? (I know that I can use the object oriented interface but for interactive stuff just using commands like plot(x, y) is much nicer)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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().
Method 2
Give each figure a number:
f1 = pl.figure(1) f2 = pl.figure(2) # use f2 pl.figure(1) # make f1 active again
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0