Search code examples
pythonplotseabornvisualizationseaborn-objects

Stacking to 100% with `seaborn.objects`


I'm trying to make a plot with bars or areas rescaled to 100% with the new seaborn.objects interface and I can't seem to get so.Norm() to work, with or without by...

Here's what I've got so far:

import seaborn as sns
import seaborn.objects as so


tips = sns.load_dataset("tips")

# bars
(
  so.Plot(tips, x="day", y="total_bill", color="time")
  .add(so.Bar(), so.Agg("sum"), so.Norm(func="sum"), so.Stack())
)

#areas
(
  so.Plot(tips, x="size", y="total_bill", color="time")
  .add(so.Area(), so.Agg("sum"), so.Norm(func="sum"), so.Stack())
)

Solution

  • I think that you intend for the height of each (stacked) bar to equal 1, so you'd want to aggregate by x values when normalizing:

    (
      so.Plot(tips, x="day", y="total_bill", color="time")
      .add(so.Bar(), so.Agg("sum"), so.Norm(func="sum", by=["x"]), so.Stack())
    )
    

    enter image description here