Open In App

Matplotlib.figure.Figure.autofmt_xdate() in Python

Improve
Improve
Like Article
Like
Save
Share
Report

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.xaxis.set_major_locator(years) 
ax.format_ydata = lambda x: '$% 1.2f' % x
ax.grid(True)
   
fig.autofmt_xdate()
   
fig.suptitle('matplotlib.figure.Figure.autofmt_xdate() \
function Example\n\n', fontweight ="bold")
  
plt.show()


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_major_locator(DayLocator())
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.autofmt_xdate()
   
fig.suptitle('matplotlib.figure.Figure.autofmt_xdate() \
function Example\n\n', fontweight ="bold")
  
plt.show()


Output:



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