scikit-hep / hist

Histogramming for analysis powered by boost-histogram
https://hist.readthedocs.io
BSD 3-Clause "New" or "Revised" License
123 stars 23 forks source link

hist can be installed and used in JupyterLite, but plotting with Matplotlib doesn't work #573

Open jpivarski opened 1 month ago

jpivarski commented 1 month ago

Code cell contents

import piplite
await piplite.install("mplhep")
await piplite.install("hist")
from hist import Hist
h = Hist.new.Reg(10, -5, 5).Double()
h

image

(good)

h.plot()
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[5], line 1
----> 1 h.plot()

File /lib/python3.11/site-packages/hist/basehist.py:479, in BaseHist.plot(self, overlay, *args, **kwargs)
    476 _project = _has_categorical or overlay is not None
    478 if self.ndim == 1 or (self.ndim == 2 and _project):
--> 479     return self.plot1d(*args, overlay=overlay, **kwargs)
    481 if self.ndim == 2:
    482     return self.plot2d(*args, **kwargs)

File /lib/python3.11/site-packages/hist/basehist.py:497, in BaseHist.plot1d(self, ax, overlay, **kwargs)
    486 def plot1d(
    487     self,
    488     *,
   (...)
    491     **kwargs: Any,
    492 ) -> Hist1DArtists:
    493     """
    494     Plot1d method for BaseHist object.
    495     """
--> 497     from hist import plot
    499     if self.ndim == 1:
    500         return plot.histplot(self, ax=ax, **_proc_kw_for_lw(kwargs))

File /lib/python3.11/site-packages/hist/plot.py:17
     15     import matplotlib.pyplot as plt
     16     from matplotlib import patches, transforms
---> 17     from mplhep.plot import Hist1DArtists, Hist2DArtists, hist2dplot, histplot
     18 except ModuleNotFoundError:
     19     print(  # noqa: T201
     20         "Hist requires mplhep to plot, either install hist[plot] or mplhep",
     21         file=sys.stderr,
     22     )

File /lib/python3.11/site-packages/mplhep/__init__.py:10
      6 import mplhep_data
      8 # Get styles directly, also available within experiment helpers.
      9 # Get helper functions
---> 10 from . import alice, atlas, cms, label, lhcb, plot
     11 from . import styles as style
     12 from ._tools import Config

File /lib/python3.11/site-packages/mplhep/alice.py:8
      5 import mplhep
      7 from . import label as label_base
----> 8 from ._compat import docstring
      9 from .label import lumitext
     11 # Log styles

File /lib/python3.11/site-packages/mplhep/_compat.py:3
      1 from __future__ import annotations
----> 3 from matplotlib import _docstring as docstring  # type: ignore[attr-defined]
      5 __all__ = ("docstring",)

ImportError: cannot import name '_docstring' from 'matplotlib' (/lib/python3.11/site-packages/matplotlib/__init__.py)

(bad)

Screenshot directly copied from JupyterLite

image

henryiii commented 1 month ago
await piplite.install("hist[plot]")

Hist can't ship with plotting dependencies because boost-histogram refuses to support named axes, so "hist" by itself stays very light.

henryiii commented 1 month ago

Ah, wait, mplhep is present. Looks like mplhep needs to update to stop using a private member of Matplotlib that seems to have been removed. @andrzejnovak?

jpivarski commented 1 month ago

Oh, you're right:

File /lib/python3.11/site-packages/mplhep/_compat.py:3
      1 from __future__ import annotations
----> 3 from matplotlib import _docstring as docstring  # type: ignore[attr-defined]

I had thought it was this: https://github.com/matplotlib/matplotlib/issues/26827, which is a symptom of a general installation issue (two installations of Matplotlib stomping on each other), but that line comes directly from mplhep/_compat.py...