Matplotlib.axes.Axes.format_ydata() in Python
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_ydata() Function
The Axes.format_ydata() function in axes module of matplotlib library is used to return y formatted as an y-value.
Syntax:
Axes.format_ydata(self, y)
Below examples illustrate the matplotlib.axes.Axes.format_ydata() function in matplotlib.axes:
Example 1:
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.axes.Axes.format_ydata() function\
Example', fontweight = "bold" )
plt.show()
|
Output:
Example 2:
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, color = "k" )
ax.yaxis.set_major_locator(years)
ax.yaxis.set_major_formatter(years_fmt)
ax.yaxis.set_minor_locator(months)
ax.format_ydata = mdates.DateFormatter( '% Y' )
ax.grid( True )
fig.suptitle('matplotlib.axes.Axes.format_ydata() function\
Example', fontweight = "bold" )
plt.show()
|
Output:
Last Updated :
19 Apr, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...