Open In App

Matplotlib.axes.Axes.format_xdata() in Python

Last Updated : 19 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

matplotlib.axes.Axes.format_xdata() Function

The Axes.format_xdata() function in axes module of matplotlib library is used to return x formatted as an x-value.

Syntax:

Axes.format_xdata(self, x)

Below examples illustrate the matplotlib.axes.Axes.format_xdata() function in matplotlib.axes:

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, color ="green")
  
ax.xaxis.set_major_locator(years)
ax.set_ylim((100, 300))
  
ax.format_xdata = mdates.DateFormatter('% m')
ax.grid(True)
  
fig.suptitle('matplotlib.axes.Axes.format_xdata() function\
 Example', fontweight ="bold")
plt.show()


Output:

Example 2:




# 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']
  
fig, ax = plt.subplots()
ax.plot('date', 'adj_close', data = data)
  
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(years_fmt)
ax.xaxis.set_minor_locator(months)
  
ax.format_xdata = mdates.DateFormatter('% Y')
ax.grid(True)
  
fig.autofmt_xdate()
  
fig.suptitle('matplotlib.axes.Axes.format_xdata() function \
Example', fontweight ="bold")
plt.show()


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads