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:

https://plot.ly/~isaacfab/828/annual-us-injury-deaths-by-type-2013/

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 https://github.com/plotly/plotly.js/issues/189 for the latest info.

I’m having difficulty finding the solution, ‘etienne’, Since it’s not an open issue any longer on Github; https://github.com/plotly/plotly.js/pull/510

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]"],
  y=trace1_df['num1'].tolist(),
  name='blue'
)

trace2_df =df[df['col1']=='red'].sort_values(by='bins')
trace2 = plotly.graph_objs.Bar(
  x=trace2_df['cat1'].tolist(),\
  y=trace2_df['num1'].tolist(),
  name='red'
)
data=[trace1,trace2]
layout = plotly.graph_objs.Layout(barmode='group')
fig = plotly.graph_objs.Figure(data=data,layout=layout)
plotly.offline.plot(fig)

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?

@bears34egc

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…

<img

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
B,B,3

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

I hope it helps someone

Here’s how to do this in Python: https://plotly.com/python/bar-charts/#bar-chart-with-sorted-or-ordered-categories