Python 3.x Matplotlib动画未更新

Python 3.x Matplotlib动画未更新,python-3.x,matplotlib,Python 3.x,Matplotlib,我用的是激光雷达。它可以旋转360度,每秒提供100多个样本(信号质量、角度和距离)。使用Rplidar库,我可以将这些数据提取为多维数组([(15,90300),(14,91310)…])。现在我想把这些数据画成动画,下面是代码。问题是grap没有更新。如果你能帮忙,我将非常高兴。非常感谢您抽出时间 from rplidar import RPLidar, RPLidarException import matplotlib.pyplot as plt import matplotlib.an

我用的是激光雷达。它可以旋转360度,每秒提供100多个样本(信号质量、角度和距离)。使用Rplidar库,我可以将这些数据提取为多维数组([(15,90300),(14,91310)…])。现在我想把这些数据画成动画,下面是代码。问题是grap没有更新。如果你能帮忙,我将非常高兴。非常感谢您抽出时间

from rplidar import RPLidar, RPLidarException
import matplotlib.pyplot as plt
import matplotlib.animation as animation

lidar = RPLidar('/dev/ttyUSB0')

info = lidar.get_info()
print(info)

health = lidar.get_health()
print(health)

fig = plt.figure()

ax = fig.add_subplot(1, 1, 1, projection='polar')


def run(i):
    try:
        angle = []
        distance = []
        for i, scan in enumerate(lidar.iter_scans()):

            for d in scan:  # d[0] : Quality of the measurement
                # if 0< d[1] <200:     #d[1] : Angle of the measurement
                #    print(d[2]/10)   #d[2] : Distance of the measurement '''
                angle.append(0.0174533 * d[1])  #angle to radian
                distance.append(d[2])

            print('angle:', angle)
            ax.scatter(angle, distance, s=5)
            angle.clear()
            distance.clear()
    except KeyboardInterrupt:
        print("Stopping...")
        lidar.stop()
        lidar.stop_motor()
        lidar.disconnect()


ani = animation.FuncAnimation(fig, run, interval=50)
plt.show()

