首页 文章

Matplotlib动画未更新

提问于
浏览
1

我正在使用rplidar . 其旋转360度,每秒旋转100个样本(信号质量,角度和距离) . 使用Rplidar库我可以将这些数据作为多维数组获取([(15,90,300),(14,91,310)...]) . 现在我想将这些数据绘制为动画,而下面是代码 . 问题是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()

为了更好地理解这里来自传感器的测试数据(质量,角度,距离):

[(15, 276.359375, 873.25), (15, 260.203125, 843.0), (15, 261.28125, 811.0), (15, 262.3125, 800.5), (15, 263.375, 798.0), (15, 264.328125, 810.0), (15, 265.171875, 860.0), (15, 266.46875, 779.0), (15, 267.5, 777.75), (15, 268.375, 808.0), (15, 269.375, 809.25), (15, 270.5, 808.0), (15, 271.46875, 775.25), (15, 272.5625, 750.0), (15, 273.6875, 751.5), (15, 276.921875, 871.5), (15, 277.9375, 873.5), (15, 278.9375, 869.5), (15, 279.984375, 860.0), (15, 281.109375, 840.25), (15, 282.09375, 838.0), (15, 283.171875, 845.0), (15, 284.21875, 860.0), (15, 285.1875, 880.5), (15, 286.171875, 894.75), (15, 287.21875, 899.5), (15, 288.53125, 794.0), (15, 289.578125, 789.0), (15, 290.484375, 790.75), (15, 291.609375, 797.5), (15, 292.5, 830.25), (15, 293.53125, 861.75), (15, 294.46875, 871.75), (15, 295.484375, 874.25), (15, 296.625, 880.5), (15, 297.59375, 904.0), (15, 298.53125, 946.25), (15, 299.71875, 958.25), (15, 300.828125, 955.75), (15, 301.890625, 940.5), (15, 302.96875, 892.25), (15, 304.0, 884.0), (15, 305.15625, 896.0), (15, 306.0, 962.25), (15, 307.015625, 978.25), (15, 308.0625, 982.0), (15, 309.0625, 982.75), (15, 310.234375, 981.5), (15, 311.140625, 991.0), (15, 312.109375, 1081.75), (15, 313.1875, 1073.5), (15, 316.15625, 1261.75), (15, 317.109375, 1286.25), (15, 318.203125, 1304.75), (15, 319.171875, 1332.75), (15, 320.234375, 1354.75), (15, 321.21875, 1382.0), (15, 322.265625, 1401.5), (15, 323.65625, 1404.25), (15, 324.78125, 1382.25), (15, 325.828125, 1360.5), (15, 326.890625, 1342.5), (15, 328.015625, 1322.25), (15, 329.078125, 1307.25), (15, 330.171875, 1285.75), (15, 331.1875, 1270.75), (15, 332.328125, 1251.75), (15, 333.34375, 1240.25), (15, 334.40625, 1227.75), (15, 335.5, 1217.75), (15, 336.609375, 1202.25), (15, 337.65625, 1189.0), (15, 338.6875, 1179.0), (15, 339.75, 1167.5), (15, 340.828125, 1158.5), (15, 341.96875, 1148.5), (15, 342.96875, 1136.75), (15, 344.078125, 1131.75), (15, 345.140625, 1124.75), (15, 346.1875, 1117.75), (15, 347.546875, 1111.25), (15, 348.578125, 1105.5), (15, 349.625, 1097.25), (15, 350.765625, 1094.5)]

每个seceond我得到这些数据

1 回答

  • 1

    函数plt.show是阻塞的,这意味着它在数字打开时暂停代码 . 在你的代码中你有一个show命令,它停止该行的动画 . Addint参数block = False将阻止show停止,即plt.show(block = False) . 看看matplotlib中的this示例 . 我没有't have the lidar data hence I can' t运行你的代码 . 无论如何,您可以将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

相关问题