Black Lives Matter. Please consider donating to Black Girls Code today.

Set specific order to bars


I would like to know how can I set the order of the bars in a horizontal bar chart as the reverse order of the labels. For example, if:

labels = ['label1', 'label2', 'label3']
values = [15, 10, 25]

I tried to specify in the yaxis dict in layout like this:

yaxis = {'categoryorder': 'array', 'categoryarray': labels[::-1]}

So the first bar from top to bottom is the one that refers to the ‘label1’ and so it goes. But that doesn’t work as I expected, instead it seems to always use ‘categorryorder’ as ‘trace’.

Hi @camila.cardoso,

Happy Birthday!!! :slight_smile:

Is the resulting plot of this bar definition:

labels=['L1', 'L2', 'L3']
fig.add_bar(x=[15, 10, 25], y=labels[::-1], orientation='h' )

what you are expecting?

Thanks, @empet!

Indeed it is what I exepcted, but I was hoping I could do this by specifying the order in the ‘categoryarray’ argument. I wanted to avoid changing the labels and values order in the construction of the bar chart.