我正在尝试使用Matplotlib(following this exact tutorial)使用Python进行实时图表。但是我的代码中X轴的顺序是错误的,因为它从1开始,到10,然后是11,然后回到2,3,4,5 ...
我刚刚复制了教程中的代码和数字,但是我得到了不同的结果。以下是它为我显示的图表:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
style.use('fivethirtyeight')
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
def animate(i):
graph_data = open("animation_file.txt", 'r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x, y = line.split(',')
xs.append(x)
ys.append(y)
ax1.clear()
ax1.plot(xs, ys)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
这是“animation_file.txt”:
1, 5
2, 3
3, 4
4, 7
5, 4
6, 3
7, 6
8, 7
9, 4
10,4
11, 2