Python Seaborn条形图按条长排序

Python Seaborn条形图按条长排序,python,matplotlib,bar-chart,seaborn,Python,Matplotlib,Bar Chart,Seaborn,我正在尝试使用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 pa

我正在尝试使用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)
我的情节是这样的

我希望这些条线按降序排列。行使的股票期权在左边有最高的价值,然后是总的股票价值,依此类推

请帮忙。谢谢这两行

pairs = zip(features_list[1:], scores) # zip with features_list
pairs= sorted(pairs, key=lambda x: x[1], reverse= True)
已经为您提供了一个元组列表,按
分数
值排序。现在,您只需要将其解压缩到两个列表中即可绘制

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()