'Seaborn Pointplot xticks=Days do not appear in correct order, how to change it
My xticks of Days (categorical data from data frame) doesn't appear in correct order, how to change it?
plt.figure(figsize=(13,6))
houragg = pd.DataFrame(df.groupby(['Day','Seasons'])['bike_count'].mean()).reset_index()
sns.pointplot(data=houragg,x=houragg['Day'],
y=houragg['bike_count'],
scale = 0.3,
hue=houragg['Seasons']).set(title='Count by Day and Seasons')
plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)
here is the plot:
Solution 1:[1]
You can set the order parameter of seaborn.pointplot:
from calendar import day_name
order = list(day_name)
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
sns.pointplot(data=houragg, x=houragg['Day'], order=order,
y=houragg['bike_count'],
scale = 0.3,
hue=houragg['Seasons']).set(title='Count by Day and Seasons')
Alternatively, use an ordered Categorical with pandas.Categorical:
cat = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday']
houragg['Day'] = pd.Categorical(houragg['Day'], categories=cat, ordered=True)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mozway |

