How do I fix " ValueError: Argument Z must be 2-dimensional" error

Question:

I’m trying to plot a 3D surface passes through a set of (X,Y,Z) 3D point and I got raise ValueError("Argument Z must be 2-dimensional.")

ax = Axes3D(fig)
X = df.index.values
Y = np.where(df['trans_count'].values>0,np.ma.log(df['trans_count'].values), 0)

X, Y = np.meshgrid(X, Y)

Z = np.where(df['gpu_trans_count'].values>0, np.log(df['gpu_trans_count'].values), 0)

print(X.shape) #(37,37)
print(Y.shape) #(37,37)
print(Z.shape) #(37,)

ax.plot_surface(X, Y, Z)

ValueError: Argument Z must be 2-dimensional

Asked By: EeMei Tang

||

Answers:

You can expand the dimensions of Z using np.expand_dims().
You have not specified which axis to expand.
Hence I have given code snippets for both. Just uncomment as your wish.

ax = Axes3D(fig)
X = df.index.values


Y = np.where(df['trans_count'].values>0,np.ma.log(df['trans_count'].values), 0)

X, Y = np.meshgrid(X, Y)


Z = np.where(df['gpu_trans_count'].values>0, np.log(df['gpu_trans_count'].values), 0)

#If you want to expand the x dimensions
#From (n,) to (1,n)
Z=np.expand_dims(Z,axis=0)
print(Z.shape) #(1,37)

# If you want to expand the y dim
# from (n,) to (n,1)
#Z=np.expand_dims(Z,axis=1)
#print(Z.shape) #(37,1)

print(X.shape) #(37,37)
print(Y.shape) #(37,37)



ax.plot_surface(X, Y, Z)
Answered By: em_bis_me
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.