2 回答
TA贡献1797条经验 获得超6个赞
添加order参数。使用pandas.crosstab和sort_values来获得:
import pandas as pd
test_df = pd.DataFrame([
['Jake', 38, 'MW', 'Classic'],
['John', 38,'NW', 'Classic'],
['Sam', 34, 'SE', 'Classic'],
['Sam', 22, 'E' ,'Classic'],
['Joe', 43, 'ESE2', 'Classic'],
['Joe', 34, 'MTN2', 'Classic'],
['Joe', 38, 'MTN2', 'Classic'],
['Scott', 38, 'ESE2', 'Classic'],
['Chris', 34, 'SSE1', 'Classic'],
['Joe', 43, 'S1', 'New'],
['Paul', 34, 'NE2', 'New'],
['Joe', 38, 'MC1', 'New'],
['Joe', 34, 'NE2', 'New'],
['Nick', 38, 'MC1', 'New'],
['Al', 38, 'SSE1', 'New'],
['Doug', 34, 'ME', 'New'],
['Fred', 34, 'MC1', 'New'],
['Joe', 43, 'S1', 'New']], columns = ['Name','Code_A','Code_B','Type'])
import seaborn as sns
sns.set(style="darkgrid")
palette ={"Classic":"#FF9999","New":"#99CC99"}
order = pd.crosstab(test_df.Name, test_df.Type).sort_values('Classic', ascending=False).index
g = sns.countplot(y="Name",
palette=palette,
hue="Type",
data=test_df,
order=order
)
TA贡献1825条经验 获得超4个赞
import pandas as pd
test_df = pd.DataFrame([
['Jake', 38, 'MW', 'Classic'],
['John', 38,'NW', 'Classic'],
['Sam', 34, 'SE', 'Classic'],
['Sam', 22, 'E' ,'Classic'],
['Joe', 43, 'ESE2', 'Classic'],
['Joe', 34, 'MTN2', 'Classic'],
['Joe', 38, 'MTN2', 'Classic'],
['Scott', 38, 'ESE2', 'Classic'],
['Chris', 34, 'SSE1', 'Classic'],
['Joe', 43, 'S1', 'New'],
['Paul', 34, 'NE2', 'New'],
['Joe', 38, 'MC1', 'New'],
['Joe', 34, 'NE2', 'New'],
['Nick', 38, 'MC1', 'New'],
['Al', 38, 'SSE1', 'New'],
['Al', 34, 'ME', 'New'],
['Al', 34, 'MC1', 'New'],
['Joe', 43, 'S1', 'New']], columns = ['Name','Code_A','Code_B','Type'])
import seaborn as sns
sns.set(style="darkgrid")
palette ={"Classic":"#FF9999","New":"#99CC99"}
sb.countplot(y = 'Name', hue='Type', data=test_df,
order=test_df['Name'].value_counts().index)
添加回答
举报