matplotlib交互模式:确定图形窗口是否仍然显示

发布于 2024-12-06 20:04:03 字数 887 浏览 1 评论 0原文

我在交互模式下使用 matplotlib 向用户显示一个绘图,帮助他们输入一系列变量。他们可以选择点击“?”显示该图,然后将重复提示输入变量。

如果该图仍在显示,我如何知道不重新绘制该图?

从表面上看,我有这个笨重的(伪)代码:

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

我可以使用什么(就 plt.gca()、fig 等而言)来确定是否需要重新绘制?我可以在某处查看状态吗?

非常感谢,

大卫

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与之呼应 2024-12-13 20:04:03

与 unutbu 的答案相同,您还可以检查给定的图形是否仍然打开,

import matplotlib.pyplot as plt

if plt.fignum_exists(<figure number>):
    # Figure is still opened
else:
    # Figure is closed

图形的图形编号位于 fig.number 中。

PS:请注意,figure(num=…)中的“数字”实际上可以是一个字符串:它显示在窗口标题中。但是,该图窗仍然具有数字的 number 属性。然而,原始的 string num 值可以与 fignum_exists() 一起使用(根据 Mark H 在评论中的说法,自 2015 年起)。

PPS:也就是说,subplots(…, num=) 可以使用给定的字符串数字正确恢复现有图形。因此,在 Matplotlib 的某些部分中,图形仍然通过其字符串编号来识别。

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.

残花月 2024-12-13 20:04:03
import matplotlib.pyplot as plt
if plt.get_fignums():
    # window(s) open
else:
    # no windows
import matplotlib.pyplot as plt
if plt.get_fignums():
    # window(s) open
else:
    # no windows
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文