Reputation: 63
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:
Upvotes: 0
Views: 290
Reputation: 260690
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)
Upvotes: 2