python - 使用序列 matplotlib 3d 设置数组元素

标签 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.

不幸的是,我无法发布数据,但我希望错误与数据的结构有关......

提前致谢,翁贝托

最佳答案

如果您检查 contour3d example 中的 XYZ 的形状, 你应该 发现它们是相同的。
因此,为了使您的代码正常工作,您 在创建绘图之前,应使用 np.meshgridxxyy 扩展为二维数组。

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

关于python - 使用序列 matplotlib 3d 设置数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51097971/

相关文章:

python - 如何拆分 numpy 数组并对拆分数组执行某些操作 [Python]

python - 如何设置 Django 在 Travis CI 上的 PostgreSQL 上运行测试?

opengl - OpenGL 中 glTexParameter 的范围是什么?

java - 3D 编程视锥体剔除

python - Python中的多处理内存错误

python - 获取元组列表中元素元组的最大值

python - 如何根据 xtick 标签控制条形图中特定列的颜色?

python - 如何使用 matplotlib 和 SVM 算法向图形(数据集)添加边界?

python - python中的饼图雷达图(披萨片)

opengl - 绘制 NURBS 曲线时节点的含义?