如何修复“ ValueError:参数Z必须为二维”错误

时间:2019-11-08 08:21:45

标签: python

我试图绘制3d曲面穿过一组(X,Y,Z)3d点,然后引发ValueError(“参数Z必须是二维的。”)

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:参数Z必须为二维

1 个答案:

答案 0 :(得分:0)

您可以使用np.expand_dims()扩展Z的尺寸。 您尚未指定要扩展的轴。 因此,我给出了两个代码段。只是取消注释即可。

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)