Skill - Multiple Subplots in a figure using Matplotlib
Skills Required
- Setup python development environment
- Basic Printing in Python
- Commenting in Python
- Managing Variables in python
- Introduction to Matplotlib plotting library
- Styling Matplotlib plots
- Multiple Plots in a same subplot using Matplotlib
Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision
Matplotlib is a plotting library in the scipy ecosystem of libraries.
In this post we will learn how to plot multiple subplots on a single Matplotlib figure
Creating a grid of subplots
use nrows, ncols
parameters of the plt.subplots
function to get a grid of subplots.
This returns a figure and an array of axes handles in the same shape of the grid. That is, if the subplots grid is 3x2 then the shape of axes handles array will also be 3x2
import matplotlib.pyplot as plt
# suppose we want a figure with 3 rows 2 columns
# use nrows, ncols inputs to control the rows and columns of subplots
# constrained_layout parameter takes care of proper spacing
fig, axs = plt.subplots(nrows=3,ncols=2,constrained_layout=True)
# set entire figure title using the suptitle function
fig.suptitle('3 x 2 Subplot grid', fontsize=12)
# set titles for each subplot using the respective axes handles
axs[0][0].set_title('0,0 position')
axs[0][1].set_title('0,1 position')
axs[1][0].set_title('1,0 position')
axs[1][1].set_title('1,1 position')
axs[2][0].set_title('2,0 position')
axs[2][1].set_title('2,1 position')
# print the figure
plt.show()
Grid of subplots with uneven sizes
If the size of all the subplots in the grid is not same, then we need to use the GridSpec
api to control how many rows and column each subplot will occupy in the subplot grid.
import matplotlib.pyplot as plt
# create the figure and get the figure handle
fig = plt.figure(constrained_layout=True)
# create a grid spec for 3 rows and 3 columns
gspec = fig.add_gridspec(nrows=3, ncols=3)
# set title for the whole figure
fig.suptitle('Uneven subplots grid example')
# create a subplot in 0th row but occupying all the columns
# we are using add_subplot on the figure handle and getting the
# subplot axis handle in return
ax0 = fig.add_subplot(gspec[0, :])
# set subplot title
ax0.set_title('gspec[0, :]')
# create a subplot in 1st row but occupying all the columns leaving the last column
ax1 = fig.add_subplot(gspec[1, :-1])
# set subplot title
ax1.set_title('gspec[1, :-1]')
# create a subplot occupying 1st till end rows and last column
ax2 = fig.add_subplot(gspec[1:, -1])
# set subplot title
ax2.set_title('gspec[1:, -1]')
# create a subplot in last row, 0th column
ax3 = fig.add_subplot(gspec[-1, 0])
# set subplot title
ax3.set_title('gspec[-1, 0]')
# create a subplot in last row, 2nd last column
ax4 = fig.add_subplot(gspec[-1, -2])
# set subplot title
ax4.set_title('gspec[-1, -2]')
# print the figure
plt.show()
Plot inside subplot using inset axes
call inset_axes
function on axes handle to create a plot inside the subplot
import matplotlib.pyplot as plt
# create a figure and axes handle
fig, ax = plt.subplots()
# plot data on the main axes
ax.plot([1,2,3],[9,5,4])
# create an inset axes
# the input array specifies the sizing and position of the inset axes
# in the form of lower-left corner coordinates of inset axes, and its width and height respectively as a fraction of original axes
axins = ax.inset_axes([0.5, 0.5, 0.47, 0.4])
axins.plot([7,8,9],[2,8,6])
# set title to the inset axes
axins.set_title('Inset Plot')
# print the figure
plt.show()
Please take time to refer to this official guide on creating matplotlib layouts with subplots for detailed and in depth explanation covering many use cases
Online Interpreter
Although we recommend to practice the above examples in Visual Studio Code, you can run these examples online at https://pynative.com/online-python-code-editor-to-execute-python-code/
References
- subplot layouts official guide - https://matplotlib.org/3.1.0/tutorials/intermediate/gridspec.html
- another post - https://towardsdatascience.com/data-visualization-using-matplotlib-16f1aae5ce70
- Examples gallery - https://matplotlib.org/gallery/index.html
Comments
Post a Comment