我一直在尝试使用MatplotLib模块中的FuncAnimation绘制实时数据(不断更新的数据)。当我运行程序时,该图不显示。该程序没有给出任何错误,但是在我运行该程序时显示以下消息:“图形大小576x396,带有0个轴”
这是我的代码,用于绘制实时数据:
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.animation import FuncAnimation
%matplotlib inline
plt.style.use('seaborn')
def animate(i):
df = pd.read_csv('data2.csv')
x = df['x_value']
y1 = df['total1']
y2 = df['total2']
plt.cla()
plt.plot(x, y1, label='Line1')
plt.plot(x, y2, label = 'Line2')
plt.legend(loc='upper left')
plt.tight_layout()
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
plt.tight_layout()
plt.show()
这是我为程序创建的csv文件:
import random
import csv
import time
import pandas as pd
x_value = 0
total1 = 10000
total2 = 10000
fieldnames = ['x_value','total1','total2']
with open('data2.csv', 'w') as csv_file:
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
csv_writer.writeheader()
while True:
with open('data2.csv','a') as csv_file:
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
info = {
'x_value': x_value,
'total1' : total1,
'total2' : total2
}
csv_writer.writerow(info)
print(x_value, total1, total2)
x_value += 1
total1 = total1 + random.randint(-6,8)
total2 = total2 + random.randint(9,11)
time.sleep(1)
任何建议将不胜感激。
答案 0 :(得分:0)
消息<Figure size 576x396 with 0 Axes>
显示了图形的保存位置。删除plt.show()将显示图。