Numpy pcolormesh:TypeError:C的尺寸与X和/或Y不兼容

elp*_*att 11 python numpy matplotlib

这段代码:

xedges = np.arange(self.min_spread - 0.5, self.max_spread + 1.5)
yedges = np.arange(self.min_span - 0.5, self.max_span + 1.5)
h, xe, ye = np.histogram2d(
    self.spread_values
    , self.span_values
    , [xedges, yedges]
)
fig = plt.figure(figsize=(7,3))
ax = fig.add_subplot(111)
x, y = np.meshgrid(xedges, yedges)
ax.pcolormesh(x, y, h)
Run Code Online (Sandbox Code Playgroud)

给出了这个错误:

TypeError: Dimensions of C (55, 31) are incompatible with X (56) and/or Y (32); see help(pcolormesh)
Run Code Online (Sandbox Code Playgroud)

如果有55x31箱,网格中的56x32箱边缘是否正确?

DrV*_*DrV 24

这可能看起来很神奇,但解释很简单......

以这种方式打印错误消息:

if not (numCols in (Nx, Nx - 1) and numRows in (Ny, Ny - 1)):
    raise TypeError('Dimensions of C %s are incompatible with'
                            ' X (%d) and/or Y (%d); see help(%s)' % (
                                C.shape, Nx, Ny, funcname))
Run Code Online (Sandbox Code Playgroud)

这里的要点是以C(行,列)顺序打印的形状,而X表示列和Y行.你应该有一个数组(31,55)来使它工作.

转置你的阵列,它停止抱怨.不可否认,错误信息令人惊讶.