【发布时间】:2017-12-06 17:51:06
【问题描述】:
我使用 matplotlib.pyplot 和 seaborn 库创建了一个条形图。如何根据Speed 对条形进行升序排序?我想看左边速度最低的条,右边速度最高的条。
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()
【问题讨论】:
标签: python pandas matplotlib seaborn