Eli Turasky
Eli Turasky

Reputation: 1061

Change xtick labels from pandas plot to different date format

I plot data from this dataframe.

Date    2011    2012    2013    2014    2015    2016    2017    2018    2019    2020
Date                                        
01-01   12.896  13.353  12.959  13.011  13.073  12.721  12.643  12.484  12.876  13.102
01-02   12.915  13.421  12.961  13.103  13.125  12.806  12.644  12.600  12.956  13.075
01-03   12.926  13.379  13.012  13.116  13.112  12.790  12.713  12.634  12.959  13.176
01-04   13.051  13.414  13.045  13.219  13.051  12.829  12.954  12.724  13.047  13.187
01-05   13.176  13.417  13.065  13.148  13.115  12.874  12.956  12.834  13.098  13.123

The plot generates a line plot for each year across every day of the year.

ice_data.plot(figsize=(20,12), title='Arctic Sea Ice Extent', lw=4, fontsize=16, ax=ax, grid=True)

However, this plot generates the xtick labels in the format from the dataframe, which is 01-01 (January 1). I want to change the xtick label to be 01 Jan, but cannot seem to figure out how to manipulate the dataframe using strftime() to accomplish this. Here is a picture for reference.

enter image description here

Upvotes: 1

Views: 71

Answers (1)

jezrael
jezrael

Reputation: 863431

Convert values to datetimes by to_datetime with specified year for correct parsing 2-29, last convert to custom format by DatetimeIndex.strftime:

idx = pd.to_datetime('2000' + ice_data.index, format='%Y%m-%d').strftime('%d %b')
ice_data = ice_data.set_index(idx)

Upvotes: 1

Related Questions