我想知道为什么 y 轴的顺序是随机的(见截图)。我只是从带有值对的文本文件中绘制 x 和 y 值,并希望 y 轴从最小值开始并以最大值结束。在我的情况下,y 轴图的中间有最小的数字 (3)。我该如何纠正这个错误?txt文件:1,52,33,54,35,76,37,58,39,510,3代码:import matplotlib.pyplot as pltimport matplotlib.animation as animationfrom matplotlib import stylestyle.use('fivethirtyeight')fig = plt.figure()ax1 = fig.add_subplot(1,1,1)def animate(i): graph_data = open('example.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) # 1000 ms = 1 sec (updating)plt.show()我发现编辑以下两行代码有帮助!!!之前: xs.append(x) ys.append(y)之后: xs.append(float(x)) ys.append(float(y))
添加回答
举报
0/150
提交
取消