Violin

import holoviews as hv

from hv_anndata.interface import register

register()

hv.extension("bokeh")
import anndata as ad
import pooch
import scanpy as sc
EXAMPLE_DATA = pooch.create(
    path=pooch.os_cache("scverse_tutorials"),
    base_url="doi:10.6084/m9.figshare.22716739.v1/",
)
EXAMPLE_DATA.load_registry_from_doi()
samples = {
    "s1d1": "s1d1_filtered_feature_bc_matrix.h5",
    "s1d3": "s1d3_filtered_feature_bc_matrix.h5",
}
adatas = {}

for sample_id, filename in samples.items():
    path = EXAMPLE_DATA.fetch(filename)
    sample_adata = sc.read_10x_h5(path)
    sample_adata.var_names_make_unique()
    adatas[sample_id] = sample_adata

adata = ad.concat(adatas, label="sample")
adata.obs_names_make_unique()
print(adata.obs["sample"].value_counts())
adata
Downloading file 's1d1_filtered_feature_bc_matrix.h5' from 'doi:10.6084/m9.figshare.22716739.v1/s1d1_filtered_feature_bc_matrix.h5' to '/home/docs/.cache/scverse_tutorials'.
/home/docs/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/scanpy/readwrite.py:247: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.
  adata = adata.copy()
Downloading file 's1d3_filtered_feature_bc_matrix.h5' from 'doi:10.6084/m9.figshare.22716739.v1/s1d3_filtered_feature_bc_matrix.h5' to '/home/docs/.cache/scverse_tutorials'.
sample
s1d1    8785
s1d3    8340
Name: count, dtype: int64
/home/docs/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/scanpy/readwrite.py:247: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.
  adata = adata.copy()
/tmp/ipykernel_2017/3491096135.py:13: UserWarning: Observation names are not unique. To make them unique, call `.obs_names_make_unique`.
  adata = ad.concat(adatas, label="sample")
AnnData object with n_obs × n_vars = 17125 × 36601
    obs: 'sample'
    layers: None
adata.var["mt"] = adata.var_names.str.startswith("MT-")
adata.var["ribo"] = adata.var_names.str.startswith(("RPS", "RPL"))
adata.var["hb"] = adata.var_names.str.contains("^HB[^(P)]")

sc.pp.calculate_qc_metrics(
    adata, qc_vars=["mt", "ribo", "hb"], inplace=True, log1p=True
)
sc.pl.violin(
    adata,
    ["n_genes_by_counts", "total_counts", "pct_counts_mt"],
    jitter=0.4,
    multi_panel=True,
)
../_images/c24988a0f6541f662885b0534d904a2b47338534f47a06ea6bba38c83bb596b0.png
violins = [
    hv.Violin(adata, vdims=i).opts(
        ylabel="Value",
        title=i.split(".")[-1],  # drop the 'obs.'
        show_grid=True,
        ylim=(0, None),
    )
    for i in ["obs.n_genes_by_counts", "obs.total_counts", "obs.pct_counts_mt"]
]
hv.Layout(violins).opts(axiswise=True)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/core/dimension.py:1381, in Dimensioned._repr_mimebundle_(self, include, exclude)
   1374 def _repr_mimebundle_(self, include=None, exclude=None):
   1375     """Resolves the class hierarchy for the class rendering the
   1376     object using any display hooks registered on Store.display
   1377     hooks.  The output of all registered display_hooks is then
   1378     combined and returned.
   1379 
   1380     """
-> 1381     return Store.render(self)

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/core/options.py:1433, in Store.render(cls, obj)
   1431 data, metadata = {}, {}
   1432 for hook in hooks:
-> 1433     ret = hook(obj)
   1434     if ret is None:
   1435         continue

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/ipython/display_hooks.py:340, in pprint_display(obj)
    338 if not ip.display_formatter.formatters['text/plain'].pprint:
    339     return None
--> 340 return display(obj, raw_output=True)

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/ipython/display_hooks.py:311, in display(obj, raw_output, **kwargs)
    309 elif isinstance(obj, (Layout, NdLayout, AdjointLayout)):
    310     with option_state(obj):
