Open In App
Related Articles

Matplotlib.figure.Figure.autofmt_xdate() in Python

Improve Article
Save Article
Like Article

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot elements.

matplotlib.figure.Figure.autofmt_xdate() function

The autofmt_xdate() method figure module of matplotlib library is used to rotate them and right align them.

Syntax: autofmt_xdate(self, bottom=0.2, rotation=30, ha=’right’, which=None)

Parameters: This accept the following parameters that are described below:

  • bottom : This parameter is the bottom of the subplots for subplots_adjust().
  • rotation : This parameter is the rotation of the xtick labels.
  • ha : This parameter is the horizontal alignment of the xticklabels.
  • which : This parameter selects which ticklabels to rotate.

Returns: This method does not return any value.

Below examples illustrate the matplotlib.figure.Figure.autofmt_xdate() function in matplotlib.figure:

Example 1:

# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.cbook as cbook
years = mdates.YearLocator()   
months = mdates.MonthLocator()  
years_fmt = mdates.DateFormatter('% Y')
with cbook.get_sample_data('goog.npz') as datafile:
    data = np.load(datafile)['price_data'].view(np.recarray)
fig, ax = plt.subplots()
ax.plot('date', 'adj_close'
         data = data[:300],
         color ="green")
ax.format_ydata = lambda x: '$% 1.2f' % x
fig.suptitle('matplotlib.figure.Figure.autofmt_xdate() \
function Example\n\n', fontweight ="bold")


Example 2:

# Implementation of matplotlib function
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange
import numpy as np
date1 = datetime.datetime(2020, 4, 2)
date2 = datetime.datetime(2020, 4, 6)
delta = datetime.timedelta(hours = 6)
dates = drange(date1, date2, delta)
y = np.arange(len(dates))
fig, ax = plt.subplots()
ax.plot_date(dates, y ** 2, 'g')
ax.set_xlim(dates[0], dates[-1])
ax.xaxis.set_minor_locator(HourLocator(range(0, 25, 6)))
ax.xaxis.set_major_formatter(DateFormatter('% Y-% m-% d'))
ax.fmt_xdata = DateFormatter('% Y-% m-% d % H:% M:% S')
fig.suptitle('matplotlib.figure.Figure.autofmt_xdate() \
function Example\n\n', fontweight ="bold")


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 30 Apr, 2020
Like Article
Save Article
Similar Reads
Complete Tutorials