imshow 非均匀矩阵 bin 大小

imshow non unifrom matrix bin size

我正在尝试使用 imshow 创建图像,但矩阵中的 bin 不相等。 例如下面的矩阵

C = [[1,2,2],[2,3,2],[3,2,3]]

用于 X = [1,4,8]Y = [2,4,9] 我知道我可以只做 xticksyticks,但我希望轴相等..这意味着我需要构建 imshow 的正方形具有不同的大小。 可能吗?

这似乎是 pcolormesh 的工作。 来自 When to use imshow over pcolormesh:

Fundamentally, imshow assumes that all data elements in your array are to be rendered at the same size, whereas pcolormesh/pcolor associates elements of the data array with rectangular elements whose size may vary over the rectangular grid.

pcolormesh 将矩阵绘制为单元格,并将单元格的 x 和 y 坐标作为参数,这允许您以不同的大小绘制每个单元格。

我假设示例数据的 X 和 Y 是单元格的大小。所以我将它们转换为坐标:

xSize=[1,4,9]
ySize=[2,4,8]
x=np.append(0,np.cumsum(xSize)) # gives [ 0  1  5 13]
y=np.append(0,np.cumsum(ySize)) # gives [ 0  2  6 15]

然后,如果您想要与 imshow 类似的行为,则需要反转 y 轴。

c=np.array([[1,2,2],[2,3,2],[3,2,3]])
plt.pcolormesh(x,-y,c)

这给了我们: