Create Different Subplot Sizes in Matplotlib using Gridspec

The GridSpec from the gridspec module is used to adjust the geometry of the Subplot grid. We can use different parameters to adjust the shape, size, and number of columns and rows.

gridspec.GridSpec(ncols=2, nrows=2, width_ratios=[2, 1],wspace=0.5,hspace=0.5,height_ratios=[1,2])

Python3




# importing required libraries
import matplotlib.pyplot as plt
from matplotlib import gridspec
import numpy as np
 
# create a figure
fig = plt.figure()
 
# to change size of subplot's
# set height of each subplot as 8
fig.set_figheight(8)
 
# set width of each subplot as 8
fig.set_figwidth(8)
 
# create grid for different subplots
spec = gridspec.GridSpec(ncols=2, nrows=2,
                         width_ratios=[2, 1], wspace=0.5,
                         hspace=0.5, height_ratios=[1, 2])
 
# initializing x,y axis value
x = np.arange(0, 10, 0.1)
y = np.cos(x)
 
# ax0 will take 0th position in
# geometry(Grid we created for subplots),
# as we defined the position as "spec[0]"
ax0 = fig.add_subplot(spec[0])
ax0.plot(x, y)
 
# ax1 will take 0th position in
# geometry(Grid we created for subplots),
# as we defined the position as "spec[1]"
ax1 = fig.add_subplot(spec[1])
ax1.plot(x, y)
 
# ax2 will take 0th position in
# geometry(Grid we created for subplots),
# as we defined the position as "spec[2]"
ax2 = fig.add_subplot(spec[2])
ax2.plot(x, y)
 
# ax3 will take 0th position in
# geometry(Grid we created for subplots),
# as we defined the position as "spec[3]"
ax3 = fig.add_subplot(spec[3])
ax3.plot(x, y)
 
# display the plots
plt.show()


Output: 

 

How to Create Different Subplot Sizes in Matplotlib?

In this article, we will learn different ways to create subplots of different sizes using Matplotlib. It provides 3 different methods using which we can create different subplots of different sizes.

Methods available to create subplot: 

  • Gridspec
  • gridspec_kw
  • subplot2grid

Similar Reads

Create Different Subplot Sizes in Matplotlib using Gridspec

The GridSpec from the gridspec module is used to adjust the geometry of the Subplot grid. We can use different parameters to adjust the shape, size, and number of columns and rows....

Create Different Subplot Sizes in Matplotlib gridspec_kw

...

Create Different Subplot Sizes in Matplotlib subplot2grid

It is a dictionary available inside “plt.subplots()” method in Matplotlib. By passing different parameters to the dictionary we can adjust the shape and size of each subplot....