每次我得到这些数据时,plt.show函数都会阻塞,这意味着它会在图形打开时停止代码。在代码中有一个show命令,该命令将在该行停止动画。另外,参数block=False将防止停止显示,即plt.show(block=False)。看看matplotlib中的示例。我没有激光雷达数据,因此无法运行您的代码。无论如何,您可以将plt.show移动到动画函数调用下方,或者添加block=False参数来解决问题。建议采用前者。包括示例代码

   data = [(15, 184.484375, 1740.25), (15, 190.015625, 1337.75), (15, 191.9375, 941.0), (15, 193.28125, 951.5), (15, 194.6875, 916.0), (15, 196.21875, 806.25), (15, 197.859375, 739.25), (15, 199.3125, 732.0), (15, 200.625, 731.25), (15, 202.0, 730.25), (15, 203.71875, 644.75), (15, 205.3125, 618.25), (15, 206.96875, 588.5), (15, 208.265625, 581.5), (15, 210.28125, 487.0), (15, 211.609375, 461.25), (15, 213.4375, 450.25), (15, 214.734375, 445.25), (15, 215.953125, 443.75), (15, 217.578125, 443.0), (15, 218.71875, 446.5), (15, 220.046875, 451.0), (15, 221.5, 451.0), (15, 223.1875, 451.25), (15, 224.640625, 445.25), (15, 226.078125, 425.75), (15, 227.359375, 408.5), (15, 229.171875, 387.25), (15, 230.390625, 396.0), (15, 231.875, 389.75), (15, 233.28125, 384.75), (15, 234.828125, 376.5), (15, 236.53125, 368.25), (15, 237.703125, 370.75), (15, 239.28125, 371.0), (15, 240.328125, 372.5), (15, 242.109375, 366.0), (15, 243.609375, 367.75), (15, 244.859375, 365.75), (15, 246.296875, 366.75), (15, 247.8125, 365.0), (15, 251.1875, 408.25), (15, 252.6875, 406.5), (15, 254.0625, 405.0), (15, 255.515625, 403.0), (15, 256.953125, 401.0), (15, 258.625, 401.5), (15, 259.96875, 400.25), (15, 261.171875, 400.5), (15, 262.875, 400.5), (15, 264.09375, 400.25), (15, 265.625, 400.25), (15, 266.59375, 401.25), (15, 268.140625, 401.5), (15, 269.84375, 403.0), (15, 271.21875, 404.5), (15, 272.1875, 406.0), (15, 273.71875, 406.5), (15, 275.15625, 409.5), (15, 276.453125, 411.5), (15, 278.15625, 414.5), (15, 282.21875, 423.5), (15, 283.765625, 428.5), (15, 288.09375, 227.25), (15, 289.71875, 218.75), (15, 291.4375, 213.0), (15, 293.28125, 209.5), (15, 293.9375, 207.0), (15, 296.34375, 204.75), (15, 297.75, 203.5), (15, 299.15625, 203.5), (15, 300.578125, 203.5), (15, 301.984375, 203.75), (15, 303.390625, 205.5), (15, 304.796875, 207.0), (15, 306.140625, 209.5), (15, 307.546875, 212.75), (15, 308.953125, 216.75), (15, 313.953125, 479.25), (15, 315.546875, 482.5), (15, 316.859375, 489.0), (13, 318.25, 500.0), (15, 320.578125, 546.25), (15, 322.1875, 537.75), (15, 323.5, 534.5), (15, 325.125, 532.75), (15, 326.5, 536.0), (15, 327.78125, 544.25), (15, 329.6875, 459.25), (15, 331.390625, 448.75), (15, 332.765625, 444.75), (15, 334.25, 443.0), (15, 335.65625, 447.0), (15, 336.890625, 455.5), (15, 340.4375, 589.75), (15, 341.78125, 587.75), (15, 343.21875, 584.75), (15, 344.578125, 583.75), (15, 345.984375, 581.0), (15, 347.453125, 580.75), (15, 348.828125, 578.0), (15, 350.265625, 577.0), (15, 351.6875, 575.75)]
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()

ax = fig.add_subplot(1, 1, 1, projection='polar')


def run(i):
    try:
        angle = []
        distance = []
        for i, scan in enumerate(data):
            angle.append(0.0174533 * scan[1])
            distance.append(scan[2])

            print('angle:', angle)
            ax.scatter(angle, distance, s=5)
    except KeyboardInterrupt:
        print("Stopping...")


ani = animation.FuncAnimation(fig, run, interval=50)
plt.show()
第二次编辑:我会这样做:

