2

I am using seaborn objects to plot a stacked bar chart and I am wondering what the right was is to change the sort order in which the bars are stacked.

Looking at this example:

import seaborn.objects as so
import seaborn as sns

penguins = sns.load_dataset("penguins")
so.Plot(
    penguins,
    x="species",
    y="body_mass_g",
    color="sex",
).add(so.Bar(width=0.2), so.Agg(), so.Stack())

enter image description here the order is ['male', 'female']. How do I reverse that order?

The only answer I came up with is to sort the input data. It appears that the order of the bars depends on the order of appearance in the dataset.

so.Plot(
    penguins.sort_values(by="sex", ascending=True),
    x="species",
    y="body_mass_g",
    color="sex",
).add(so.Bar(width=0.2), so.Agg(), so.Stack()).scale(
    color=so.Nominal(order=["Male", "Female"])
)   

enter image description here

Is there a way to achieve this without changing the input data?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
divingTobi
  • 2,044
  • 10
  • 25
  • There's a question on this topic for matplotlib, however [this answer](https://stackoverflow.com/a/68744777) uses `sns.barplot` and may be adaptable to your issue. – MagnusO_O Sep 11 '22 at 09:11

1 Answers1

3

You can define the order for the color variable with a scale:

(
    so.Plot(penguins, x="species", y="body_mass_g", color="sex")
    .add(so.Bar(width=0.2), so.Agg(), so.Stack())
    .scale(color=so.Nominal(order=["Female", "Male"]))  # <--- Add this line
)

enter image description here

It looks like the stack operation is still putting the female bars on top of the male bars, which is interesting and may be a bug, but also may be what you're looking for here (i.e. that the stack order matches the legend order from top-to-bottom). There is an issue to make that happen by default: https://github.com/mwaskom/seaborn/issues/2888

mwaskom
  • 46,693
  • 16
  • 125
  • 127
  • Hi @mwaskomm, the order of the legend was just sugar on top, I was more looking for a way to get the bars in a defined order, independent of how the data was sorted. – divingTobi Sep 11 '22 at 19:09