使用序列matplolib 3d设置数组元素

时间:2018-06-29 08:58:12

标签: python matplotlib 3d

我有以下奇怪的问题。我正在尝试绘制3D图。没关系。我想将投影放在图的表面上。我的代码看起来像这样

fig = plt.figure(figsize = (10,8))
ax = fig.add_subplot(111, projection='3d')

ax.plot_trisurf(xarr, yarr, zarr, cmap=cm.coolwarm, linewidth=50)
ax.set_xlabel('\nMAE', fontsize = 14, linespacing = 1.5)
ax.set_ylabel('\nDIFF', fontsize = 14)
ax.set_zlabel('\nCounts', fontsize = 14, linespacing=1.5)

cset = ax.contour(np.array(xx), np.array(yy), 
                  np.array(zz), zdir='z', offset=-100, cmap=cm.coolwarm)
cset = ax.contour(xx, yy, np.array(zz), zdir='x', offset=-40, cmap=cm.coolwarm)
cset = ax.contour(xx, yy, np.array(zz), zdir='y', offset=40, cmap=cm.coolwarm)

plt.show()

不起作用的是以下行

cset = ax.contour(np.array(xx), np.array(yy), 
np.array(zz), zdir='z', offset=-100, cmap=cm.coolwarm)

这里是向量

np.array(yy)
array([ 21,   6,  30,   3,  27,  61,  56,  52,  38,  14,  33,  12,  93,
       129,  36,  11,  59,   9, 113,  18,  26,   8,  17,  10,  29,   2,
         4,  16,  85,  55,  58,  45,   7,  15,  19,   5,  69,  57,  20,
       158,  86, 118,  31, 107,  34,  92,  32,  28,  66,  54,  87,  25,
        13,  99,  23,  60,  81,  24,  72, 123,  49,  63,  64,  71,  67,
        40,  46,  48,  47,  95,  43, 159,  22,  37,  35, 105, 104,  42,
       128,  53,  76,  75, 103,  65, 136, 144,  68,  77, 278,  98, 111,
       114,  41,  84, 154,  62, 214, 124, 210,   1, 155,  79,  74,  80,
        83, 318,  70, 120,  78,  44,  88,  73,  50, 110, 178,  51, 134,
       106, 189,  91, 411, 135, 138, 143, 127, 122, 160,  94, 109, 226,
       140, 117, 100, 133, 191, 141,  89, 288, 126,  97, 653, 121, 172,
       161,  39,  96,  90, 130, 169, 142,  82, 132, 156, 137, 119, 102,
       112, 188, 610, 115, 146, 234, 108, 150, 182, 170, 116, 223, 139,
       197, 194, 241, 131, 181, 183, 152, 147, 250, 203, 165, 199, 218,
       334, 167, 151, 384, 163, 162, 125, 148, 233, 354, 184, 168, 186,
       180, 166, 369, 192, 101, 201, 157, 164, 419, 239], dtype=int64)

np.array(xx)
array([   500.,   1500.,   2500.,   3500.,   4500.,   5500.,   6500.,
         7500.,   8500.,   9500.,  10500.,  11500.,  12500.,  13500.,
        14500.,  15500.,  16500.,  17500.,  18500.,  19500.,  20500.,
        21500.,  22500.,  23500.,  24500.,  25500.,  26500.,  27500.,
        28500.])

zz的尺寸

np.array(zz).shape
(205,29)

应当如此。任何人都可以猜测出什么问题了吗?完整的错误是

ValueError: setting an array element with a sequence.

很遗憾,我无法发布数据,但我希望错误与数据的结构有关...

预先感谢,翁贝托

1 个答案:

答案 0 :(得分:1)

如果您在contour3d example中检查XYZ的形状,则应 找出它们是相同的。
因此,为了使您的代码正常工作, 在创建绘图之前,应使用xxyynp.meshgrid扩展到2d数组。

xx, yy = np.meshgrid(xx, yy)