1D和ND绘图之间的差异

时间:2012-11-08 21:44:52

标签: python numpy matplotlib

我已经在Matplotlib上进行了一段时间的绘图并注意到一些绘图技术(如3D绘图和其他绘图技术)要求数据存在于尺寸超过1D的数组中。例如,如果我有1D阵列X,Y,Z,那么我将无法在3D图中绘制它们。但是,如果我将相同的阵列重塑为2D或任何ND,然后我可以在3D中绘制它们。我的问题是,为什么你认为这会发生?更重要的是,重塑和1D阵列之间存在差异(就其数据而言)?

1 个答案:

答案 0 :(得分:1)

让我们调查ax.contour。有example in the docs

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
print(X.shape, Y.shape, Z.shape)
# ((120, 120), (120, 120), (120, 120))
cset = ax.contour(X, Y, Z)
ax.clabel(cset, fontsize=9, inline=1)

plt.show()

enter image description here

print语句显示ax.contour可以接受2D输入。 如果我们要将XY数组更改为1D数组:

X, Y, Z = axes3d.get_test_data(0.05)
X = X.reshape(-1)
Y = Y.reshape(-1)
print(X.shape, Y.shape, Z.shape)

然后我们得到

((14400,), (14400,), (120, 120))

作为形状,并引发TypeError

TypeError: Length of x must be number of columns in z,
and length of y must be number of rows.

所以似乎别无选择。 ax.contour期望2D数组。

相关问题