python - 类型错误 : Dimensions of C are incompatible with X Y

标签 python matplotlib colormap

当我调用 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)))

,结果如下:

Transpose of array

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

关于python - 类型错误 : Dimensions of C are incompatible with X Y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36483060/

相关文章:

python - 如何轻松地将颜色图应用于线图?

python - 在字典列表中查找最大值

python - 如何在 SciPy 中创建对角稀疏矩阵

python - 绘制python对象属性和对象存储在列表中

python-3.x - 如何在 matplotlib 图像图中实现 Fiji "HiLo"颜色图,以标记曝光不足和曝光过度的像素

opencv - openCV的colormap转换是线性的吗?

python - PyTorch 在张量和 numpy 数组之间的转换 : the addition operation

python - 3D 图和 3D 直方图子图

python - 误差条图的 Matplotlib set_data

python - 如何在 python 中使用 alpha 和 beta 参数绘制 Gamma 分布