当我调用 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)))
,结果如下:
我必须进行转置才能让您的代码正常工作。
关于python - 类型错误 : Dimensions of C are incompatible with X Y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36483060/