python中带有固定窗口的实时图形

python中带有固定窗口的实时图形,python,matplotlib,Python,Matplotlib,我正在尝试用python构建一个活动图,在一个带有固定绘图窗口的图中绘制随机数 绘图窗口的宽度为20个样本。对于第21个样本,第一个样本将从右侧显示。这是我的密码。我想不出为什么它没有标绘 import random import time import matplotlib.pyplot as plt import matplotlib.animation as animation fig = plt.figure() #creating a subplot ax1 = fig.add_s

我正在尝试用python构建一个活动图,在一个带有固定绘图窗口的图中绘制随机数 绘图窗口的宽度为20个样本。对于第21个样本,第一个样本将从右侧显示。这是我的密码。我想不出为什么它没有标绘

import random 
import time
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
#creating a subplot 
ax1 = fig.add_subplot(1,1,1)
xs = []
ys = []
iter = 0
def animate(i,xs,ys,iter):
    while True:
        iter = iter+1
        xs.append(iter)
        ys.append(round(random.uniform(-120,20),2))
#I want only 20 data points on the plot i.e the plot window will be only showing 20 samples at a time 
        x = xs[-20:]
        y = ys[-20:]
        ax1.clear()
        ax1.plot(x, y)
        ax1.set_ylim([-120,20])
        plt.xlabel('Value')
        plt.ylabel('Time')
        plt.title('Live Graph')
        time.sleep(1)   

ani = animation.FuncAnimation(fig, animate, fargs = (xs,ys,iter), interval=1000) 
plt.show()
看看这本书

第二个参数是一个函数,对于每个连续帧重复调用,每次调用都会更新图形。函数
animate
是一个无限循环,因此执行线程永远不会返回到
FuncAnimation
。作为起点,尝试以下方法:

随机导入
导入时间
将matplotlib.pyplot作为plt导入
将matplotlib.animation导入为动画
图=plt.图()
ax1=图add_子批次(1,1,1)
xs=[]
ys=[]
行,=ax1.绘图(xs,ys)
plt.xlabel('值')
plt.ylabel(“时间”)
plt.title(‘活动图’)
定义动画(帧、xs、ys):
追加(帧)
附加(圆形(随机均匀(-120,20),2))
x=xs[-20:]
y=ys[-20:]
行。设置扩展数据(x)
行。设置数据(y)
ax1.设置\u xlim(最小值(x)-1,最大值(x)+1)
ax1.设置y最小值(y)-1,最大值(y)+1
ax1.set_xticks(列表(范围(最小(x)、最大(x)+1)))
回程线
ani=animation.FuncAnimation(图,animate,fargs=(xs,ys),interval=100)
plt.show()
查看

第二个参数是一个函数,对于每个连续帧重复调用,每次调用都会更新图形。函数
animate
是一个无限循环,因此执行线程永远不会返回到
FuncAnimation
。作为起点,尝试以下方法:

随机导入
导入时间
将matplotlib.pyplot作为plt导入
将matplotlib.animation导入为动画
图=plt.图()
ax1=图add_子批次(1,1,1)
xs=[]
ys=[]
行,=ax1.绘图(xs,ys)
plt.xlabel('值')
plt.ylabel(“时间”)
plt.title(‘活动图’)
定义动画(帧、xs、ys):
追加(帧)
附加(圆形(随机均匀(-120,20),2))
x=xs[-20:]
y=ys[-20:]
行。设置扩展数据(x)
行。设置数据(y)
ax1.设置\u xlim(最小值(x)-1,最大值(x)+1)
ax1.设置y最小值(y)-1,最大值(y)+1
ax1.set_xticks(列表(范围(最小(x)、最大(x)+1)))
回程线
ani=animation.FuncAnimation(图,animate,fargs=(xs,ys),interval=100)
plt.show()

问题是什么?无法找出为什么不绘图问题是什么?无法找出为什么不绘图如何将plt.xticks(min(x),max(x)+1,1.0)放入此代码中?如何将plt.xticks(min(x),max(x)+1,1.0放入此代码中?