numpy vstack投掷维度错误

时间:2013-12-30 01:58:05

标签: python numpy

我正在监视串口并尝试在Matplotlib到达时绘制数据。因为数据是以不规则的间隔到达的,所以我使用的方法来附加数据 - 类似于this thread

这是我的代码:

data = np.zeros(shape=(1,1), dtype=[('millis',float),('temperature_Celsius',float),('relative_humidity',float),('setpoint',float),('relay_status',float)])
print data # gives a 1-row, 5-element tuple: [[(0.0, 0.0, 0.0, 0.0, 0.0)]]

# append the new row
# throws error regarding array dimensions
data = np.vstack(( data, [(1,2,3,4,5)] ))

我无法正确获取尺寸,因为我收到以下错误:

ValueError: all the input array dimensions except for the concatenation axis must match exactly

请帮助识别语法错误。

在Python 2.6,Numpy 1.8,Windows 7上运行。

1 个答案:

答案 0 :(得分:5)

必须是dtype

>>> d2=asarray([(1.,2.,3.,4.,5.)],dtype=[('millis',float),('temperature_Celsius',float),('relative_humidity',float),('setpoint',float),('relay_status',float)])
>>> d2=asarray([(1.,2.,3.,4.,5.)],dtype=data.dtype) #or this
>>> d2
array([(1.0, 2.0, 3.0, 4.0, 5.0)], 
      dtype=[('millis', '<f8'), ('temperature_Celsius', '<f8'), ('relative_humidity', '<f8'), ('setpoint', '<f8'), ('relay_status', '<f8')])
>>> vstack((data,d2))
array([[(0.0, 0.0, 0.0, 0.0, 0.0)],
       [(1.0, 2.0, 3.0, 4.0, 5.0)]], 
      dtype=[('millis', '<f8'), ('temperature_Celsius', '<f8'), ('relative_humidity', '<f8'), ('setpoint', '<f8'), ('relay_status', '<f8')])

旁注:是否适用于某些建筑项目?看起来很有趣。

相关问题