最大刻度数并滚动X

时间:2018-08-04 11:38:17

标签: python python-3.x matplotlib axis-labels

我想定义x轴上的最大刻度数,比方说8,这样绘图就不会像下面的图像一样:

enter image description here

此图随时间推移具有动画效果,x轴上的数据将“永远”增加。

但是我不想丢失旧数据,我想在x轴上滚动,也许像here

我已经尝试过了,但无法解决这种情况:

import requests as r
import time
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def data_gen(t=0):
    while True:
        t += int(time.time())
        yield t, int(r.get('https://qrng.anu.edu.au/API/jsonI.php?length=1&type=uint8&size=6&time=1533249361313').json()['data'][0])

def run(data):
    t, y = data
    xdata.append(t)
    ydata.append(y)
    x_labels.append(time.strftime('%H:%m:%S'))
    xmin, xmax = ax.get_xlim()
    ymin, ymax = ax.get_ylim()
    plt.xticks(xdata, x_labels, rotation=20)
    if t >= xmax or y >= ymax:
        if t >= xmax:
            ax.set_xlim(0, t+(t/10))
        if y >= ymax:
            ax.set_ylim(ymin, ymax+50)
        ax.figure.canvas.draw()
    line.set_data(xdata, ydata)
    return line,


fig, ax = plt.subplots()
line, = ax.plot([], [])
ax.grid()
xdata, ydata, x_labels = [0], [0], [time.strftime('%H:%m:%S')]

ax.set_ylim(0, 250)
ax.set_xlim(int(time.time()), int(time.time())+10)
line.set_data(xdata, ydata)

ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10,
                              repeat=False)
plt.show()

0 个答案:

没有答案