Open In App
Related Articles

Matplotlib.figure.Figure.autofmt_xdate() in Python

Like Article
Save Article
Report issue
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")

Output: 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")


Last Updated : 30 Apr, 2020
Like Article
Save Article
Share your thoughts in the comments
Similar Reads