Display px.bar from maximum value to minimum

26 Views Asked by At

I have displayed a column of a dataframe using plotly express. The data is in the range (0,80) so I made bins between those values. I want to display them from 80 to 0, but the

update_xaxes(categoryorder = 'total descending')

is not working for me. could someone please help me? I found an issue in github from 2022 still opened about this: https://github.com/plotly/plotly.py/issues/3645

counts, bins = np.histogram(df.Age, bins=range(0, 80, 5))
bins = 0.5 * (bins[:-1] + bins[1:])

fig = px.bar(x=bins, y=counts, text_auto=True, labels={'x':'Age', 'y':'count'}, title = 'Age distribution between min (18) and max(69)' )
fig.update_xaxes(categoryorder = 'total descending')
fig.update_traces(textfont_size=14, textangle=0, textposition="outside", cliponaxis=False)
fig.show()

I have also tried sorting the values in descending order, but not working either:

dfAge = df.Age.sort_values(ascending = False)
print(dfAge)
counts, bins = np.histogram(dfAge, bins=range(0, 80, 5))
bins = 0.5 * (bins[:-1] + bins[1:])

fig = px.bar(x=bins, y=counts, text_auto=True, labels={'x':'Age', 'y':'count'}, title = 'Age distribution between min (18) and max(69)' )
fig.update_traces(textfont_size=14, textangle=0, textposition="outside", cliponaxis=False)
fig.show()
1

There are 1 best solutions below

0
GEM On

I have inverted the bins and it works, but I would really like to know why the option

fig.update_xaxes(categoryorder = 'total descending')

is not working for me.