Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Apr 3, 2020 · 18 comments

Comments

@ghost
Copy link

ghost commented Apr 3, 2020

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

@nicolaskruchten
Copy link
Contributor

Unfortunately I cannot reproduce this... your first example orders correctly for me:

image

And your second does also... Could you try upgrading to the latest version of plotly which is 4.6.0 to see if this is a bug we've already fixed?

@ghost
Copy link
Author

ghost commented Apr 3, 2020

So, I was indeed on 4.5.2 but I upgraded and still the same:
image

Strange... What else could it be?
I'm on Win10 + Anaconda3.

I downgraded pandas to 0.25, because who knows, but to no avail.

@wcj365
Copy link

wcj365 commented Aug 31, 2020

I have the same issue.
Plotly: 4.5.2
Pandas: 1.0.1
Ubuntu on AWS
The sorting works if I remove the color option.

@audelorAd
Copy link

Hello,
I have the same issue too.
Sorting isn't working with histogram but with bar chart it's ok.
Tried with latest version pandas : 1.1.2 & plotly : 4.10.0

2020-09-23_10h03_53

@jframe-fid
Copy link

jframe-fid commented Oct 26, 2020

I see this same issue with histograms, and switching to bar fixes it. I would love for it to be fixed so I can move back to the simplicity of histograms.

plotly: 4.11.0
pandas: 1.0.5

@nicolaskruchten
Copy link
Contributor

nicolaskruchten commented Oct 26, 2020

Are folks who are encountering this bug running JupyterLab? If so, please note that the version of the jupyterlab-plotly labextension must match the version of the plotly Python module to run correctly... See https://plotly.com/python/troubleshooting/#jupyterlab-problems for more details.

If this is happening in a "classic" Jupyter Notebook or is happening in JupyterLab with an up-to-date extension then there is definitely something to fix here.

Here is a Javascript-only version of the figure produced by this Python call: https://codepen.io/nicolaskruchten/pen/ExyWMZj ... if someone who is seeing this issue can please click on that link and tell me if things are rendering in or out of order (in the same browser/on the same machine as they're seeing the problem), that might help narrow down the possibilities!

Note: the expected order is BAFEDC

@ghost
Copy link
Author

ghost commented Oct 27, 2020

Hello @nicolaskruchten, I just tested on:

  • Win10 + Jupyter Notebook + Plotly 4.12
  • Manjaro + Jupyter Notebook + Plotly 4.11

and the problem was there on both. However, your codepen looks fine.

@audelorAd
Copy link

@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

@ghost
Copy link
Author

ghost commented Oct 27, 2020

2020-10-27_10h21_16

I really can't guess a logic that would generate that order...!
@audelorAd: you are studying Breton names, aren't you?!

@jframe-fid
Copy link

When I create a color by histogram, it never comes out correctly, and like @Kerybas I can't figure out how it's ordering things. Even if it acts up with more than one color bin, if I turn off all colors but one, it orders properly again. If I turn even one color back on though, it goes back to the really odd ordering.

@audelorAd
Copy link

I really can't guess a logic that would generate that order...!
@audelorAd: you are studying Breton names, aren't you?!

Yes I am ;)

@jenna-jordan
Copy link

I am also having this issue, but with px.bar rather than px.histogram
Like @wcj365 said, the problem goes away if you don't pass a "color" argument. However, that kinda defeats the point of a stacked bar chart...
I am rendering this with streamlit, not in a jupyter notebook.
code:

df = df.sort_values(by=["company_count", "Category"])
    fig = px.bar(
        df,
        y="Keyword",
        x="company_count",
        color="Category",
        barmode="stack",
        template="plotly_white",
        orientation="h",
        hover_name="Keyword",
        text="company_count",
        color_discrete_sequence=px.colors.qualitative.Vivid,
    )
    fig.update_yaxes(
        type="category",
        categoryorder="total ascending")

Plot:
Screen Shot 2020-11-11 at 3 07 19 PM

@nicolaskruchten
Copy link
Contributor

nicolaskruchten commented Nov 12, 2020

OK, we've got a fix in the underlying JS engine, which will become available with version 4.13 (edit: 4.14 now) of Plotly for Python. Hopefully that'll resolve all these issues!

@jframe-fid
Copy link

I've updated to plotly 4.13 today, and I'm still seeing the odd ordering behavior with px.histogram. Has anyone else seen this fixed updating to 4.13?

@nicolaskruchten
Copy link
Contributor

Hmm ok, maybe there's more than one problem here :) @jframe-fid can you share some simple code that shows this problem with v4.13 ?

@nicolaskruchten
Copy link
Contributor

Oh, sorry, no the problem is simply that the fix for this got bumped to 4.14 (next week) but I didn't update my comment, sorry!

@jenna-jordan
Copy link

@nicolaskruchten Did this fix get bumped again? I updated plotly and am still having this issue.

@nicolaskruchten
Copy link
Contributor

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

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

No branches or pull requests

5 participants