Roberto
Roberto

Reputation: 2194

numpy vstack throwing dimension error

I am monitoring the serial port and attempting to plot data in Matplotlib as it arrives. Because the data arrives at irregular intervals, I am using an approach to append data - similar to this thread.

Here is my code:

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)] ))

I am having trouble getting the dimensioning right, as I get the following error:

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

Please help to identify the syntax error.

Running on Python 2.6, Numpy 1.8, Windows 7.

Upvotes: 1

Views: 5965

Answers (1)

CT Zhu
CT Zhu

Reputation: 54330

Has to be the same 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')])

Sidenote: is it for some construction project? Looks fun.

Upvotes: 6

Related Questions