Skip to content
Related Articles

Related Articles

Matplotlib.figure.Figure.align_ylabels() in Python
  • Last Updated : 30 Apr, 2020

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:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :