How do I sort columns in a strip in ascending order?
I have created a graph using matplotlib.pyplot and nautical libraries. How can I sort the columns in ascending order according to Speed
? I want to see lanes with the lowest speed on the left and the highest speed on the right.
df =
Id Speed
1 30
1 35
1 31
2 20
2 25
3 80
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
result = df.groupby(["Id"])['Speed'].aggregate(np.median).reset_index()
norm = plt.Normalize(df["Speed"].values.min(), df["Speed"].values.max())
colors = plt.cm.Reds(norm(df["Speed"]))
plt.figure(figsize=(12,8))
sns.barplot(x="Id", y="Speed", data=gr_vel_1, palette=colors)
plt.ylabel('Speed', fontsize=12)
plt.xlabel('Id', fontsize=12)
plt.xticks(rotation='vertical')
plt.show()
+4
source to share
1 answer
df.groupby(['Id']).median().sort_values("Speed").plot.bar()
Or just try sort_values ("Speed") after combining 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 the order:
sns.barplot(x='Id', y="Speed", data=a, palette=colors, order=result['Id'])
+6
source to share