Search code examples
pythonpandasbar-chartalphabetical

How to plot a bar graph labels in alphabetical order in python?


I have the following bar graph:

Click here for bar graph

I would like to alphabetically order the Y-axis labels (i.e, control, exclude, necrosis, other, tissue, tumor and not control, other, necrosis, exclude, tissue, tumor). Ho do I do that?

What I tried so far?

smack = df.roi_name.value_counts()

plt.barh(width=smack.values, y=smack.index)
plt.xlim(0,50000)
plt.xlabel("ROI_NAME count")
fig = plt.gcf()
fig.set_size_inches(18.5, 10.5)

Thanks in advance!


Solution

  • you just need to sort index of your dataframe when you give it to plt.barh(), like below:

    plt.barh(width=smack.values, y=smack.index.sort_values())