Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 status somewhere I can check?

Many thanks,

David

like image 413
Dave Avatar asked Sep 26 '11 15:09

Dave


People also ask

What happens if I dont use %Matplotlib inline?

In the current versions of the IPython notebook and jupyter notebook, it is not necessary to use the %matplotlib inline function. As, whether you call matplotlib. pyplot. show() function or not, the graph output will be displayed in any case.

Is matplotlib asynchronous?

Asynchronous Plotting in Matplotlib: rather than call savefig directly, add plots to an asynchronous queue to avoid holding up the main program. Makes use of multiple processes to speed up the writing out.

Is matplotlib better than Plotly?

Matplotlib is also a great place for new Python users to start their data visualization education, because each plot element is declared explicitly in a logical manner. Plotly, on the other hand, is a more sophisticated data visualization tool that is better suited for creating elaborate plots more efficiently.


2 Answers

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).

like image 195
Eric O Lebigot Avatar answered Sep 27 '22 19:09

Eric O Lebigot


import matplotlib.pyplot as plt
if plt.get_fignums():
    # window(s) open
else:
    # no windows
like image 29
unutbu Avatar answered Sep 27 '22 19:09

unutbu