# Stacked histogram

**URL:** <https://discourse.holoviz.org/t/stacked-histogram/6205>\
**Category:** hvPlot\
**Tags:** holoviews\
**Created:** [October 13, 2023, 4:12pm UTC](https://discourse.holoviz.org/t/stacked-histogram/6205 "2023-10-13T16:12:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tupui](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/tupui/32/5678_2.png) [@tupui](https://discourse.holoviz.org/u/tupui)\
**Post date:** [October 13, 2023, 4:12pm UTC](https://discourse.holoviz.org/t/stacked-histogram/6205/1 "2023-10-13T16:12:21Z")

</div>

Anyone has a recipe to produce stacked histograms?

If I try the following

`df.hvplot.hist(stacked=True, color=colors_hex)`

I get:

`WARNING:param.main: Stacking for histograms is not yet implemented in holoviews. Use bar plots if stacking is required.`

My dataframe is a simple 9 columns by 3k rows and I want to get something like this:

 ![Screenshot 2023-10-13 at 18.10.25](https://discourse.holoviz.org/uploads/default/original/2X/4/44bf9ce0093de38c610d01919a272899de87f213.png)

This is the output from seaborn with:

```python
ax = sns.histplot(
    df,
    multiple="stack",
    stat="probability",
    palette= colors_hex,
    common_bins=True,
    common_norm=True,
    bins=n_bins,
    legend=False,
    ax=ax,
)

```

---

<div class="post-metadata">

**Author:** ![Hoxbro](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/hoxbro/32/3590_2.png) [@Hoxbro](https://discourse.holoviz.org/u/Hoxbro)\
**Post date:** [October 13, 2023, 8:11pm UTC](https://discourse.holoviz.org/t/stacked-histogram/6205/2 "2023-10-13T20:11:59Z")

</div>

To be honest, I don’t think the functionality is there in HoloViews (yet!). There is this [feature request](https://github.com/holoviz/holoviews/issues/5482). We can go down into pure Bokeh with Hooks and do something like this:

 ![image](https://discourse.holoviz.org/uploads/default/original/2X/8/8acbd3aa814eb9533ee252a68d48c2a0ff134d7b.png)

```python
import hvplot.pandas # noqa: F401
import numpy as np
import pandas as pd
import panel as pn

def stacked_hist(plot, element):
    offset = 0
    for r in plot.handles["plot"].renderers:
        r.glyph.bottom = "bottom"

        data = r.data_source.data
        new_offset = data["top"] + offset
        data["top"] = new_offset
        data["bottom"] = offset * np.ones_like(data["top"])
        offset = new_offset

    plot.handles["plot"].y_range.end = max(offset) * 1.1
    plot.handles["plot"].y_range.reset_end = max(offset) * 1.1

df = pd.DataFrame(
    np.random.normal(1, 4, (1000, 4)) * np.arange(1, 5), columns=list("ABCD")
)
plot = df.hvplot.hist().opts(hooks=[stacked_hist]) + df.hvplot.hist()

pn.panel(plot).servable()

```

Note as it is a bit late, a mistake could have sneaked in when making the code, but the fundamentals should be there.

---

<div class="post-metadata">

**Author:** ![tupui](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/tupui/32/5678_2.png) [@tupui](https://discourse.holoviz.org/u/tupui)\
**Post date:** [October 14, 2023, 12:12pm UTC](https://discourse.holoviz.org/t/stacked-histogram/6205/3 "2023-10-14T12:12:06Z")

</div>

Oh waou thank you so much! I will try to use that and let you know if I have issues.

For a bit more context, I am working on a panel app which right now is using seaborn. And I am trying to see how I can convert these so that the figures can be interactive.
