我需要从加速度计读取变量并构建图形,但图形不显示。为什么?我的代码需要更改什么?如果我在读取变量之前放置最后2个字符串,图形构建但数据未读取。 求救!
import lightblue
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
w = 1
conn = None
def animate(i):
global conn
global w, ax
if w:
sock = lightblue.socket()
sock.bind(("", 0))
sock.listen(1)
lightblue.advertise("EchoService", sock, lightblue.RFCOMM)
print "Advertised and listening on channel %d..." % sock.getsockname()[1]
conn, addr = sock.accept()
print "Connected by", addr
w = 0
line = conn.recv(1024)
angles = line.split(", ")
if len(angles) == 3:
ax = float(angles[0])
ay = float(angles[1])
az = float(angles[2])
print ("ax : " + str(ax))
print ("ay : " + str(ay))
print ("az : " + str(az))
xs = []
ys = []
xs.append(ax)
ys.append(ay)
ax1.clear()
ax1.plot(xs, ys)
ani = animation.FuncAnimation(fig, animate)
plt.show()