TypeError: Dimensions of C are incompatible with X Y

TypeError: Dimensions of C are incompatible with X Y

当我调用 plt.pcolormesh

时,Matplotlib 目前正在引发以下错误
TypeError: Dimensions of C (16, 1000) are incompatible with X (16) and/or Y (1000); see help(pcolormesh)

除非我遗漏了什么,这很可能是尺寸匹配?为什么会出现错误?

我在其他地方看到的问题与我自己的不同,所以我不知道该如何解决这个问题。

要求的代码:

def Colormap(lst):

    intensity = np.array(lst)

    x, y = intensity.shape

    x1 = range(0, x)
    y1 = range(0, y)

    x2,y2 = np.meshgrid(x1,y1)

    print x2,y2

    print intensity.shape

    plt.pcolormesh(x2,y2,intensity)
    plt.colorbar()
    plt.savefig('colormap.pdf', dpi = 1200)
    plt.show()

打印语句给出:

[[ 0  1  2 ..., 13 14 15]
 [ 0  1  2 ..., 13 14 15]
 [ 0  1  2 ..., 13 14 15]
 ..., 
 [ 0  1  2 ..., 13 14 15]
 [ 0  1  2 ..., 13 14 15]
 [ 0  1  2 ..., 13 14 15]] [[  0   0   0 ...,   0   0   0]
 [  1   1   1 ...,   1   1   1]
 [  2   2   2 ...,   2   2   2]
 ..., 
 [997 997 997 ..., 997 997 997]
 [998 998 998 ..., 998 998 998]
 [999 999 999 ..., 999 999 999]]

(16, 1000)

如我所料。我缺少一些非常基本的东西吗?谢谢。

问题是您正在更改尺寸(x 到 y,y 到 x),因此尺寸不正确。检查以下更改:

import matplotlib.pyplot as plt
import numpy as np

def Colormap(lst):

    intensity = np.array(lst)

    x, y = intensity.shape

    x1 = range(x+1) # changed this also
    y1 = range(y+1) # changed this also

    x2,y2 = np.meshgrid(x1,y1)

    print(x2.shape,y2.shape)

    print(intensity.shape)
    print(np.swapaxes(intensity,0,1).shape)
    plt.pcolormesh(x2,y2,np.swapaxes(intensity,0,1)) # Transpose of intensity
    plt.colorbar()
    plt.savefig('colormap.pdf', dpi = 1200)
    plt.show()

Colormap(np.random.randint(0,100,(16,1000)))

,结果是:

我必须进行转置才能使您的代码正常工作。