基于python的动画图形

2024-06-01 13:14:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从加速计中读取变量并生成图形,但图形不显示。为什么?我的代码中需要更改什么?如果我在读取变量之前放入最后两个字符串,则生成图形但不读取数据。 救命啊,求你了

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

Tags: import图形figpltaxfloatconnlightblue