from numpy import array, zeros
data = [(15, 184.484375, 1740.25), (15, 190.015625, 1337.75), (15, 191.9375, 941.0), (15, 193.28125, 951.5), (15, 194.6875, 916.0), (15, 196.21875, 806.25), (15, 197.859375, 739.25), (15, 199.3125, 732.0), (15, 200.625, 731.25), (15, 202.0, 730.25), (15, 203.71875, 644.75), (15, 205.3125, 618.25), (15, 206.96875, 588.5), (15, 208.265625, 581.5), (15, 210.28125, 487.0), (15, 211.609375, 461.25), (15, 213.4375, 450.25), (15, 214.734375, 445.25), (15, 215.953125, 443.75), (15, 217.578125, 443.0), (15, 218.71875, 446.5), (15, 220.046875, 451.0), (15, 221.5, 451.0), (15, 223.1875, 451.25), (15, 224.640625, 445.25), (15, 226.078125, 425.75), (15, 227.359375, 408.5), (15, 229.171875, 387.25), (15, 230.390625, 396.0), (15, 231.875, 389.75), (15, 233.28125, 384.75), (15, 234.828125, 376.5), (15, 236.53125, 368.25), (15, 237.703125, 370.75), (15, 239.28125, 371.0), (15, 240.328125, 372.5), (15, 242.109375, 366.0), (15, 243.609375, 367.75), (15, 244.859375, 365.75), (15, 246.296875, 366.75), (15, 247.8125, 365.0), (15, 251.1875, 408.25), (15, 252.6875, 406.5), (15, 254.0625, 405.0), (15, 255.515625, 403.0), (15, 256.953125, 401.0), (15, 258.625, 401.5), (15, 259.96875, 400.25), (15, 261.171875, 400.5), (15, 262.875, 400.5), (15, 264.09375, 400.25), (15, 265.625, 400.25), (15, 266.59375, 401.25), (15, 268.140625, 401.5), (15, 269.84375, 403.0), (15, 271.21875, 404.5), (15, 272.1875, 406.0), (15, 273.71875, 406.5), (15, 275.15625, 409.5), (15, 276.453125, 411.5), (15, 278.15625, 414.5), (15, 282.21875, 423.5), (15, 283.765625, 428.5), (15, 288.09375, 227.25), (15, 289.71875, 218.75), (15, 291.4375, 213.0), (15, 293.28125, 209.5), (15, 293.9375, 207.0), (15, 296.34375, 204.75), (15, 297.75, 203.5), (15, 299.15625, 203.5), (15, 300.578125, 203.5), (15, 301.984375, 203.75), (15, 303.390625, 205.5), (15, 304.796875, 207.0), (15, 306.140625, 209.5), (15, 307.546875, 212.75), (15, 308.953125, 216.75), (15, 313.953125, 479.25), (15, 315.546875, 482.5), (15, 316.859375, 489.0), (13, 318.25, 500.0), (15, 320.578125, 546.25), (15, 322.1875, 537.75), (15, 323.5, 534.5), (15, 325.125, 532.75), (15, 326.5, 536.0), (15, 327.78125, 544.25), (15, 329.6875, 459.25), (15, 331.390625, 448.75), (15, 332.765625, 444.75), (15, 334.25, 443.0), (15, 335.65625, 447.0), (15, 336.890625, 455.5), (15, 340.4375, 589.75), (15, 341.78125, 587.75), (15, 343.21875, 584.75), (15, 344.578125, 583.75), (15, 345.984375, 581.0), (15, 347.453125, 580.75), (15, 348.828125, 578.0), (15, 350.265625, 577.0), (15, 351.6875, 575.75)]
data = array(data)
data[:, 0] *= 0.0174533

import matplotlib.pyplot as plt
import matplotlib.animation as animation

# init figure
fig, ax = plt.subplots(subplot_kw = dict(projection = 'polar'))
buffer = zeros((20, 2)) # make buffer of some size
p, = ax.plot(*buffer.T, '.', markersize = 20) # get the line
ymax = data.max(0)[1]
ymin = data.min(0)[1]
ax.set(**dict(ylim = (ymin, ymax))) # set the limits

while True:
    for idx, scan in enumerate(data):
        buffer[idx % len(buffer)] = scan[:2] # fill buffer
        p.set_data(*buffer.T)
        fig.canvas.draw() # draw
        fig.canvas.flush_events() # deal with resize
fig.canvas.show() # show 

