update_xaxes(categoryorder="total descending") does not work correctly on px.histogram
See original GitHub issueHello,
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")
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")
Issue Analytics
- State:
- Created 3 years ago
- Comments:18 (6 by maintainers)
Top Results From Across the Web
Histograms in Python - Plotly
In statistics, a histogram is representation of the distribution of numerical data, where the data are binned and the count for each bin...
Read more >Histograms with Plotly Express: Complete Guide
A histogram is a special kind of bar chart showing the distribution of a variable(s). Plotly.Express allows creating several types of histograms ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@nicolaskruchten I’m testing in a Dash App and with Terminal with same result.
I’m working with data in this attach file ( test_dataframe.txt ) and running the following exmeple :
fig1 is always displaying in the right order and fig2 always displaying in the wrong order
I released Plotly.py 4.14 this morning and it includes this fix. Note that if you’re using JupyterLab you’ll need to update your extensions, and if you’re using VSCode Python you’ll need to wait for the vscode-jupyter extension to incorporate Plotly.js 1.58.1 in a release (which should happen next month).