我想使用matplotlib绘制来自激光雷达的一些实时数据。因此,我使用plot.pause绘制数据,效果很好。
现在,我想添加一些按钮来缩放显示或将数据保存到文件中,但是我无法使其工作。我在此添加我的最后一个草稿代码(我用随机的x和y数据替换了激光雷达的东西,因此我不需要每次都启动激光雷达...)。我将“ gridsize”定义为要更改的参数,但是它不起作用(程序冻结或给出“分配前引用的局部变量'gridsize'”)。我真的不知道如何使它起作用,因此欢迎所有建议。
import matplotlib.pyplot as plt
import time
import random
from matplotlib.widgets import Button
# -- rest of the admin
gridsize = 4000
# -- button class
class Index(object):
def zoom_in(self,event):
gridsize -= 500
print("yolo")
fig,ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
callback = Index()
# -- main loop
for i in range(20):
# --- setup plt
fig.clf()
plt.axis([-gridsize,gridsize,-gridsize,gridsize])
plt.grid(which='both',linestyle='--')
# --- get sensor data (simulated)
x = random.sample(range(-5000,5000),50)
y = random.sample(range(-5000,50)
s = 10
plt.scatter(x,y,s)
# --- button stuff
# THIS NEEDS SOME WORK
axzoomIn = plt.axes([0.85,0.05,0.1,0.075])
bzoomIn = Button(axzoomIn,'+')
bzoomIn.on_clicked(callback.zoom_in)
# --- plot
plt.pause(0.05)
# -- this is the end
plt.show()