matplotlib实时图

时间:2019-05-17 23:15:44

标签: python matplotlib animation

我正在尝试创建一个实时图形,该图形从python文本文件中写入的信息实时更新,每次运行代码时都会遇到问题,但图形窗口会打开,但始终冻结并说没有回应,有人可以帮我解决这个问题吗?以下是我的代码

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def animate(i):
    pullData = open("example.txt","r").read()
    dataArray = pullData.split('\n')
    xar = []
    yar = []
    for eachLine in dataArray:
        if len(eachLine)>1:
            x,y = eachLine.split(',')
            xar.append(int(x))
            yar.append(int(y))
    ax1.clear()
    ax1.plot(xar,yar)




ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()

count = 14
import random
while(count > 200):
    with open("example.txt", "a") as text_file:
        count+=1
        text_file.write(str(count) + "," + str(random.randint(1,101))) 
        text_file.write("\n")

0 个答案:

没有答案