Thursday, November 20, 2025
HomeLanguagesMatplotlib.pyplot.xlabels() in Python

Matplotlib.pyplot.xlabels() in Python

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface.

matplotlib.pyplot.xlabel() Function

The xlabel() function in pyplot module of matplotlib library is used to set the label for the x-axis..

Syntax: matplotlib.pyplot.xlabel(xlabel, fontdict=None, labelpad=None, **kwargs)

Parameters: This method accept the following parameters that are described below:

  • xlabel: This parameter is the label text. And contains the string value.
  • labelpad: This parameter is used for spacing in points from the axes bounding box including ticks and tick labels and its default value is None.
  • **kwargs: This parameter is Text properties that is used to control the appearance of the labels.

Below examples illustrate the matplotlib.pyplot.xlabel() function in matplotlib.pyplot:

Example #1:




# Implementation of matplotlib.pyplot.xlabels()
# function
   
import numpy as np
import matplotlib.pyplot as plt
  
t = np.arange(-180.0, 180.0, 0.1)
s = np.radians(t)/2.
  
plt.plot(t, s, '-', lw = 2)
  
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.title('xlabels() function')
plt.grid(True)
  
plt.show()


Output:

Example #2:




# Implementation of matplotlib.pyplot.xlabels()
# function
   
import numpy as np
import matplotlib.pyplot as plt
  
valx1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
  
valy1 = np.cos(2 * np.pi * valx1) * np.exp(-valx1)
y2 = np.cos(2 * np.pi * x2)
  
plt.subplot(2, 1, 1)
plt.plot(valx1, valy1, 'o-')
plt.title('xlabel() Example')
plt.ylabel('Damped oscillation')
  
plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')
  
plt.show()


Output:

RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6776 POSTS0 COMMENTS
Nicole Veronica
11925 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6905 POSTS0 COMMENTS
Ted Musemwa
7160 POSTS0 COMMENTS
Thapelo Manthata
6861 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS