我有一个有两列的pandas数据框。
我需要" Count"列。
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()
答案 0 :(得分:8)
您必须以所需方式对数据框进行排序,并重新编制索引以生成新的升序/降序索引。之后,您可以绘制条形图,索引为x值。然后按数据框的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()
答案 1 :(得分:4)
答案 2 :(得分:4)
准备数据框,以便按所需的列对其进行排序。
现在将其作为参数传递给功能。
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"]
# 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'])`
答案 3 :(得分:2)
要下一个特定的订单,我建议创建一个列表,然后按它排序:
order_list = ['first','second','third']
sns.barplot(x = df['x'], y = df['y'],order=order_list )
答案 4 :(得分:0)
您可以使用以下代码
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)
如果要将它们从大到小排序,请指定 ascending=False
。
order = iris.groupby(["species"])["sepal_width"].mean().sort_values(ascending=False).index