Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 817
elplatt Avatar asked Jul 16 '14 22:07

elplatt


1 Answers

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.

like image 69
DrV Avatar answered Nov 07 '22 06:11

DrV