• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

python matplotlib 循环不断绘制显示下张图

武飞扬头像
宇脩
帮助1

最近想在循环里添加绘制代码,实现不断刷新显示下一张图的效果。经过测试以下几种方式可实现。
1.plt.show()

import matplotlib.pyplot as plt

for i in range(500):
    plt.clf()   #清除上一次
    plt.scatter(i,i)    	#绘图...
    
    plt.show(block=False)   #显示
    #参数bloc默认为True,会阻塞窗口,需点击关闭才能显示下一张
    #If True block and run the GUI main loop until all figure windows are closed.
    #If False ensure that all figure windows are displayed and return immediately. In this case, you are responsible for ensuring that the event loop is running to have responsive figures.
    plt.pause(0.05)		#暂停秒数,延长显示时间

2.plt.draw()

import matplotlib.pyplot as plt

for i in range(500):
    plt.clf()   #清除上一次
    plt.scatter(i,i)    	#绘图...
    
    plt.draw()   #显示
    #如果不加pause无显示,猜想是显示太快看不见
    plt.pause(0.05)		#暂停秒数,延长显示时间

3.plt.ion()

import matplotlib.pyplot as plt

plt.ion()		#打开交互模式,plt绘图直接显示
for i in range(500):
    plt.clf()   #清除上一次
    plt.scatter(i,i)    	#绘图...
    
    plt.pause(0.05)		#暂停秒数,延长显示时间

另:

plt.cla() # 清除axes,即当前 figure 中的活动的axes,但其他axes保持不变。
plt.clf() # 清除当前 figure 的所有axes,但是不关闭这个 window,所以能继续复用于其他的 plot。
plt.close() # 关闭 window,如果没有指定,则指当前 window。

总结:
看源码好像 plt.show(block=False) 等同于 plt.ion() 效果。
如果注释掉 pause() 函数,plt.show(block=False) 和 plt.ion()显示窗口全白卡住,plt.draw()则无显示。
以上方法可能遇到窗口无响应,以及存在终止进程仍不断显示问题。

参考资料

https://blog.csdn.net/tz_zs/article/details/81393098
https://blog.csdn.net/tz_zs/article/details/81393098

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgafkhf
系列文章
更多 icon
同类精品
更多 icon
继续加载