Issue
I am using matplotlib in interactive mode to show the user a plot that will help them enter a range of variables. They have the option of hitting "?" to show this plot, and the prompt for variables will then be repeated.
How do I know to not re-draw this plot if it's still being displayed?
Superficially, I have this clunky (pseudo-ish) code:
answer = None
done_plot = False
while answer == None:
answer = get_answer()
if answer == '?':
if done_plot:
have_closed = True
##user's already requested a plot - has s/he closed it?
## some check here needed:
have_closed = ?????
if have_closed == False:
print 'You already have the plot on display, will not re-draw'
answer = None
continue
plt.ion()
fig = plt.figure()
### plotting stuff
done_plot = True
answer = None
else:
###have an answer from the user...
what can I use (in terms of plt.gca(), fig etc...) to determine if I need to re-plot? Is there a status somewhere I can check?
Many thanks,
David
Solution
In the same vein as unutbu's answer, you can also check whether a given figure is still opened with
import matplotlib.pyplot as plt
if plt.fignum_exists(<figure number>):
# Figure is still opened
else:
# Figure is closed
The figure number of a figure is in fig.number
.
PS: Note that the "number" in figure(num=…)
can actually be a string: it is displayed in the window title. However, the figure still has a number
attribute which is numeric. The original string num
value can however be used with fignum_exists()
(since 2015, according to Mark H, in the comments).
PPS: That said, subplots(…, num=<string num>)
properly recovers the existing figure with the given string number. Thus, figures are still known by their string number in some parts of Matplotlib.
Answered By - Eric O. Lebigot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.