mplhep icon indicating copy to clipboard operation
mplhep copied to clipboard

TypeError when plotting 2d histograms with flow="show"

Open alexander-held opened this issue 2 years ago • 2 comments

Plotting a 2d histogram with flow="show" results in errors like this:

TypeError: Dimensions of C (12, 12) should be one smaller than X(11) and Y(11) while using shading='flat' see help(pcolormesh)

I am using mplhep==0.3.28.

Reproducer:

import hist
import numpy as np

h = hist.Hist.new.Regular(10, 0, 1, name="x").Regular(10, 0, 1, name="y").Double()
h.fill(x=np.random.random(100), y=np.random.random(100))
h.plot(flow="show")

Full trace:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[25], line 6
      4 h = hist.Hist.new.Regular(10, 0, 1, name="x").Regular(10, 0, 1, name="y").Double()
      5 h.fill(x=np.random.random(100), y=np.random.random(100))
----> 6 h.plot(flow="show")

File [...]/lib/python3.9/site-packages/hist/basehist.py:400, in BaseHist.plot(self, overlay, *args, **kwargs)
    397     return self.plot1d(*args, overlay=overlay, **kwargs)
    399 if self.ndim == 2:
--> 400     return self.plot2d(*args, **kwargs)
    402 raise NotImplementedError("Please project to 1D or 2D before calling plot")

File [...]/lib/python3.9/site-packages/hist/basehist.py:452, in BaseHist.plot2d(self, ax, **kwargs)
    446 """
    447 Plot2d method for BaseHist object.
    448 """
    450 from hist import plot
--> 452 return plot.hist2dplot(self, ax=ax, **_proc_kw_for_lw(kwargs))

File [...]/lib/python3.9/site-packages/mplhep/plot.py:753, in hist2dplot(H, xbins, ybins, labels, cbar, cbarsize, cbarpad, cbarpos, cbarextend, cmin, cmax, ax, flow, **kwargs)
    750 X, Y = np.meshgrid(xbins, ybins)
    752 kwargs.setdefault("shading", "flat")
--> 753 pc = ax.pcolormesh(X, Y, H, vmin=cmin, vmax=cmax, **kwargs)
    755 if x_axes_label:
    756     ax.set_xlabel(x_axes_label)

File [...]/lib/python3.9/site-packages/matplotlib/__init__.py:1446, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
   1443 @functools.wraps(func)
   1444 def inner(ax, *args, data=None, **kwargs):
   1445     if data is None:
-> 1446         return func(ax, *map(sanitize_sequence, args), **kwargs)
   1448     bound = new_sig.bind(ax, *args, **kwargs)
   1449     auto_label = (bound.arguments.get(label_namer)
   1450                   or bound.kwargs.get(label_namer))

File [...]/lib/python3.9/site-packages/matplotlib/axes/_axes.py:6218, in Axes.pcolormesh(self, alpha, norm, cmap, vmin, vmax, shading, antialiased, *args, **kwargs)
   6215 shading = shading.lower()
   6216 kwargs.setdefault('edgecolors', 'none')
-> 6218 X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
   6219                                     shading=shading, kwargs=kwargs)
   6220 coords = np.stack([X, Y], axis=-1)
   6221 # convert to one dimensional array, except for 3D RGB(A) arrays

File [...]/lib/python3.9/site-packages/matplotlib/axes/_axes.py:5749, in Axes._pcolorargs(self, funcname, shading, *args, **kwargs)
   5747 if shading == 'flat':
   5748     if (Nx, Ny) != (ncols + 1, nrows + 1):
-> 5749         raise TypeError(f"Dimensions of C {C.shape} should"
   5750                         f" be one smaller than X({Nx}) and Y({Ny})"
   5751                         f" while using shading='flat'"
   5752                         f" see help({funcname})")
   5753 else:    # ['nearest', 'gouraud']:
   5754     if (Nx, Ny) != (ncols, nrows):

TypeError: Dimensions of C (12, 12) should be one smaller than X(11) and Y(11) while using shading='flat' see help(pcolormesh)

alexander-held avatar Aug 07 '23 13:08 alexander-held