我有三个长度相等的数组 x、y 和 z。 x 和 y 数组是网格的 x 轴和 y 轴。 z 数组将确定网格 block 的颜色。例如,
x = [10, 10, 10, 20, 20, 20, 30, 30, 30]
y = [10, 20, 30, 10, 20, 30, 10, 20, 30]
z = [99, 54, 32, 67, 71, 88, 100, 15, 29]
很容易制作 3D 绘图,例如
ax.plot_trisurf(x, y, z, cmap=cm.RdYlGn)
或
ax.bar3d(x, y, [0] * len(x), 100, 100, z, cmap=cm.RdYlGn)
但我正在寻找类似 this grid 的东西
另一个问题是我的 z 数组的生成方式不是按索引排序的。所以我的 x、y 和 z 数组可能如下所示。
x = [30, 10, 20, 20, 30, 10, 10, 30, 20]
y = [10, 20, 30, 10, 30, 30, 10, 20, 20]
z = [100, 54, 88, 67, 29, 32, 99, 15, 71]
最佳答案
这是针对您的具体问题的一个小示例。我正在将您的 x 和 y 索引转换为数组中查看您的数据的位置 - 您可能需要自己更改它。
import numpy as np
import matplotlib.pyplot as plt
x = [10, 10, 10, 20, 20, 20, 30, 30, 30]
y = [10, 20, 30, 10, 20, 30, 10, 20, 30]
z = [99, 54, 32, 67, 71, 88, 100, 15, 29]
# Convert x/y to indices. This only works if you have a rigid grid (which seems to be the case, but you might have to change the transform for your case)
x = (np.array(x)/10 - 1).astype(int)
y = (np.array(y)/10 - 1).astype(int)
# Create the image. Default color is black
z_im = np.zeros((x.max() + 1, y.max() + 1, 3))
# Go through z and color acoordingly -- only gray right now
for i, v in enumerate(z):
z_im[x[i], y[i]] = (v, v, v)
fig, ax = plt.subplots()
ax.imshow(z_im)
plt.show()
关于python - 如何用 3 个数组制作 2D 彩色网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43661111/