Open In App

Matplotlib.figure.Figure.set_constrained_layout() in Python

Improve
Improve
Like Article
Like
Save
Share
Report

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.set_constrained_layout() method

The set_constrained_layout() method figure module of matplotlib library is used to set whether constrained_layout is used upon drawing.

Syntax: set_constrained_layout(self, constrained)

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

  • constrained: This parameter is the bool or dict or None.

Returns: This method returns the axes.

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

Example 1:




# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
   
  
fig, ax = plt.subplots(constrained_layout = True)
x = np.arange(0.02, 1, 0.02)
  
np.random.seed(19680801)
y = np.random.randn(len(x)) ** 2
  
ax.loglog(x, y)
  
ax.set_xlabel('f [Hz]')
ax.set_ylabel('PSD')
ax.set_title('Random spectrum')
   
   
def forward(x):
    return 1 / x
   
   
def inverse(x):
    return 1 / x
  
fig.set_constrained_layout(True)
  
fig.suptitle("""matplotlib.figure.Figure.set_constrained_layout()
function Example\n\n""", fontweight ="bold")    
  
plt.show()


Output:

Example 2:




# Implementation of matplotlib function
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
   
  
fig = plt.figure()
gs = fig.add_gridspec(3, 3)
ax = fig.add_subplot(gs[0, :])
  
ax.set_title('gs[0, :]')
ax2 = fig.add_subplot(gs[1, :-1])
ax2.set_title('gs[1, :-1]')
  
fig.set_constrained_layout(False)
  
fig.suptitle("""matplotlib.figure.Figure.set_constrained_layout()
function Example\n\n""", fontweight ="bold")    
  
plt.show()


Output:



Last Updated : 03 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads