2

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

@bobruels44 What new error? I don't see another comment or edit. Either way, a completely new question should be posed as that - a new question.
Sorry still sorting through them, I noticed a more detailed edit, and I'm just quickly trying stupid things.
I'm still getting the attribute error, even though I'm using numpy arrays.
It's impossible to diagnose the problem without a more complete example. If you edit your question to a minimal example that produces the error we can take a look at it.
This basically is the entire plotting routine. The rest of the code is building those arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.