codingstreets
Search
Close this search box.
python-matplotlib-subplots

Introduction to Python Matplotlib Subplots

In This Article, You Will Learn About Python Matplotlib Subplots.

Python Matplotlib Subplots – Before moving ahead, let’s take a look at Python Matplotlib Grid Lines

Table of Contents

The subplots() Function

The subplots() function takes three arguments to describe how the figure is laid out.

The layout is divided into rows and columns, shown in two arguments: the first and second argument.

The third argument is what is known as the index on the plot currently in use.

Display Multiple Plots

To multiple plots in a single graph, we can use subplot() function.

Example: Adding subplots to graph.

				
					import matplotlib.pyplot as plt
import numpy as np

#plot 1
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
plt.subplot(1, 2, 1)
plt.plot(x_axis, y_axis)

#plot 2
x_axis = np.array([20, 28, 15, 26, 38])
y_axis = np.array([19, 26, 37, 21, 19])

plt.subplot(1, 2, 2)
plt.plot(x_axis, y_axis)

plt.show()

				
			
python-matplotlib-subplots

As shown above, it returned a plot that has two graphs.

Now, if we want a figure with 3 rows a 1 column, we can write the syntax like this –

Example: Adding three subplots to graph.

				
					import matplotlib.pyplot as plt
import numpy as np

#plot 1
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
plt.subplot(3, 1, 1)
plt.plot(x_axis, y_axis)

#plot 2
x_axis = np.array([20, 28, 15, 26, 38])
y_axis = np.array([19, 26, 37, 21, 19])
plt.subplot(3, 1, 2)
plt.plot(x_axis, y_axis)

#plot 3
x_axis = np.array([20, 28, 15, 26, 38])
y_axis = np.array([19, 26, 37, 21, 19])
plt.subplot(3, 1, 3)
plt.plot(x_axis, y_axis)

plt.show()

				
			
python-matplotlib-subplots

As shown above, it returned a plot that has three graphs.

You can draw as many plots as you like on one figure; describe the number of rows, columns, and plot index.

Example: Adding four subplots to graph.

				
					import matplotlib.pyplot as plt
import numpy as np

#plot 1
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
plt.subplot(2, 2, 1)
plt.plot(x_axis, y_axis)

#plot 2
x_axis = np.array([20, 28, 10, 26, 38])
y_axis = np.array([9, 16, 27, 22, 19])
plt.subplot(2, 2, 2)
plt.plot(x_axis, y_axis)

#plot 3
x_axis = np.array([20, 17, 21, 26, 32])
y_axis = np.array([15, 21, 24, 23, 18])
plt.subplot(2, 2, 3)
plt.plot(x_axis, y_axis)

#plot 4
x_axis = np.array([10, 20, 15, 26, 38])
y_axis = np.array([14, 26, 17, 10, 19])
plt.subplot(2, 2, 4)
plt.plot(x_axis, y_axis)

plt.show()

				
			

As shown above, it returned a plot that has four graphs.

Title

To add title in graph, we can use title() function.

Example: Adding title name to graphs.

				
					import matplotlib.pyplot as plt
import numpy as np

#plot 1
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
plt.subplot(2, 1, 1)
plt.plot(x_axis, y_axis)
plt.title("Record-x")

#plot 2
x_axis = np.array([20, 28, 15, 26, 38])
y_axis = np.array([19, 26, 37, 21, 19])
plt.subplot(2, 1, 2)
plt.plot(x_axis, y_axis)
plt.title("Record-y")

plt.show()

				
			

As a result, it returned a graph after adding title for each graph. 

Super Title

To add a title name to entire graph, we can use supertitle() function.

Example: Adding a title name to entire graph.

				
					import matplotlib.pyplot as plt
import numpy as np

#plot 1
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
plt.subplot(2, 1, 1)
plt.plot(x_axis, y_axis)
plt.title("Record-x")

#plot 2
x_axis = np.array([20, 28, 15, 26, 38])
y_axis = np.array([19, 26, 37, 21, 19])
plt.subplot(2, 1, 2)
plt.plot(x_axis, y_axis)
plt.title("Record-y")

plt.suptitle("FY-2020")

plt.show()

				
			
python-matplotlib-subplots

As shown above, it returned a graph after adding title name as entire name of the graph.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on