gpt4 book ai didi

python - 如何修复 "ValueError: Argument Z must be 2-dimensional"错误

转载 作者:行者123 更新时间:2023-12-05 05:07:53 26 4
gpt4 key购买 nike

我正在尝试绘制通过一组 (X,Y,Z) 3D 点的 3D 表面,但出现 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:参数 Z 必须是二维的

最佳答案

您可以使用 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)

关于python - 如何修复 "ValueError: Argument Z must be 2-dimensional"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58762868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com