且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 pyserial 和 matplotlib 实时绘图

更新时间:2023-02-20 15:04:34

好吧,这会读取您的字符串并将数字转换为浮点数.我想您可以根据需要进行调整.

Well, this reads your string and converts the numbers to floats. I assume you'll be able to adapt this as needed.

import numpy as np
import pylab as plt

str = '''>21 21 0 
>
>41 41 0.5
>
>73 73 1
>
>2053 2053 5
>
>2084 2084 5.5
>
>2125 2125 6'''
nums = np.array([[float(n) for n in sub[1:].split(' ') if len(n)>0] for sub in str.splitlines() if len(sub)>1])

fig = plt.figure(0)
ax = plt.subplot(2,1,1)
ax.plot(nums[:,0], nums[:,1], 'k.')
ax = plt.subplot(2,1,2)
ax.plot(nums[:,0], nums[:,2], 'r+')
plt.show()