Open In App

Matplotlib.axes.Axes.set_xlabel() in Python

Improve
Improve
Like Article
Like
Save
Share
Report

The Axes.set_aspect() function in axes module of Matplotlib library is used to set the aspect of the axis scaling, i.e. the ratio of y-unit to x-unit. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. The instances of Axes support callbacks through a callbacks attribute.

Matplotlib.axes.Axes.set_xlabel() Syntax in Python

Syntax: Axes.set_xlabel(self, xlabel, fontdict=None, labelpad=None, **kwargs) Parameters: This method accepts the following parameters.

  • xlabel : This parameter is the label text.
  • labelpad : This parameter is the spacing in points from the axes bounding box including ticks and tick labels.

Returns:This method does not returns any value.

Python Matplotlib.axes.Axes.set_xlabel() Function

Matplotlib.axes.Axes.set_xlabel() is used to set the label for the x-axis in a plot. It takes a string as an argument, representing the label text. This function allows users to provide a clear description or name for the x-axis, enhancing the overall understanding of the plotted data. The label is then displayed below the x-axis on the plot.

Matplotlib.axes.Axes.set_xlabel() Examples

Below are some examples by which we can use Axes Labels Matplotlib in Python:

Plotting Exponential Decay Using Matplotlib.axes.Axes.set_xlabel()

In this example, code utilizes Matplotlib to create a plot of an exponentially decaying function. It defines a time array ‘t’ and corresponding decay values ‘s’. The script then generates a plot, customizes the X-axis label, sets the plot limits, adds a grid, and provides a bold title.

Python3




import matplotlib.pyplot as plt
import numpy as np
 
t = np.arange(0.01, 5.0, 0.01)
s = np.exp(-t)
 
fig, ax = plt.subplots()
 
ax.plot(t, s)
ax.set_xlim(5, 0)
ax.set_xlabel('Display X-axis Label',
               fontweight ='bold')
ax.grid(True)
 
ax.set_title('matplotlib.axes.Axes.set_xlabel()\
 Examples\n', fontsize = 14, fontweight ='bold')
plt.show()


Output:

Visualizing Stock Data Trends Using Matplotlib.axes.set_xlabel() Function

In this example, code employs Matplotlib to visualize stock data trends. It loads historical stock price data for Google, selects the most recent 250 trading days, calculates price changes and volumes, and then creates a scatter plot. The color and size of the markers indicate close prices and trading volumes, respectively.

Python3




# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
 
with cbook.get_sample_data('goog.npz') as datafile:
    price_data = np.load(datafile)['price_data'].view(np.recarray)
     
# get the most recent 250
# trading days
price_data = price_data[-250:] 
 
delta1 = np.diff(price_data.adj_close)/price_data.adj_close[:-1]
 
volume = (25 * price_data.volume[:-2] / price_data.volume[0])**3
close = (0.03 * price_data.close[:-2] / 0.03 * price_data.open[:-2])**2
 
fig, ax = plt.subplots()
ax.scatter(delta1[:-1], delta1[1:],
           c = close, s = volume,
           alpha = 0.5)
 
ax.set_xlabel(r'X-axis contains $\Delta_i$ values',
              fontweight ='bold')
ax.grid(True)
fig.suptitle('matplotlib.axes.Axes.set_xlabel() Examples\n',
             fontsize = 14, fontweight ='bold')
 
plt.show()


Output:

Time Series Analysis Using set_xlabel() Function

In this example, the code uses the Matplotlib library to create a time series plot. It generates a sample time series dataset with random values, spanning from January 1, 2023, to December 31, 2023, with a daily frequency. The data is then cumulatively summed and stored in a Pandas DataFrame.

Python3




import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
# Generate sample time series data
date_rng = pd.date_range(start='2023-01-01', end='2023-12-31', freq='D')
data = np.random.randn(len(date_rng)).cumsum()
df = pd.DataFrame(data, columns=['value'], index=date_rng)
 
# Plot time series data
fig, ax = plt.subplots()
ax.plot(df.index, df['value'])
 
# Set x-axis label
ax.set_xlabel('Date')
 
plt.show()


Output:

g1



Last Updated : 10 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads