question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
audelorAdcommented, Oct 27, 2020

@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 :

col = ['lib','type','year','sup']

# Get data with attach file
df = pd.DataFrame(data, columns=col)

fig1 = px.histogram(df, x='year', y='sup', color='type').update_xaxes(type="category", categoryorder="total descending")

fig1.show()

fig2 = px.histogram(df, x='lib', y='sup', color='type').update_xaxes(type="category", categoryorder="total descending")

fig2.show()

fig1 is always displaying in the right order and fig2 always displaying in the wrong order 2020-10-27_10h21_16

0reactions
nicolaskruchtencommented, Dec 7, 2020

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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found