Cate Daniel
Cate Daniel

Reputation: 744

python plot 3d color map

I have 3 1-D arrays, for the X values, Y values and Z values. I want to make a 2-d plot with X vs Y and have Z in color.

However every time I try I run I get

AttributeError: 'list' object has no attribute 'shape'

currently I have:

X=np.array(X)
Y=np.array(Y)
Z=np.array(Z)

fig = pyplot.figure()
ax = fig.add_subplot(111)
p = ax.scatter(X,Y,Z)

I have also tried

fig, ax = pyplot.figure()
p = ax.pcolor(X,Y,Z,cmap = cm.RdBu)
cb = fig.colorbar(p,ax=ax)

both give me the same error.

Upvotes: 2

Views: 2751

Answers (1)

Hooked
Hooked

Reputation: 88278

The documentation of plt.scatter expects input like:

matplotlib.pyplot.scatter(x, y, s=20, ...)

Which is not (x,y,z). You were setting s, the size of the points, as your Z value. To give a "Z" value of color, pass it as the c parameter:

import numpy as np
import pylab as plt

# Example points that use a color proportional to the radial distance
N = 2000
X = np.random.normal(size=N)
Y = np.random.normal(size=N)
Z = (X**2+Y**2)**(1/2.0)

plt.scatter(X,Y,c=Z,linewidths=.1)
plt.axis('equal')
plt.show()

enter image description here

Upvotes: 2

Related Questions