How to Create Different Subplot Sizes in Matplotlib?
Last Updated :
30 Sep, 2022
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
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
import matplotlib.pyplot as plt
from matplotlib import gridspec
import numpy as np
fig = plt.figure()
fig.set_figheight( 8 )
fig.set_figwidth( 8 )
spec = gridspec.GridSpec(ncols = 2 , nrows = 2 ,
width_ratios = [ 2 , 1 ], wspace = 0.5 ,
hspace = 0.5 , height_ratios = [ 1 , 2 ])
x = np.arange( 0 , 10 , 0.1 )
y = np.cos(x)
ax0 = fig.add_subplot(spec[ 0 ])
ax0.plot(x, y)
ax1 = fig.add_subplot(spec[ 1 ])
ax1.plot(x, y)
ax2 = fig.add_subplot(spec[ 2 ])
ax2.plot(x, y)
ax3 = fig.add_subplot(spec[ 3 ])
ax3.plot(x, y)
plt.show()
|
Output:
Create Different Subplot Sizes in Matplotlib gridspec_kw
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.
plt.subplots(nrows=2, ncols=2,figsize=(7,7), gridspec_kw={‘width_ratios’: [3, 3], ‘height_ratios’: [3, 3], ‘wspace’ : 0.4, ‘hspace’ : 0.4})
Code :
Python3
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(nrows = 2 , ncols = 2 , figsize = ( 7 , 7 ),
gridspec_kw = {
'width_ratios' : [ 3 , 3 ],
'height_ratios' : [ 3 , 3 ],
'wspace' : 0.4 ,
'hspace' : 0.4 })
x = np.arange( 0 , 10 , 0.1 )
y = np.tan(x)
ax[ 0 ][ 0 ].plot(x, y)
ax[ 0 ][ 1 ].plot(x, y)
ax[ 1 ][ 0 ].plot(x, y)
ax[ 1 ][ 1 ].plot(x, y)
plt.show()
|
Output :
Create Different Subplot Sizes in Matplotlib subplot2grid
It provides more flexibility to create a grid at any location. We can extend the grid horizontally as well as vertically very easily. Here shape is denoting to no. of rows and columns and loc denotes the geometric location of the grid. Whereas, colspan = 3 in ax1, it means this subplot will cover 3 of all columns of that row. If we give irregular input then it will give an error so we have given the proper value to colspan and rowspan.
plt.subplot2grid(shape=(3, 3), loc=(0, 0), colspan=3)
Python3
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
fig.set_figheight( 6 )
fig.set_figwidth( 6 )
ax1 = plt.subplot2grid(shape = ( 3 , 3 ), loc = ( 0 , 0 ), colspan = 3 )
ax2 = plt.subplot2grid(shape = ( 3 , 3 ), loc = ( 1 , 0 ), colspan = 1 )
ax3 = plt.subplot2grid(shape = ( 3 , 3 ), loc = ( 1 , 2 ), rowspan = 2 )
ax4 = plt.subplot2grid(( 3 , 3 ), ( 2 , 0 ))
ax5 = plt.subplot2grid(( 3 , 3 ), ( 2 , 1 ), colspan = 1 )
x = np.arange( 0 , 10 , 0.1 )
y = np.cos(x)
ax1.plot(x, y)
ax1.set_title( 'ax1' )
ax2.plot(x, y)
ax2.set_title( 'ax2' )
ax3.plot(x, y)
ax3.set_title( 'ax3' )
ax4.plot(x, y)
ax4.set_title( 'ax4' )
ax5.plot(x, y)
ax5.set_title( 'ax5' )
plt.tight_layout()
plt.show()
|
Output :
Share your thoughts in the comments
Please Login to comment...