Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
alexander-held opened this issue Aug 7, 2023 · 2 comments · Fixed by #487
Closed

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

alexander-held opened this issue Aug 7, 2023 · 2 comments · Fixed by #487

Comments

@alexander-held
Copy link
Member

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)
@andrzejnovak
Copy link
Member

@Ming-Yan

@Ming-Yan
Copy link
Contributor

Ming-Yan commented Aug 8, 2023

Thanks @alexander-held for finding this bug, fixed in #437 PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants