开发者

matplotlib interactive mode: determine if figure window is still displayed

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 statu开发者_StackOverflow社区s somewhere I can check?

Many thanks,

David


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 cannot be used with fignum_exists().

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 (but fignum_exists() doesn't use such strings).


import matplotlib.pyplot as plt
if plt.get_fignums():
    # window(s) open
else:
    # no windows
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