--> 311         output = layout_display(obj)
    312 elif isinstance(obj, (HoloMap, DynamicMap)):
    313     with option_state(obj):

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/ipython/display_hooks.py:205, in display_hook.<locals>.wrapped(element)
    203 try:
    204     max_frames = OutputSettings.options['max_frames']
--> 205     mimebundle = fn(element, max_frames=max_frames)
    206     if mimebundle is None:
    207         return {}, {}

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/ipython/display_hooks.py:274, in layout_display(layout, max_frames)
    271     max_frame_warning(max_frames)
    272     return None
--> 274 return render(layout)

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/ipython/display_hooks.py:76, in render(obj, **kwargs)
     73 if renderer.fig == 'pdf':
     74     renderer = renderer.instance(fig='png')
---> 76 return renderer.components(obj, **kwargs)

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/plotting/renderer.py:396, in Renderer.components(self, obj, fmt, comm, **kwargs)
    394 embed = (not (dynamic or streams or self.widget_mode == 'live') or config.embed)
    395 if embed or config.comms == 'default':
--> 396     return self._render_panel(plot, embed, comm)
    397 return self._render_ipywidget(plot)

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/plotting/renderer.py:403, in Renderer._render_panel(self, plot, embed, comm)
    401 doc = Document()
    402 with config.set(embed=embed):
--> 403     model = plot.layout._render_model(doc, comm)
    404 if embed:
    405     return render_model(model, comm)

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/panel/viewable.py:775, in Viewable._render_model(self, doc, comm)
    773 if comm is None:
    774     comm = state._comm_manager.get_server_comm()
--> 775 model = self.get_root(doc, comm)
    777 if self._design and self._design.theme.bokeh_theme:
    778     doc.theme = self._design.theme.bokeh_theme

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/panel/layout/base.py:332, in Panel.get_root(self, doc, comm, preprocess)
    328 def get_root(
    329     self, doc: Document | None = None, comm: Comm | None = None,
    330     preprocess: bool = True
    331 ) -> Model:
--> 332     root = super().get_root(doc, comm, preprocess)
    333     # ALERT: Find a better way to handle this
    334     if hasattr(root, 'styles') and 'overflow-x' in root.styles:

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/panel/viewable.py:705, in Renderable.get_root(self, doc, comm, preprocess)
    703 wrapper = self._design._wrapper(self)
    704 if wrapper is self:
--> 705     root = self._get_model(doc, comm=comm)
    706     if preprocess:
    707         self._preprocess(root)

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/panel/layout/base.py:316, in Panel._get_model(self, doc, root, parent, comm)
    314 root = root or model
    315 self._models[root.ref['id']] = (model, parent)
--> 316 objects, _ = self._get_objects(model, [], doc, root, comm)
    317 props = self._get_properties(doc)
    318 props[self._property_mapping['objects']] = objects

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/panel/layout/base.py:298, in Panel._get_objects(self, model, old_objects, doc, root, comm)
    296 else:
    297     try:
--> 298         child = pane._get_model(doc, root, model, comm)
    299     except RerenderError as e:
    300         if e.layout is not None and e.layout is not self:

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/panel/pane/holoviews.py:494, in HoloViews._get_model(self, doc, root, parent, comm)
    492     plot = self.object
    493 else:
--> 494     plot = self._render(doc, comm, root)
    496 plot.pane = self
    497 backend = plot.renderer.backend

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/panel/pane/holoviews.py:588, in HoloViews._render(self, doc, comm, root)
    585     if comm:
    586         kwargs['comm'] = comm
--> 588 return renderer.get_plot(self.object, **kwargs)

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/plotting/bokeh/renderer.py:70, in BokehRenderer.get_plot(self_or_cls, obj, doc, renderer, **kwargs)
     63 @bothmethod
     64 def get_plot(self_or_cls, obj, doc=None, renderer=None, **kwargs):
     65     """Given a HoloViews Viewable return a corresponding plot instance.
     66     Allows supplying a document attach the plot to, useful when
     67     combining the bokeh model with another plot.
     68 
     69     """
