在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
查看: 46058|回复: 4

[解决] 使用python中的matplotlib 画图,show后关闭窗口,继续运行命令

[复制链接]
发表于 2013-3-14 23:36:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 caoshangfei 于 2013-3-14 23:44 编辑

在用python中的matplotlib 画图时,show()函数总是要放在最后,且它阻止命令继续往下运行,直到1.0.1版本才支持多个show()的使用。
想在显示图像后继续运行相关的处理命令,或者显示一副图像后关闭它,再显示第二幅图像。如下办法:

首先搜索到:
plt.close() will close current instance.
plt.close(2) will close figure 2
plt.close(plot1) will close figure with instance plot1
plt.close('all') will close all fiures
Found here.
Remember that plt.show() is a blocking function, so in the example code you used above,plt.close() isn't being executed until the window is closed, which makes it redundant.
You can use plt.ion() at the beginning of your code to make it non-blocking, although this has other implications.

搜索到:http://matplotlib.org/faq/usage_faq.html#what-is-interactive-mode

总结如下例子:
import matplotlib.pyplot as plt
import time
plt.ion() #开启interactive mode
x = np.linspace(0, 50, 1000)
plt.figure(1) # 创建图表1
plt.plot(x, np.sin(x))
plt.draw()
time.sleep(5)
plt.close(1)
plt.figure(2) # 创建图表2
plt.plot(x, np.cos(x))
plt.draw()
time.sleep(5)
print 'it is ok'

如果不需要关闭图表1,去掉plt.close(1),如果不需要redraw the current figure,那也可以去掉plt.draw()
发表于 2013-5-3 23:04:10 | 显示全部楼层
批处理程序,可以用pdf backend,直接把图表存到文件里。程序全部运行完之后一起看。
发表于 2016-7-17 18:58:17 | 显示全部楼层
这个可以解决。也可以存在pdf中。完了,一起看
发表于 2018-1-19 00:40:12 | 显示全部楼层
感謝分享
发表于 2018-12-28 16:15:17 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐 上一条 /2 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-4-28 07:43 , Processed in 0.026285 second(s), 9 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表