【发布时间】:2019-07-19 11:18:45
【问题描述】:
我正在尝试构建一种电池表,其中我有一个程序可以收集电压样本并将其添加到数组中。我的想法是,当电池充满时我会收集大量数据,然后构建一个函数,将这些数据与最近 100 次左右的电压读数的平均值进行比较,因为每隔几秒就会添加新读数因为我不会打断这个过程。
我正在使用 matplotlib 来显示电压输出,到目前为止它工作正常:I posted an answer here on live changing graphs
电压函数如下所示:
pullData = open("dynamicgraph.txt","r").read() //values are stored here in another function
dataArray = pullData.split('\n')
xar = []
yar = []
averagevoltage = 0
for eachLine in dataArray:
if len(eachLine)>=19:
x,y = eachLine.split(',')
xar.append(np.int64(x)) //a datetime value
yar.append(float(y)) //the reading
ax1.clear()
ax1.plot(xar,yar)
ax1.set_ylim(ymin=25,ymax=29)
if len(yar) > 1:
plt.title("Voltage: " + str(yar [-1]) + " Average voltage: "+ str(np.mean(yar)))
我只是想知道获取数组最后 x 个数字的平均值的语法应该是什么样的?
if len(yar) > 100
#get average of last 100 values only
【问题讨论】:
-
numpy.average ? docs.scipy.org/doc/numpy-1.10.1/reference/generated/…
-
您可以通过
yar[-100:]获取数组的最后 100 项。然后,您可以通过np.mean(yar[-100:])获得平均值。 -
哦,这真的很简单,我什至有那个注释
str(yar [-1])。谢谢!