使用 Matplotlib 中的动画函数缓慢绘图,Python [英] Slow ploting using Animation function in Matplotlib, Python

查看:67
本文介绍了使用 Matplotlib 中的动画函数缓慢绘图,Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些天来,我需要您的帮助来解决这个问题.我可以绘制从我的手机蓝牙传输并由我的笔记本电脑的 COM 端口接收的串行数据.乍一看似乎还可以,但是最多可以每260毫秒(〜3 fps)绘制一次.但是手机每 100 毫秒发送一次数据.我很确定问题出在"plot"和"figure"命令,这使我感到困惑.如果有人可以更正我的代码,我将不胜感激:

I need your help for a problem that i'm dealing with it these days. I can plot a serial data which transfered from my cell phone Bluetooth and received by COM Port of my laptop. In the first glance it seems to be Ok, but at most it can plot every 260 ms (~3 fps). however the cellphone send data every 100 ms. I am pretty sure that the problem stems from "plot" and "figure" command that makes me confused. I appreciate if somebody can correct my code:

from Tkinter import *
import serial
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
ser = serial.Serial("COM4", baudrate=115200, timeout=0.1)
cnt=0
xComponent=[]
plt.ylim(0,30)
while (ser.inWaiting() == 0): # Wait here until there is data
    pass
def animate(i):

    BluetoothString = ser.readline()
    ser.flush()
    dataArray = BluetoothString.split(',')
    x = float(dataArray[2]) # we only need 3rd component
    xComponent.append(x)
    print xComponent
    ax1.clear()
    ax1.plot(xComponent)
    plt.ylim(0,25)
    global cnt
    if (cnt > 16): 
        xComponent.pop(0)
    else:
        cnt = cnt + 1

ani = animation.FuncAnimation(fig, animate, interval=0)
plt.show()

推荐答案

关于您的特殊情况,很难说什么,因为我们没有您正在使用的串行连接部分.

It's hard to say anything about your special case, since we do not have the serial connection part that you're using.

然而,如果这只是一个包含一些点的线图,那么在 matplotlib 中绘图应该比 3 fps 快得多.您可以直接尝试不要在每个迭代步骤重新绘制所有内容,而是绘制一次,然后仅使用 .set_data()

Plotting should however be much faster than 3 fps in matplotlib if this is only a line plot with some points in it. One thing you can directly try it not to replot everything at every iteration step, but plot it once and then only update the data using .set_data()

以下示例与您的代码紧密相关,并且在我的计算机上以90 fps的速度运行.所以也许您可以尝试一下,看看它是否有助于加快您的案子.

The following example is closely related to your code and runs with 90 fps on my machine. So maybe you try that one out and see if it helps speeding up your case.

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

fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)

cnt=0
xComponent=[]

line,  = ax1.plot([0], [0])
text = ax1.text(0.97,0.97, "", transform=ax1.transAxes, ha="right", va="top")

plt.ylim(0,25)
plt.xlim(0,100)
last_time = {0: time.time()}
def animate(i):

    if len(xComponent)>100:
        xComponent.pop(0)
    y = i % 25
    xComponent.append(y)

    line.set_data(range( len(xComponent) ) ,xComponent)
    new_time = time.time()
    text.set_text("{0:.2f} fps".format(1./(new_time-last_time[0])))
    last_time.update({0:new_time})


ani = animation.FuncAnimation(fig, animate, interval=0)
plt.show()

这篇关于使用 Matplotlib 中的动画函数缓慢绘图,Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