matplotlib交互模式:确定图形窗口是否仍然显示
我在交互模式下使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与 unutbu 的答案相同,您还可以检查给定的图形是否仍然打开,
图形的图形编号位于
fig.number
中。PS:请注意,
figure(num=…)
中的“数字”实际上可以是一个字符串:它显示在窗口标题中。但是,该图窗仍然具有数字的number
属性。然而,原始的 stringnum
值可以与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
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 anumber
attribute which is numeric. The original stringnum
value can however be used withfignum_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.