Thursday, September 4, 2025
HomeLanguagesMatplotlib.figure.Figure.align_ylabels() in Python

Matplotlib.figure.Figure.align_ylabels() in Python

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.align_ylabels() function

The align_ylabels() method figure module of matplotlib library is used to Align the ylabels of subplots with the same subplots row or column if label alignment is being done automatically.

Syntax: align_ylabels(self, axs=None)

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

  • axs : This parameter is the list of Axes to align the ylabels.

Returns: This method does not return any value.

Below examples illustrate the matplotlib.figure.Figure.align_ylabels() function in matplotlib.figure:

Example 1:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
  
fig = plt.figure()
gs = gridspec.GridSpec(2, 2)
  
for i in range(2):
    ax = fig.add_subplot(gs[1, i])
    ax.set_ylabel('Y label')
    ax.set_xlabel('X label')
    if i == 0:
        for tick in ax.get_xticklabels():
            tick.set_rotation(45)
  
fig.align_ylabels()
  
fig.suptitle('matplotlib.figure.Figure.align_ylabels() \
function Example\n\n', fontweight ="bold")
  
plt.show()


Output:

Example 2:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
   
      
fig = plt.figure(tight_layout = True)
gs = gridspec.GridSpec(2, 2)
   
ax = fig.add_subplot(gs[0, :])
ax.plot(np.arange(0, 1e6, 1000))
  
ax.set_ylabel('YLabel0')
ax.set_xlabel('XLabel0')
   
for i in range(2):
    ax = fig.add_subplot(gs[1, i])
    ax.plot(np.arange(1., 0., -0.1) * 2000.
            np.arange(1., 0., -0.1))
      
    ax.set_ylabel('YLabel1 % d' % i)
    ax.set_xlabel('XLabel1 % d' % i)
      
    if i == 0:
        for tick in ax.get_xticklabels():
            tick.set_rotation(55)
  
fig.align_ylabels()  
   
fig.suptitle('matplotlib.figure.Figure.align_ylabels()\
function Example\n\n', fontweight ="bold")
  
plt.show()


Output:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS