How to Improve Subplot Size or Spacing With Many Subplots in Matplotlib

  1. Tight_layout() Method to Change Matplotlib Subplot Size and Spacing
  2. plt.subplots_adjust() Method
  3. plt.subplot_tool() Method
  4. Activate constrained_layout=True in Subplots Function

We could use tight_layout(), subplots_adjust() and subplot_tool() methods to improve subplot size or spacing with many subplots in Matplotlib. We can also improve subplot spacing by setting constrained_layout=True in subplots() function.

Tight_layout() Method to Change Matplotlib Subplot Size and Spacing

The tight_layout() method automatically maintains correct spacing among subplots.

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(-3,3,100)
y1=np.sin(x)
y2=np.cos(x)
y3=1/(1+np.exp(-x))
y4=np.exp(x)

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

ax[0, 0].plot(x, y1)
ax[0, 1].plot(x, y2)
ax[1, 0].plot(x, y3)
ax[1, 1].plot(x,y4)

ax[0, 0].set_title("Sine function")
ax[0, 1].set_title("Cosine function")
ax[1, 0].set_title("Sigmoid function")
ax[1, 1].set_title("Exponential function")

fig.tight_layout()
plt.show()

Improve subplot size with many subplots using tight_layout

If we don’t use tight_layout() method, one row will overlap with the title of the next.

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(-3,3,100)
y1=np.sin(x)
y2=np.cos(x)
y3=1/(1+np.exp(-x))
y4=np.exp(x)

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

ax[0, 0].plot(x, y1)
ax[0, 1].plot(x, y2)
ax[1, 0].plot(x, y3)
ax[1, 1].plot(x,y4)

ax[0, 0].set_title("Sine function")
ax[0, 1].set_title("Cosine function")
ax[1, 0].set_title("Sigmoid function")
ax[1, 1].set_title("Exponential function")

plt.show()

subplots without using tight_layout

plt.subplots_adjust() Method

We can use plt.subplots_adjust() method to change the spacing between the subplots.

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(-3,3,100)
y1=np.sin(x)
y2=np.cos(x)
y3=1/(1+np.exp(-x))
y4=np.exp(x)

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

ax[0, 0].plot(x, y1)
ax[0, 1].plot(x, y2)
ax[1, 0].plot(x, y3)
ax[1, 1].plot(x,y4)

ax[0, 0].set_title("Sine function")
ax[0, 1].set_title("Cosine function")
ax[1, 0].set_title("Sigmoid function")
ax[1, 1].set_title("Exponential function")

plt.subplots_adjust(left=0.125,
                    bottom=0.1, 
                    right=0.9, 
                    top=0.9, 
                    wspace=0.2, 
                    hspace=0.35)

plt.show()

Improve subplot spacing with many subplots using plt.subplots_adjust

plt.subplots_adjust(left=0.125,
                    bottom=0.1, 
                    right=0.9, 
                    top=0.9, 
                    wspace=0.2, 
                    hspace=0.35)

wspace and hspace specify the space reserved between subplot. They are the fractions of axis width and height respectively.

left, right, top and bottom parameters specify the positions of four sides of the subplots. They are the fractions of the width and height of the figure.

plt.subplot_tool() Method

This method launches a subplot tool window for a figure.

import numpy as np
import matplotlib.pyplot as plt

im1=np.random.random((50,50))
im2=np.random.random((40,50))
im3=np.random.random((50,40))
im4=np.random.random((60,50))

plt.subplot(221)
plt.imshow(im1)
plt.subplot(222)
plt.imshow(im2)
plt.subplot(223)
plt.imshow(im3)
plt.subplot(224)
plt.imshow(im4)

plt.subplot_tool()
plt.show()

Improve subplot spacing with many subplots using plt.subplots_adjust

It provides an interactive method for the user to drag the bar in the subplot_tool to change the layout of the subplots.

Activate constrained_layout=True in Subplots Function

constrained_layout adjusts subplots and decorations automatically to fit them in the figure, as best as possible.

constrained_layout must be activated before or during subplot creation as it optimizes the layout before every drawing step.

import numpy as np
import matplotlib.pyplot as plt

a=np.linspace(0,5,100)

figure, axes = plt.subplots(2,2, constrained_layout=True)

axes[0, 0].plot(x, np.exp(a))
axes[0, 1].plot(a, np.sin(a))
axes[1, 0].plot(a, np.cos(a))
axes[1, 1].plot(range(10))

axes[0, 0].set_title("subplot 1")
axes[0, 1].set_title("subplot 2")
axes[1, 0].set_title("subplot 3")
axes[1, 1].set_title("subplot 4")

plt.show()

Improve subplot spacing with many subplots activating constrained_layout

Related Article - Matplotlib Subplot

  • How to Create Different Subplot Sizes in Matplotlib
  • comments powered by Disqus