Table of Contents
Creating multiple matplotlib plots in one figure allows you to display and compare multiple data sets or visualizations within a single image. This can be achieved by using the subplot function to divide the figure into multiple axes, each representing a separate plot. This method provides a convenient way to present complex data in a concise and organized manner, making it easier to interpret and analyze. Additionally, it allows for easier customization and editing of the overall figure, as all plots are contained within one image. Overall, creating multiple matplotlib plots in one figure is a useful technique for visualizing and understanding data in a systematic and efficient way.
Create Multiple Matplotlib Plots in One Figure
You can use the following syntax to create multiple Matplotlib plots in one figure:
import matplotlib.pyplotas plt #define grid of plots fig, axs = plt.subplots(nrows=2, ncols=1) #add data to plots axs[0].plot(variable1, variable2) axs[1].plot(variable3, variable4)
The following examples show how to use this function in practice.
Example 1: Stack Plots Vertically
The following code shows how to create three Matplotlib plots, stacked vertically:
#create some data
var1 = [1, 2, 3, 4, 5, 6]
var2 = [7, 13, 16, 18, 25, 19]
var3 = [29, 25, 20, 25, 20, 18]
#define grid of plots
fig, axs = plt.subplots(nrows=3, ncols=1)
#add title
fig.suptitle('Plots Stacked Vertically')
#add data to plots
axs[0].plot(var1, var2)
axs[1].plot(var1, var3)
axs[2].plot(var2, var3)

Example 2: Stack Plots Horizontally
The following code shows how to create three Matplotlib plots, stacked horizontally:
#create some data
var1 = [1, 2, 3, 4, 5, 6]
var2 = [7, 13, 16, 18, 25, 19]
var3 = [29, 25, 20, 25, 20, 18]
#define grid of plots
fig, axs = plt.subplots(nrows=1, ncols=3)
#add title
fig.suptitle('Plots Stacked Horizontally')
#add data to plots
axs[0].plot(var1, var2)
axs[1].plot(var1, var3)
axs[2].plot(var2, var3)
Example 3: Create a Grid of Plots
The following code shows how to create a grid of Matplotlib plots:
#create some data
var1 = [1, 2, 3, 4, 5, 6]
var2 = [7, 13, 16, 18, 25, 19]
var3 = [29, 25, 20, 25, 20, 18]
var4 = [4, 4, 6, 4, 7, 11]
#define grid of plots
fig, axs = plt.subplots(nrows=2, ncols=2)
#add title
fig.suptitle('Grid of Plots')
#add data to plots
axs[0, 0].plot(var1, var2)
axs[0, 1].plot(var1, var3)
axs[1, 0].plot(var1, var4)
axs[1, 1].plot(var3, var1)

Example 4: Share Axes Between Plots
You can use the sharex and sharey arguments to ensure that multiple plots use the same x-axis:
#create some data
var1 = [1, 2, 3, 4, 5, 6]
var2 = [7, 13, 16, 18, 25, 19]
var3 = [29, 25, 20, 25, 20, 18]
var4 = [4, 4, 6, 4, 7, 11]
#define grid of plots
fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)#add title
fig.suptitle('Grid of Plots with Same Axes')
#add data to plots
axs[0, 0].plot(var1, var2)
axs[0, 1].plot(var1, var3)
axs[1, 0].plot(var1, var4)
axs[1, 1].plot(var3, var1)
Cite this article
stats writer (2024). How can I create multiple matplotlib plots in one figure?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-multiple-matplotlib-plots-in-one-figure/
stats writer. "How can I create multiple matplotlib plots in one figure?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-multiple-matplotlib-plots-in-one-figure/.
stats writer. "How can I create multiple matplotlib plots in one figure?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-multiple-matplotlib-plots-in-one-figure/.
stats writer (2024) 'How can I create multiple matplotlib plots in one figure?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-multiple-matplotlib-plots-in-one-figure/.
[1] stats writer, "How can I create multiple matplotlib plots in one figure?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I create multiple matplotlib plots in one figure?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
