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?

1
  • There's a question on this topic for matplotlib, however this answer uses sns.barplot and may be adaptable to your issue. Commented Sep 11, 2022 at 9:11

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.