Reputation: 769
I am trying to plot a 4D array using as 4th dimension the color. Here is a sample of my matrix:
[[ 4.216 0. 1. 0. ]
[ 5.36 0. 1. 0. ]
[ 5.374 0. 2. 0. ]
...,
[ 0.294 0. 1. 0. ]
[ 0.314 0. 2. 0. ]
[ 0.304 0. 1. 0. ]]
4th column only contains values 0, 1 and 2.
So when I try to plot it using this script:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(data[:,0],data[:,1],data[:,2], c=data[:,3], cmap=plt.hot())
plt.show()
I am getting this error:
TypeError: can't multiply sequence by non-int of type 'float'
Upvotes: 1
Views: 833
Reputation: 15349
This isn't a 4D array. It's a 2D array with 4 columns (the 2 dimensions could be referred to as "rows" and "columns"). But I see what you're trying to say—each row could be interpreted as describing a point in 4-dimensional space, with the fourth "dimension" being colour.
Two-dimensionality is actually the key to the problem. I suspect your data
variable is a numpy.matrix
rather than a vanilla numpy.array
. A matrix
is particular class of 2D-array that has various special properties, including the fact that a slice of it (for example, data[:, 0]
) is still a 2-dimensional matrix
object, whereas .scatter()
expects each argument to be a 1-D array.
The fix is to say:
data = numpy.asarray(data)
to convert your data from a matrix
to a normal array whose column slices will be 1-dimensional.
BTW: you probably meant to say cmap='hot'
. The call to plt.hot()
sets the default colormap (so your figure may look right, but there's a side effect) but it actually returns None
.
Upvotes: 2