【发布时间】:2018-06-10 21:08:19
【问题描述】:
我正在尝试使用 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
【问题讨论】:
-
您在互联网上找到了一些代码,但您不知道它是如何工作的?你有没有花时间阅读 Matplotlib 文档?您是否排除了代码的任何部分?你怀疑是什么问题,你做了什么来验证?
标签: python python-3.x matplotlib