简体   繁体   English

Seaborn 条形图排序

[英]Seaborn Bar Plot Ordering

I have a pandas dataframe that has two columns.我有一个有两列的熊猫数据框。

I need the plot ordered by the "Count" Column.我需要按“计数”列排序的图。

dicti=({'37':99943,'25':47228,'36':16933,'40':14996,'35':11791,'34':8030,'24' : 6319 ,'2'  :5055 ,'39' :4758 ,'38' :4611  })
pd_df = pd.DataFrame(list(dicti.iteritems()))
pd_df.columns =["Dim","Count"]
plt.figure(figsize=(12,8))
ax = sns.barplot(x="Dim", y= "Count",data=pd_df )
ax.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x, loc: "
{:,}".format(int(x))))
ax.set(xlabel="Dim", ylabel='Count')
for item in ax.get_xticklabels():
    item.set_rotation(90)
for i, v in enumerate(pd_df["Count"].iteritems()):        
    ax.text(i ,v[1], "{:,}".format(v[1]), color='m', va ='bottom', 
    rotation=45)
plt.tight_layout()

Right now the plot is getting ordered by the "Dim" column, I need it ordered by the "Count" column,How can I do this?现在情节按“昏暗”列排序,我需要按“计数”列排序,我该怎么做? 在此处输入图片说明

you can use the order parameter for this.您可以为此使用 order 参数。

sns.barplot(x='Id', y="Speed", data=df, order=result['Id'])

Credits to Wayne.归功于韦恩。

See the rest of his code .查看他的其余代码

You have to sort your dataframe in desired way and the reindex it to make new ascending / descending index.您必须以所需的方式对数据框进行排序并重新索引它以创建新的升序/降序索引。 After that you may plot bar graph with index as x values.之后,您可以使用索引作为 x 值绘制条形图。 Then set set labels by Dim column of your dataframe:然后按数据框的 Dim 列设置标签:

import matplotlib.pylab as plt
import pandas as pd
import seaborn as sns

dicti=({'37':99943,'25':47228,'36':16933,'40':14996,'35':11791,'34':8030,'24' : 6319 ,'2'  :5055 ,'39' :4758 ,'38' :4611  })
pd_df = pd.DataFrame(list(dicti.items()))
pd_df.columns =["Dim","Count"]
print (pd_df)
# sort df by Count column
pd_df = pd_df.sort_values(['Count']).reset_index(drop=True)
print (pd_df)

plt.figure(figsize=(12,8))
# plot barh chart with index as x values
ax = sns.barplot(pd_df.index, pd_df.Count)
ax.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x, loc: "{:,}".format(int(x))))
ax.set(xlabel="Dim", ylabel='Count')
# add proper Dim values as x labels
ax.set_xticklabels(pd_df.Dim)
for item in ax.get_xticklabels(): item.set_rotation(90)
for i, v in enumerate(pd_df["Count"].iteritems()):        
    ax.text(i ,v[1], "{:,}".format(v[1]), color='m', va ='bottom', rotation=45)
plt.tight_layout()
plt.show()

在此处输入图片说明

Prepare the data frame such that it is ordered by the column that you want.准备数据框,使其按所需的列排序。

Now pass that as a parameter to function.现在将其作为参数传递给函数。

import pandas as pd
import seaborn as sns

dicti=({'37': 99943,'25': 47228,'36': 16933,'40': 14996,'35': 11791,'34': 8030,'24': 6319 ,'2': 5055 ,'39': 4758 ,'38' :4611})
pd_df = pd.DataFrame(list(dicti.items()))
pd_df.columns =["Dim", "Count"]

# Here the dataframe is already sorted if not use the below line
# pd_df = pd_df.sort_values('Count').reset_index()
# or 
# pd_df = pd_df.sort_values('Count',ascending=False).reset_index()

sns.barplot(x='Dim', y='Count', data=pd_df, order=pd_df['Dim'])`

在此处输入图片说明

To make a specific order, I recommend to create a list and then order by it:要下一个特定的订单,我建议创建一个列表,然后按它排序:

order_list = ['first', 'second', 'third']
sns.barplot(x=df['x'], y=df['y'], order=order_list)

You can use the following code您可以使用以下代码

import seaborn as sns

iris = sns.load_dataset("iris")
order = iris.groupby(["species"])["sepal_width"].mean().sort_values().index

sns.barplot(x="species", y="sepal_width", data=iris, order=order)

在此处输入图片说明

You specify ascending=False if you want to sort them from biggest to smallest.如果要将它们从大到小排序,则指定ascending=False

order = iris.groupby(["species"])["sepal_width"].mean().sort_values(ascending=False).index

Try using this.尝试使用这个。 There is no need to sort the dataframe or create extra lists.无需对数据框进行排序或创建额外的列表。

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

dicti=({'34':8030,'37':99943,'38':4611,'25':47228,'39':4758,'36':16933,'2':5055,'40':14996,'24':6319,'35':11791})
pd_df = pd.DataFrame(list(dicti.items()))
pd_df.columns =["Dim","Count"]
plt.figure(figsize=(12,8))
ax = sns.barplot(x="Dim", y= "Count",data=pd_df, order=pd_df.sort_values(by=['Count'], ascending=False).set_index('Dim').index)
ax.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x, loc: "{:,}".format(int(x))))
ax.set(xlabel="Dim", ylabel='Count')
for item in ax.get_xticklabels():
    item.set_rotation(90)
#for i, v in enumerate(pd_df["Count"].iteritems()):        
#    ax.text(i ,v[1], "{:,}".format(v[1]), color='m', va ='bottom', 
#    rotation=45)
plt.tight_layout()

输出

Note: You may notice that there are 3 lines of code that were turned to comments.注意:您可能会注意到有 3 行代码变成了注释。 This is because @Tronald Dump asked about the Seaborn Bar Plot functionality specifically, but there was code to display custom magenta labels that doesn't account for the usage of the optional "order" parameter of the seaborn.barplot function.这是因为@Tronald Dump询问了 Seaborn Bar Plot 功能,但有代码显示自定义洋红色标签,但没有考虑使用 seaborn.barplot 函数的可选“order”参数。 Therefore, this serves as a valid answer for the OP, but specially for future visitors.因此,这是 OP 的有效答案,但特别适用于未来的访问者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM