skylineskyline
skylineskyline

Reputation: 13

How to change axis order from graph plotted using crosstab .plot() in pandas?

These are my codes to plot a 2-line graph using crosstab .plot() from dataframe in pandas. However, the axis order is wrong and I want to order it from Monday to Sunday. I sorted the logData dataframe to unauthorizedAccess and used that for crosstab. Is there any way to do it?

I included the codes, dataframe and the plot.

unauthorizedAccess = logData.loc[logData['Access Type'] == 'Unauthorized'] pd.crosstab(unauthorizedAccess['Day'], unauthorizedAccess['Month']).plot()

logData Dataframe crosstab .plot() graph

Upvotes: 1

Views: 542

Answers (1)

jezrael
jezrael

Reputation: 862701

You can convert names of days to ordered categorical:

unauthorizedAccess = logData.loc[logData['Access Type'] == 'Unauthorized'] 
names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday']
unauthorizedAccess['Day'] = pd.Categorical(unauthorizedAccess['Day'],
                                           categories=names, 
                                           ordered=True)
pd.crosstab(unauthorizedAccess['Day'], unauthorizedAccess['Month']).plot()

Or use DataFrame.reindex:

unauthorizedAccess = logData.loc[logData['Access Type'] == 'Unauthorized'] 
names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday']
pd.crosstab(unauthorizedAccess['Day'], unauthorizedAccess['Month']).reindex(names).plot()

Upvotes: 1

Related Questions