Matplotlib Subplot Spacing: 4 Different Approaches

Hello coders!! In this article, we will be learning about matplotlib subplot spacing. We know that matplotlib is a library used for the visualization of given data. Subplots are used in order to two or more plots in the same figure. We will now learn the different methods of matplotlib subplot spacing.

Different methods to add matplotlib subplot spacing:

  • tight_layout()
  • plt.subplot_tool()
  • plt.subplot_adjust()
  • constrained_layout parameter

Let us now discuss all these methods in detail.

Method 1: tight_layout for matplotlib subplot spacing:

The tight_layout() is a method available in the pyplot module of the matplotlib library. It is used to adjust subplot parameters to give specified padding automatically.

Syntax:

matplotlib.pyplot.tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None)

Parameters:

  • pad: padding between the figure edge and the edges of subplots
  • h_pad, w_pad: padding (height/width) between edges of adjacent subplots
  • rect: a rectangle in the normalized figure coordinate that the whole subplots area will fit into

Return Value:

No return value.

import numpy as np 
import matplotlib.pyplot as plt 

x=np.array([1, 2, 3, 4, 5]) 

fig, ax = plt.subplots(2, 2) 

ax[0, 0].plot(x, x) 
ax[0, 1].plot(x, x*x) 
ax[1, 0].plot(x, x*x*x) 
ax[1, 1].plot(x, x*x*x*x) 

ax[0, 0].set_title("Linear")
ax[0, 1].set_title("Square")
ax[1, 0].set_title("Cube")
ax[1, 1].set_title("Fourth power")
fig.tight_layout() 
plt.show()

Output:

tight_layout for matplotlib subplot spacing
Output

In this example, we used the tight_layout() method for automatic spacing between the subplots. We used four subplots, all showing the relation of x with different powers. As we can see, all the subplots are properly spaced. However, if we did not use the tight_layout() method, one row would overlap with the title of the next.

Method 2: plt.subplot_tool() for matplotlib subplot spacing:

This method is also available in the pyplot module of the matplotlib library. It is used to launch a subplot tool window for a figure.

Syntax:

matplotlib.pyplot.subplot_tool(targetfig=None)

Parameters:

No parameter.

Return Value:

No return value.

import numpy as np 
import matplotlib.pyplot as plt 

x=np.array([1, 2, 3, 4, 5]) 

fig, ax = plt.subplots(2, 2) 

ax[0, 0].plot(x, x) 
ax[0, 1].plot(x, x*x) 
ax[1, 0].plot(x, x*x*x) 
ax[1, 1].plot(x, x*x*x*x) 

ax[0, 0].set_title("Linear")
ax[0, 1].set_title("Square")
ax[1, 0].set_title("Cube")
ax[1, 1].set_title("Fourth power")
plt.subplot_tool() 
plt.show()

Output:

plt.subplot_tool()
plt.subplot_tool() for matplotlib subplot spacing
Output

We have used the same example. Here, we have used the plt.subplot_tool() that provides us with an interactive method in which the user can drag the bar to adjust the spacing and the layout of the subplots.

Method 3: plt.subplot_adjust() for matplotlib subplot spacing:

This is a function available in the pyplot module of the matplotlib library. It is used to tune the subplot layout.

Syntax:

matplotlib.pyplot.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

Parameters:

  • left: left side of the subplots
  • right: right side of the subplots
  • bottom: bottom of the subplots
  • top: top of the subplots
  • wspace: amount of width reserved for space between subplots
  • hspace: amount of height reserved for space between subplots
import numpy as np 
import matplotlib.pyplot as plt 

x=np.array([1, 2, 3, 4, 5]) 

fig, ax = plt.subplots(2, 2) 

ax[0, 0].plot(x, x) 
ax[0, 1].plot(x, x*x) 
ax[1, 0].plot(x, x*x*x) 
ax[1, 1].plot(x, x*x*x*x) 

ax[0, 0].set_title("Linear")
ax[0, 1].set_title("Square")
ax[1, 0].set_title("Cube")
ax[1, 1].set_title("Fourth power")
plt.subplots_adjust(left=0.1, 
                    bottom=0.1,  
                    right=0.9,  
                    top=0.9,  
                    wspace=0.4,  
                    hspace=0.4) 
plt.show()

Output:

plt.subplot_adjust() for matplotlib subplot spacing
Output

We have used the same example again. But this time, we have used plt.subplot_adjust() to adjust the layout of the figure as per our preference.

Method 4: Achieving Subplot spacing Using constrained_layout parameter

The constrained_layout parameter is used to adjust the subplots automatically in order to fit them in the best possible way. This parameter must be activated in order to optimize the layout.

import numpy as np 
import matplotlib.pyplot as plt 

x=np.array([1, 2, 3, 4, 5]) 

fig, ax = plt.subplots(2, 2,  
                       constrained_layout = True) 

ax[0, 0].plot(x, x) 
ax[0, 1].plot(x, x*x) 
ax[1, 0].plot(x, x*x*x) 
ax[1, 1].plot(x, x*x*x*x) 

ax[0, 0].set_title("Linear")
ax[0, 1].set_title("Square")
ax[1, 0].set_title("Cube")
ax[1, 1].set_title("Fourth power")

plt.show()

Output:

constrained_layout parameter
Output

As you can see, we have activated the constrained_layout parameter. As a result, we get an optimized output.

Conclusion:

With this, we come to an end with this article. We learned four different methods for matplotlib spacing of subplots. We also saw detailed examples for each.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments