Python 如何使用3个阵列制作二维彩色栅格

Python 如何使用3个阵列制作二维彩色栅格,python,arrays,matplotlib,multidimensional-array,plot,Python,Arrays,Matplotlib,Multidimensional Array,Plot,我有三个长度相等的数组x、y和z。x和y阵列是栅格的x轴和y轴。z数组将决定网格块的颜色。比如说, 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] 这是很容易作出三维绘图了这样的 ax.plot_trisurf(x, y, z, cmap=cm.RdYlGn) 或 但我在找类似的东西 另一个问题

我有三个长度相等的数组x、y和z。x和y阵列是栅格的x轴和y轴。z数组将决定网格块的颜色。比如说,

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]
这是很容易作出三维绘图了这样的

ax.plot_trisurf(x, y, z, cmap=cm.RdYlGn)

但我在找类似的东西

另一个问题是,我的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()

Yo首先必须对整个网格进行插值(例如,检查scipy的
griddata
),然后使用matplotlib中的
imshow
heatmap
。为了实现这一目标,我们有很多例子。
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()