猿问

如何在 y 轴(matplotlob (P 2.7) 正确获得正确顺序

我想知道为什么 y 轴的顺序是随机的(见截图)。我只是从带有值对的文本文件中绘制 x 和 y 值,并希望 y 轴从最小值开始并以最大值结束。在我的情况下,y 轴图的中间有最小的数字 (3)。我该如何纠正这个错误?


txt文件:


1,5

2,3

3,5

4,3

5,7

6,3

7,5

8,3

9,5

10,3

代码:


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('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))

http://img3.mukewang.com/61013d4d000112d006420540.jpg

慕盖茨4494581
浏览 206回答 1
1回答

BIG阳

您在行上指定了 FiveThirtyEight 样式style.use('fivethirtyeight')只需将其删除即可使用正常的绘图样式。
随时随地看视频慕课网APP

相关分类

Python
我要回答