How to sort bars in a bar plot in ascending order?

后端 未结 1 777
慢半拍i
慢半拍i 2021-01-05 00:19

I created a bar plot using matplotlib.pyplot and seaborn libraries. How can I sort bars in increasing order according to Speed? I want to see the bars with the

相关标签:
1条回答
  • 2021-01-05 00:50
    df.groupby(['Id']).median().sort_values("Speed").plot.bar()
    

    Or just try to sort_values("Speed") after you aggregate them.

    EDIT: so you need to do this:

    result = a.groupby(["Id"])['Speed'].aggregate(np.median).reset_index().sort_values('Speed')
    

    and in sns.barplot add order:

    sns.barplot(x='Id', y="Speed", data=a, palette=colors, order=result['Id'])
    
    0 讨论(0)
提交回复
热议问题