Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 258
Osmond Bishop Avatar asked Feb 18 '13 22:02

Osmond Bishop


People also ask

How do I change the date format on the x-axis in Matplotlib?

Create a figure and a set of subplots using subplots() method. Plot the dataframe using plot method, with df's (Step 1) time and speed. To edit the date formatting from %d-%m-%d to %d:%m%d, we can use set_major_formatter() method.

How do I format a date axis in Matplotlib?

Using the DateFormatter module from matplotlib, you can specify the format that you want to use for the date using the syntax: "%X %X" where each %X element represents a part of the date as follows: %Y - 4 digit year with upper case Y. %y - 2 digit year with lower case y. %m - month as a number with lower case m.

How do you change the X-axis ticks in Python?

To plot the line chart, use the plot() method. To rotate the ticks at the x-axis, use the plt. xticks() method and pass the rotation argument to it.


1 Answers

In short:

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

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

like image 174
Paul H Avatar answered Sep 24 '22 23:09

Paul H