Editing the date formatting of x-axis tick labels in matplotlib


I am looking to edit the formatting of the dates on the x-axis. The picture below shows how they appear on my bar graph by default. I would like to remove the repetition of 'Dec' and '2012' and just have the actual date numbers along the x-axis.

Any suggestions as to how I can do this?

enter image description here

This question is tagged with python matplotlib

~ Asked on 2013-02-18 22:21:22

The Best Answer is


In short:

import matplotlib.dates as mdates
myFmt = mdates.DateFormatter('%d')

Many examples on the matplotlib website. The one I most commonly use is here

~ Answered on 2013-02-18 22:26:09


While the answer given by Paul H shows the essential part, it is not a complete example. On the other hand the matplotlib example seems rather complicated and does not show how to use days.

So for everyone in need here is a full working example:

from datetime import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter

myDates = [datetime(2012,1,i+3) for i in range(10)]
myValues = [5,6,4,3,7,8,1,2,5,4]
fig, ax = plt.subplots()

myFmt = DateFormatter("%d")

## Rotate date labels automatically

~ Answered on 2017-04-26 10:04:39

Most Viewed Questions: