Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Allow specifying category order for legends in stacked bar plots #121

Open
alecfwilson opened this issue Jul 18, 2019 · 2 comments
Open

Comments

@alecfwilson
Copy link

I'd like to be able to set the order in which the items in color stack. For example, I'd like to make Sun the top value of this plot:

tips = px.data.tips()

fig = px.bar(
    tips,
    x="sex",
    y="total_bill",
    color="day",
    category_orders={
        "day": [
            "Mon",
            "Tue",
            "Wed",
            "Thu",
            "Fri",
            "Sat",
            "Sun"
        ]
    },
)
fig

but it returns:
image

I thought category_order would handle this, but it does not.

@nicolaskruchten
Copy link
Contributor

Actually I think it does, it’s just that in the dataset it’s spelled “Thur” not “Thu” and the order of the legend is from the bottom of the stack. If you want the order of the legend to go the other way you can add something like .update_layout(legend_traceorder=“reversed”)

@alecfwilson
Copy link
Author

Ah, that did solve the example I provided. However, passing a color_discrete_map seems to override it, at least with a different dataset I have been using:

fig = px.bar(
    adf,
    x="x",
    y="y",
    color="color",
    category_orders={
        "y": [
            "a",
            "b",
            "c",
            "d",
            "e",
            "f",
        ]
    },
    barmode="relative",
    opacity=1,
    color_discrete_map={
            "a": "rgb(31,120,180)",
            "b": "rgb(166,206,227)",
            "c": "rgb(51,160,44)",
            "d": "rgb(178,223,138)",
            "e": "rgb(255,127,0)",
            "f": "rgb(253,191,111)",
        },
)
fig

returns
image

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants