Editing x-axis tick mark date formatting in matplotlib

I want to change the date formatting on the x axis. The image below shows how they appear on my histogram by default. I would like to remove the repetition of "Dec" and "2012" and just have the actual numbers of the numbers along the x axis.

Any suggestions on how I can do this?

enter image description here

+22
python matplotlib
Feb 18 '13 at 22:21
source share
2 answers

In short:

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

Many examples are on the matplotlib website. I usually use here

+37
Feb 18 '13 at 22:26
source share

The link provided by Paul H ( http://matplotlib.org/examples/api/date_demo.html ) uses the following line of code to set the format:

 ax.format_xdata = mdates.DateFormatter('%Y-%m-%d') 

This does not work with my python 3 installation, which is the original installation that comes with jupyter. Instead, you need to install it using the Paul viz-a-viz line of code:

 ax.xaxis.set_major_formatter(myFmt) 

Oddly enough, this is not enough on the matplotlib website, so watch out for this error. Here is the complete code I used:

 ## Rotate date labels automatically import matplotlib.pyplot as plt from matplotlib.dates import DateFormatter fig, ax = plt.subplots() ax.plot(myDates,myValues) fig.autofmt_xdate() myFmt = DateFormatter("%d") ax.xaxis.set_major_formatter(myFmt) plt.show() 
+4
Apr 26 '17 at 10:04 on
source share



All Articles