---> 70     plot = super().get_plot(obj, doc, renderer, **kwargs)
     71     if plot.document is None:
     72         plot.document = Document() if self_or_cls.notebook_context else curdoc()

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/plotting/renderer.py:239, in Renderer.get_plot(self_or_cls, obj, doc, renderer, comm, **kwargs)
    236     defaults = [kd.default for kd in plot.dimensions]
    237     init_key = tuple(v if d is None else d for v, d in
    238                      zip(plot.keys[0], defaults, strict=None))
--> 239     plot.update(init_key)
    240 else:
    241     plot = obj

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/plotting/plot.py:958, in DimensionedPlot.update(self, key)
    956 def update(self, key):
    957     if len(self) == 1 and key in (0, self.keys[0]) and not self.drawn:
--> 958         return self.initialize_plot()
    959     item = self.__getitem__(key)
    960     self.traverse(lambda x: setattr(x, '_updated', True))

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/plotting/bokeh/plot.py:954, in LayoutPlot.initialize_plot(self, plots, ranges)
    951     continue
    953 shared_plots = list(passed_plots) if self.shared_axes else None
--> 954 subplots = subplot.initialize_plot(ranges=ranges, plots=shared_plots)
    955 nsubplots = len(subplots)
    957 modes = {sp.sizing_mode for sp in subplots
    958          if sp.sizing_mode not in (None, 'auto', 'fixed')}

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/plotting/bokeh/plot.py:1103, in AdjointLayoutPlot.initialize_plot(self, ranges, plots)
   1101     else:
   1102         passed_plots = plots + adjoined_plots
-> 1103         adjoined_plots.append(subplot.initialize_plot(ranges=ranges, plots=passed_plots))
   1104 self.drawn = True
   1105 return adjoined_plots or [None]

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/plotting/bokeh/element.py:2220, in ElementPlot.initialize_plot(self, ranges, plot, plots, source)
   2218 if self.autorange:
   2219     self._setup_autorange()
-> 2220 self._init_glyphs(plot, element, ranges, source)
   2221 if not self.overlaid:
   2222     self._update_plot(key, plot, style_element)

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/plotting/bokeh/element.py:2548, in CompositeElementPlot._init_glyphs(self, plot, element, ranges, source, data, mapping, style)
   2546 if None in (data, mapping):
   2547     style = self.style[self.cyclic_index]
-> 2548     data, mapping, style = self.get_data(element, ranges, style)
   2550 keys = glyph_order(dict(data, **mapping), self._draw_order)
   2552 source_cache = {}

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/plotting/bokeh/stats.py:551, in ViolinPlot.get_data(self, element, ranges, style)
    549 if element.kdims:
    550     key = tuple(d.pprint_value(k) for d, k in zip(element.kdims, key, strict=None))
--> 551 kde, line, segs, bars, scatter = self._kde_data(
    552     element, g, key, split_dim, split_cats, **kwargs
    553 )
    554 for k, v in segs.items():
    555     seg_data[k] += v

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/plotting/bokeh/stats.py:488, in ViolinPlot._kde_data(self, element, el, key, split_dim, split_cats, **kwargs)
    486 elif self.inner == 'box':
    487     xpos = (*key, 0)
--> 488     q1, q2, q3, upper, lower, _ = self._box_stats(values)
    489     segments['x'].append(xpos)
    490     segments['y0'].append(lower)

File ~/.local/share/hatch/env/virtual/hv-anndata/STk7F69l/docs/lib/python3.13/site-packages/holoviews/plotting/bokeh/stats.py:164, in BoxWhiskerPlot._box_stats(self, vals)
    162     q1, q2, q3 = 0, 0, 0
    163     upper, lower = 0, 0
--> 164 outliers = vals[(vals > upper) | (vals < lower)]
    166 if is_cupy:
    167     return (q1.item(), q2.item(), q3.item(), upper.item(),
    168             lower.item(), cupy.asnumpy(outliers))

TypeError: unsupported operand type(s) for |: 'NumpyExtensionArray' and 'NumpyExtensionArray'
:Layout
   .Violin.I   :Violin   (n_genes_by_counts)
   .Violin.II  :Violin   (total_counts)
   .Violin.III :Violin   (pct_counts_mt)