How to sort bar's in a bar chart

I have a silly question that is easily handled in R but I can’t seem to figure it out in the web app. I created the following plot:

All I want to do is sort the bars! Is there an easy way to do this? The data is sorted in the file but because I have grouped some of the columns I can’t seem to sort it.

At the moment, the only way sort entries on a categorical axis is to sort the data of the first categorical trace on your graph.

So, in your case, you could fill your first trace with all categories in the desired order as x data where the extra entries would have NaN s as corresponding y entries. These extra NaN entries won’t show up on your graph.

I hope this make sense and note that we are planning on adding sort category sorting+ordering functionality in the near future.

1 Like

I would really like this functionality, too. Right now I’m trying to do this with cufflinks, and I’m ending up with the pandas dataframe index being used as the x-axis scale, which is not what I want.

We have someone working on adding this functionality at the moment.

Please subscribe to for the latest info.

I’m having difficulty finding the solution, ‘etienne’, Since it’s not an open issue any longer on Github;

import plotly
import plotly.graph_objs

trace1_df =df[df['col1']=='blue'].sort_values(by='bins')
trace1 = plotly.graph_objs.Bar(
  x=["(0, 5]","(5, 10]","(10, 15]","(15, 20]","(20, 25]","(25,35]","(35, 45]","(45, 55]","(55, 65]","(65, 75]","(75, 90]","(90, 105]","(105, 120]","(120, 150]","(150, 200]"],

trace2_df =df[df['col1']=='red'].sort_values(by='bins')
trace2 = plotly.graph_objs.Bar(
layout = plotly.graph_objs.Layout(barmode='group')
fig = plotly.graph_objs.Figure(data=data,layout=layout)

I am trying to set the categorical x axis for all trace(s) on the Total Bar plot, by using the static list for the ‘trace1’. Yet, this is giving me an extra duplicate bin at the far right of my x axis. Ideally, I’d like to not hardcode the ‘x=…’. Is there a Python fix for the xaxis category order issue for Multiple Group Vertical Bar Charts?


Would you mind sharing your data frame to help us debug?

I could, but it will take me a little time to keep the integrity but still anonymize it. Thanks

HI @etienne
I could transfer all data and code easier via .csv and .py files much easier if that’s better through Github?? Otherwise here are screenshots…


MORE DATA…The ‘nbr_inputs’ values go from 10 to over 150. And they are how the derived ‘input_bin’ column was created…

I was dealing with a similar case in which I realised that I needed to fill the data frame with missing observations with values 0, so for instance

col1, col2, value
A, A, 5
A,B, 2

I transformed it inserting the 0 missing data points
col1, col2, value
A, A, 5
A,B, 2
B,A, 0

I hope it helps someone

Here’s how to do this in Python: