Skip to content

update_xaxes(categoryorder="total descending") does not work correctly on px.histogram #2350

Closed
@ghost

Description

Hello,
When using update_xaxes(categoryorder="total descending") on a histogram, I expect the bars to be reordered by size.
The following example, tested in a Notebook, will not work according to my expectation:

import pandas as pd
import plotly.express as px

data =[['A','left'],['B','left'],['C','right'],['D','up'],['E','right'],['F','down'],
       ['A','right'],['B','righ'],['B','right'],['B','up'],['A','left'],['F','up'],
       ['B','left'],['F','left'],['B','right'],['A','up'],['A','right'],['F','down'],
      ]
df = pd.DataFrame(data, columns=['letter','direction'])
px.histogram(df, x='letter', color='direction').update_xaxes(categoryorder="total descending")

image

Note that the behavior is unclear to me. In certain condition, the order might be correct. For instance, if you change the last row to ['E','down'], then the order of the plot is correct.

In any case, I would expect the same result than the following:

import pandas as pd
import plotly.express as px

data =[['A','left'],['B','left'],['C','right'],['D','up'],['E','right'],['F','down'],
       ['A','right'],['B','righ'],['B','right'],['B','up'],['A','left'],['F','up'],
       ['B','left'],['F','left'],['B','right'],['A','up'],['A','right'],['E','down'],
      ]

df = pd.DataFrame(data, columns=['letter','direction'])
df['count'] = 1
df = df.groupby(['letter','direction'])['count'].sum().reset_index()
px.bar(df, x='letter', y='count', color='direction').update_xaxes(categoryorder="total descending")

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions