EeMei Tang
EeMei Tang

Reputation: 53

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

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

Upvotes: 3

Views: 5341

Answers (1)

em_bis_me
em_bis_me

Reputation: 388

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)

Upvotes: 2

Related Questions