简体   繁体   中英

Rearrange seaborn countplot x-axis

I am using seaborn countplot on a pandas data series. The series looks like this:

df['col'] = 

['Week 4',
 'Week 4',
 'Week 3',
 'Week 1',
 'Week 5',
 'Week 3',
 'Week 3',
 'Week 2',
 'Week 4',
 'Week 5',
 'Week 5',
 'Week 4',
 'Week 5',
 'Week 2',
 'Week 5',
 'Week 1',
 ..
 ..
 ..
 ..
]

I'd like rearrange the x-axis to start with Week 1, Week 2, Week 3.... and so on.

sns.countplot(LeaseComp['Weeks on market'])

在此处输入图像描述

You can sorted values like:

L = ['Week 4',
 'Week 4',
 'Week 3',
 'Week 1',
 'Week 5',
 'Week 3',
 'Week 3',
 'Week 2',
 'Week 4',
 'Week 5',
 'Week 5',
 'Week 4',
 'Week 5',
 'Week 2',
 'Week 5',
 'Week 1',

]
LeaseComp = pd.DataFrame(L, columns=['Weeks on market'])

sns.countplot(LeaseComp['Weeks on market'].sort_values())

G

For seaborn:

sns.countplot( x = 'Weeks on market', data = LeaseComp, 
              order = sorted(LeaseComp['Weeks on market'].unique()))

Same plot can be achieved by what BEATHUB has posted, but it may produce some warnings.

You may try,

sns.countplot(x= LeaseComp['Weeks on market'].sort_values())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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