لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

Python Data Science Matplotlib Subplot the subplot() function

Subplot function() is used to display Multiple Plots.
With the subplot() function you can draw multiple plots in one figure.

Example 1: Draw 2 plots.

Code

import matplotlib.pyplot as plt
import numpy as np

#plot 1: x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x,y)

#plot 2: x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)

plt.show()
the output will be


The subplot() Function

Explanation of the code.

The subplot() function takes three arguments that describes the layout of the figure.

The layout is organized in rows and columns, which are represented by the first and second argument.

The third argument represents the index of the current plot.

plt.subplot(1, 2, 1)

#the figure has 1 row, 2 columns, and this plot is the first plot.

plt.subplot(1, 2, 2)

#the figure has 1 row, 2 columns, and this plot is the second plot.

So, if we want a figure with 2 rows an 1 column (meaning that the two plots will be displayed on top of each other instead of side-by-side), we can write the syntax like this.

Example 2: Draw 2 plots on top of each other.

Code

import matplotlib.pyplot as plt
import numpy as np

#plot 1: x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 1, 1)
plt.plot(x,y)

#plot 2: x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 1, 2)
plt.plot(x,y)

plt.show()

the output will be


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

Example 3: Draw 6 plots.

Code

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([4, 8, 1, 10])

plt.subplot(2, 3, 1)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([15, 25, 35, 45])

plt.subplot(2, 3, 2)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([5, 8, 1, 10])

plt.subplot(2, 3, 3)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([25, 35, 45, 55])

plt.subplot(2, 3, 4)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([5, 8, 1, 10])

plt.subplot(2, 3, 5)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([5, 15, 25, 35])

plt.subplot(2, 3, 6)
plt.plot(x,y)
plt.show()

the output will be


Title: can be added to each plot with the title() function.

Example 4: Add title to above example 3.

Code

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([4, 8, 1, 10])

plt.subplot(2, 3, 1)
plt.plot(x,y)
plt.title("SALES 1")

x = np.array([0, 1, 2, 3])
y = np.array([15, 25, 35, 45])

plt.subplot(2, 3, 2)
plt.plot(x,y)
plt.title("INCOME 1")

x = np.array([0, 1, 2, 3])
y = np.array([5, 8, 1, 10])

plt.subplot(2, 3, 3)
plt.plot(x,y)
plt.title("SALES 2")

x = np.array([0, 1, 2, 3])
y = np.array([25, 35, 45, 55])

plt.subplot(2, 3, 4)
plt.plot(x,y)
plt.title("INCOME 2")

x = np.array([0, 1, 2, 3])
y = np.array([5, 8, 1, 10])

plt.subplot(2, 3, 5)
plt.plot(x,y)
plt.title("SALES 3")

x = np.array([0, 1, 2, 3])
y = np.array([5, 15, 25, 35])

plt.subplot(2, 3, 6)
plt.plot(x,y)
plt.title("INCOME 3")

plt.show()

the output will be


Super Title: A title/Super Title can be added to the entire figure with the suptitle() function.

Example 5: Add title for the entire figure.

Code

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([4, 8, 1, 10])

plt.subplot(2, 3, 1)
plt.plot(x,y)
plt.title("SALES 1")

x = np.array([0, 1, 2, 3])
y = np.array([15, 25, 35, 45])

plt.subplot(2, 3, 2)
plt.plot(x,y)
plt.title("INCOME 1")

x = np.array([0, 1, 2, 3])
y = np.array([5, 8, 1, 10])

plt.subplot(2, 3, 3)
plt.plot(x,y)
plt.title("SALES 2")

x = np.array([0, 1, 2, 3])
y = np.array([25, 35, 45, 55])

plt.subplot(2, 3, 4)
plt.plot(x,y)
plt.title("INCOME 2")

x = np.array([0, 1, 2, 3])
y = np.array([5, 8, 1, 10])

plt.subplot(2, 3, 5)
plt.plot(x,y)
plt.title("SALES 3")

x = np.array([0, 1, 2, 3])
y = np.array([5, 15, 25, 35])

plt.subplot(2, 3, 6)
plt.plot(x,y)
plt.title("INCOME 3")

plt.suptitle("Sales At MY SHOP")

plt.show()

the output will be