就我个人而言,我不太喜欢funcAnim的节目。尝试添加plt.show(block=False)。或者你可以直接更新对象。我真的很抱歉。我真的不明白你的意思。我是python新手。你能解释一下吗@GlobalTraveler如果你能在代码中发布一些测试数据,我会看一看。请看一下我代码的底部。我已经添加了扫描数据。非常感谢你抽出时间。我真的很感激@GlobalTraveler更新了上面的评论,并根据您输入的数据进行了调整。我没有访问激光雷达的东西,安装它没有工作,第一次尝试,但上面的例子工作。非常感谢。。有意思。。示例数据也在这里工作。但我不知道为什么ridar数据不起作用。我现在真的很困惑。给你写了第二个例子。对我来说,这样更有意义。我不太喜欢动画功能。你能把我的建议标为解决方案吗?
from numpy import array, zeros
data = [(15, 184.484375, 1740.25), (15, 190.015625, 1337.75), (15, 191.9375, 941.0), (15, 193.28125, 951.5), (15, 194.6875, 916.0), (15, 196.21875, 806.25), (15, 197.859375, 739.25), (15, 199.3125, 732.0), (15, 200.625, 731.25), (15, 202.0, 730.25), (15, 203.71875, 644.75), (15, 205.3125, 618.25), (15, 206.96875, 588.5), (15, 208.265625, 581.5), (15, 210.28125, 487.0), (15, 211.609375, 461.25), (15, 213.4375, 450.25), (15, 214.734375, 445.25), (15, 215.953125, 443.75), (15, 217.578125, 443.0), (15, 218.71875, 446.5), (15, 220.046875, 451.0), (15, 221.5, 451.0), (15, 223.1875, 451.25), (15, 224.640625, 445.25), (15, 226.078125, 425.75), (15, 227.359375, 408.5), (15, 229.171875, 387.25), (15, 230.390625, 396.0), (15, 231.875, 389.75), (15, 233.28125, 384.75), (15, 234.828125, 376.5), (15, 236.53125, 368.25), (15, 237.703125, 370.75), (15, 239.28125, 371.0), (15, 240.328125, 372.5), (15, 242.109375, 366.0), (15, 243.609375, 367.75), (15, 244.859375, 365.75), (15, 246.296875, 366.75), (15, 247.8125, 365.0), (15, 251.1875, 408.25), (15, 252.6875, 406.5), (15, 254.0625, 405.0), (15, 255.515625, 403.0), (15, 256.953125, 401.0), (15, 258.625, 401.5), (15, 259.96875, 400.25), (15, 261.171875, 400.5), (15, 262.875, 400.5), (15, 264.09375, 400.25), (15, 265.625, 400.25), (15, 266.59375, 401.25), (15, 268.140625, 401.5), (15, 269.84375, 403.0), (15, 271.21875, 404.5), (15, 272.1875, 406.0), (15, 273.71875, 406.5), (15, 275.15625, 409.5), (15, 276.453125, 411.5), (15, 278.15625, 414.5), (15, 282.21875, 423.5), (15, 283.765625, 428.5), (15, 288.09375, 227.25), (15, 289.71875, 218.75), (15, 291.4375, 213.0), (15, 293.28125, 209.5), (15, 293.9375, 207.0), (15, 296.34375, 204.75), (15, 297.75, 203.5), (15, 299.15625, 203.5), (15, 300.578125, 203.5), (15, 301.984375, 203.75), (15, 303.390625, 205.5), (15, 304.796875, 207.0), (15, 306.140625, 209.5), (15, 307.546875, 212.75), (15, 308.953125, 216.75), (15, 313.953125, 479.25), (15, 315.546875, 482.5), (15, 316.859375, 489.0), (13, 318.25, 500.0), (15, 320.578125, 546.25), (15, 322.1875, 537.75), (15, 323.5, 534.5), (15, 325.125, 532.75), (15, 326.5, 536.0), (15, 327.78125, 544.25), (15, 329.6875, 459.25), (15, 331.390625, 448.75), (15, 332.765625, 444.75), (15, 334.25, 443.0), (15, 335.65625, 447.0), (15, 336.890625, 455.5), (15, 340.4375, 589.75), (15, 341.78125, 587.75), (15, 343.21875, 584.75), (15, 344.578125, 583.75), (15, 345.984375, 581.0), (15, 347.453125, 580.75), (15, 348.828125, 578.0), (15, 350.265625, 577.0), (15, 351.6875, 575.75)]
data = array(data)
data[:, 0] *= 0.0174533

import matplotlib.pyplot as plt
import matplotlib.animation as animation

# init figure
fig, ax = plt.subplots(subplot_kw = dict(projection = 'polar'))
buffer = zeros((20, 2)) # make buffer of some size
p, = ax.plot(*buffer.T, '.', markersize = 20) # get the line
ymax = data.max(0)[1]
ymin = data.min(0)[1]
ax.set(**dict(ylim = (ymin, ymax))) # set the limits

while True:
    for idx, scan in enumerate(data):
        buffer[idx % len(buffer)] = scan[:2] # fill buffer
        p.set_data(*buffer.T)
        fig.canvas.draw() # draw
        fig.canvas.flush_events() # deal with resize
fig.canvas.show() # show