[英]Seaborn barplot ordering by bar length
我正在嘗試使用seaborn繪制barplot。
k= 'all'
k_best = SelectKBest(k=k)
k_best=k_best.fit(features, labels)
features_k=k_best.transform(features)
scores = k_best.scores_ # extract scores attribute
pairs = zip(features_list[1:], scores) # zip with features_list
pairs= sorted(pairs, key=lambda x: x[1], reverse= True) # sort tuples in descending order
print pairs
#Bar plot of features and its scores
sns.set(style="white")
ax = sns.barplot(x=features_list[1:], y=scores)
plt.ylabel('SelectKBest Feature Scores')
plt.xticks(rotation=90)
我希望條形按降序排序。Exercised_stock_options的左側是最大值,其次是total_stock_value,依此類推。
請幫助。謝謝
兩條線
pairs = zip(features_list[1:], scores) # zip with features_list
pairs= sorted(pairs, key=lambda x: x[1], reverse= True)
已經給您一個元組列表,按scores
值排序。 現在,您只需要將其解壓縮到兩個列表即可進行繪制。
newx, newy = zip(*pairs)
sns.barplot(x=newx, y=newy)
一個完整的工作示例:
import seaborn.apionly as sns
import matplotlib.pyplot as plt
x = ["z","g","o"]
y = [5,7,4]
pairs = zip(x, y)
pairs= sorted(pairs, key=lambda x: x[1], reverse= True)
newx, newy = zip(*pairs)
ax = sns.barplot(x=newx, y=newy)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.