简体   繁体   中英

Numpy pcolormesh: TypeError: Dimensions of C are incompatible with X and/or Y

This code:

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)

Gives this error:

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

If there are 55x31 bins, isn't 56x32 bin edges in the grid correct?

This may look very amazing, but the explanation is simple...

The error message is printed in this way:

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))

The point here is that the shape of C is printed in (rows, cols) order, whereas X represents columns and Y rows. You should have an array (31, 55) to make it work.

Transpose your array, and it stops complaining. Admittedly, the error message is rather surprising.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM