[英]matplotlib interactive mode: determine if figure window is still displayed
我在交互模式下使用matplotlib向用户显示一个帮助他们输入一系列变量的图。 他们可以选择点击“?” 显示此图,然后将重复提示变量。
如果它仍在显示,我怎么知道不重新绘制这个图?
从表面上看,我有这个笨重的(伪ish)代码:
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(),无花果等...)来确定我是否需要重新绘制? 我可以检查某个地方的状态吗?
非常感谢,
大卫
与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
属性,它是数字:原始字符串num
值不能与使用fignum_exists()
PPS:也就是说, subplots(…, num=<string num>)
正确地恢复了具有给定字符串编号的现有图形。 因此,数字在Matplotlib的某些部分仍然以其字符串编号而为人所知(但fignum_exists()
不使用此类字符串)。
import matplotlib.pyplot as plt
if plt.get_fignums():
# window(s) open
else:
# no windows
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.