# Understanding Matplotlib Subplots

Matplotlib subplots allows us to display multiple plots in a single figure. In this article, we’ll explore the Matplotlib subplots and learn how to use them effectively in our data visualization projects.

## What are Matplotlib Subplots?

Matplotlib subplots are a way to display multiple plots in a single figure. By creating subplots, we can compare different data sets or visualize different aspects of the same data in a single view.

The subplots are arranged in a grid layout, where each cell of the grid contains a plot.

To create subplots in Matplotlib, we use the subplots() function.

The subplots() function takes two optional parameters, nrows and ncols, which specify the number of rows and columns in the subplot grid, respectively.

## Multiple Plots

In Matplotlib subplots, you can visualize multiple plots in one figure by calling the subplots() function:

Plot the following two plots by implementing the subplot() function:

#### Example:

import matplotlib.pyplot as pt import numpy as npy #graph 1: even =npy.array([2, 4, 6, 8, 10]) rev_odd = npy.array([11, 9, 7, 5, 3]) pt.subplot(1, 2, 1) pt.plot(even, rev_odd) #graph 2: odd = npy.array([1, 3, 5, 7, 9]) rev_even = npy.array([10, 8, 6, 4, 2]) pt.subplot(1, 2, 2) pt.plot(odd,rev_even) pt.show()

Add two graphs in one row by utilizing the subplot() function:

#### Example:

import matplotlib.pyplot as pt import numpy as npy #graph 1: rand_fact =npy.array([24, 1, 6, 120, 2]) rand_prime = npy.array([17, 57, 23, 2, 11]) pt.subplot(1, 2, 1) pt.plot(rand_fact, rand_prime) #graph 2: rand_odd = npy.array([87, 63, 71, 27, 9]) rand_even = npy.array([28, 46, 32, 6, 18]) pt.subplot(1, 2, 2) pt.plot(rand_odd,rand_even) pt.show()

## Matplotlib subplots() Function

Three arguments are passed to the subplots() function to provide information about the layout of the figure.

In the layout, rows and columns are referred to as the first and second arguments.

According to Matplotlib subplots, the third argument indicates the index of the active graph.

```pt.subplot(1, 2, 1)
#The figure has one row, two columns, and this graph is the first graph in the figure.

plt.subplot(1, 2, 2)
#The figure has one row, two columns, and this graph is the second graph in the figure.```

Therefore, if we want a figure with 2 rows and 1 column (which means that the two plots are presented on top of one another instead of in parallel),
By utilizing this syntax, we can write:

Utilize two rows and one column in the following example:

#### Example:

import matplotlib.pyplot as pt import numpy as npy #graph 1: even =npy.array([2, 4, 6, 8, 10]) rev_odd = npy.array([11, 9, 7, 5, 3]) pt.subplot(2, 1, 1) pt.plot(even, rev_odd) #graph 2: odd = npy.array([1, 3, 5, 7, 9]) rev_even = npy.array([10, 8, 6, 4, 2]) pt.subplot(2, 1, 2) pt.plot(odd,rev_even) pt.show()

Apply subplot(2,1,2) in the below example:

#### Example:

import matplotlib.pyplot as pt import numpy as npy #graph 1: rand_fact =npy.array([24, 1, 6, 120, 2]) rand_prime = npy.array([17, 57, 23, 2, 11]) pt.subplot(2, 1, 1) pt.plot(rand_fact, rand_prime) #graph 2: rand_odd = npy.array([87, 63, 71, 27, 9]) rand_even = npy.array([28, 46, 32, 6, 18]) pt.subplot(2, 1, 2) pt.plot(rand_odd,rand_even) pt.show()

Indicate the number of rows, columns, and the graph index in one figure, and you can make as many graphs as you want.

Create four graphs as follows:

#### Example:

import matplotlib.pyplot as pt import numpy as npy #graph 1: even =npy.array([2, 4, 6, 8, 10]) rev_odd = npy.array([11, 9, 7, 5, 3]) pt.subplot(1, 4, 1) pt.plot(even, rev_odd) #graph 2: random1 = npy.array([147, 73, 111, 89, 33]) random2 = npy.array([31, 81, 55, 97, 77]) pt.subplot(1, 4, 2) pt.plot(random1,random2) #graph 3: odd = npy.array([1, 3, 5, 7, 9]) rev_even = npy.array([10, 8, 6, 4, 2]) pt.subplot(1, 4, 3) pt.plot(odd,rev_even) #graph 4: random1 = npy.array([147, 73, 111, 89, 33]) random2 = npy.array([31, 81, 55, 97, 77]) pt.subplot(1, 4, 4) pt.plot(random1,random2) pt.show()

Sketch eight graphs in four by two dimensions:

#### Example:

import matplotlib.pyplot as pt import numpy as npy #graph 1: random1 = npy.array([147, 73, 111, 89, 33]) random2 = npy.array([31, 81, 55, 97, 77]) pt.subplot(4, 2, 1) pt.plot(random1,random2) #graph 2: rand_fact =npy.array([24, 1, 6, 120, 2]) rand_prime = npy.array([17, 57, 23, 2, 11]) pt.subplot(4, 2, 2) pt.plot(rand_fact, rand_prime) #graph 3: rand_odd = npy.array([87, 63, 71, 27, 9]) rand_even = npy.array([28, 46, 32, 6, 18]) pt.subplot(4, 2, 3) pt.plot(rand_odd,rand_even) #graph 4: even =npy.array([2, 4, 6, 8, 10]) rev_odd = npy.array([11, 9, 7, 5, 3]) pt.subplot(4, 2, 4) pt.plot(even, rev_odd) #graph 5: rand_fact =npy.array([24, 1, 6, 120, 2]) rand_prime = npy.array([17, 57, 23, 2, 11]) pt.subplot(4, 2, 5) pt.plot(rand_fact, rand_prime) #graph 6: random1 = npy.array([147, 73, 111, 89, 33]) random2 = npy.array([31, 81, 55, 97, 77]) pt.subplot(4, 2, 6) pt.plot(random1,random2) #graph 7: rand_odd = npy.array([87, 63, 71, 27, 9]) rand_even = npy.array([28, 46, 32, 6, 18]) pt.subplot(4, 2, 7) pt.plot(rand_odd,rand_even) #graph 8: odd = npy.array([1, 3, 5, 7, 9]) rev_even = npy.array([10, 8, 6, 4, 2]) pt.subplot(4, 2, 8) pt.plot(odd,rev_even) pt.show()

## Title

By calling the title() function, you can assign a title to each graph:

In the following example plot the two graphs and provide a separate title for both:

#### Example:

Insert a title in each graph by applying the title() function:

## Super Title

Through the suptitle() function, you can include a title to the complete figure:

For the whole figure, provide the title ‘MULTIPLE GRAPHS‘:

#### Example:

import matplotlib.pyplot as pt import numpy as npy #graph 1: even =npy.array([2, 4, 6, 8, 10]) rev_odd = npy.array([11, 9, 7, 5, 3]) pt.subplot(1, 4, 1) pt.plot(even, rev_odd) #graph 2: random1 = npy.array([147, 73, 111, 89, 33]) random2 = npy.array([31, 81, 55, 97, 77]) pt.subplot(1, 4, 2) pt.plot(random1,random2) #graph 3: odd = npy.array([1, 3, 5, 7, 9]) rev_even = npy.array([10, 8, 6, 4, 2]) pt.subplot(1, 4, 3) pt.plot(odd,rev_even) #graph 4: random1 = npy.array([147, 73, 111, 89, 33]) random2 = npy.array([31, 81, 55, 97, 77]) pt.subplot(1, 4, 4) pt.plot(random1,random2) pt.suptitle('MULTIPLE GRAPHS') pt.show()

In the following example, we assign a super title ‘MR.EXAMPLES’ to the whole figure:

#### Example:

import matplotlib.pyplot as pt import numpy as npy #graph 1: random1 = npy.array([147, 73, 111, 89, 33]) random2 = npy.array([31, 81, 55, 97, 77]) pt.subplot(4, 2, 1) pt.plot(random1,random2) #graph 2: rand_fact =npy.array([24, 1, 6, 120, 2]) rand_prime = npy.array([17, 57, 23, 2, 11]) pt.subplot(4, 2, 2) pt.plot(rand_fact, rand_prime) #graph 3: rand_odd = npy.array([87, 63, 71, 27, 9]) rand_even = npy.array([28, 46, 32, 6, 18]) pt.subplot(4, 2, 3) pt.plot(rand_odd,rand_even) #graph 4: even =npy.array([2, 4, 6, 8, 10]) rev_odd = npy.array([11, 9, 7, 5, 3]) pt.subplot(4, 2, 4) pt.plot(even, rev_odd) #graph 5: rand_fact =npy.array([24, 1, 6, 120, 2]) rand_prime = npy.array([17, 57, 23, 2, 11]) pt.subplot(4, 2, 5) pt.plot(rand_fact, rand_prime) #graph 6: random1 = npy.array([147, 73, 111, 89, 33]) random2 = npy.array([31, 81, 55, 97, 77]) pt.subplot(4, 2, 6) pt.plot(random1,random2) #graph 7: rand_odd = npy.array([87, 63, 71, 27, 9]) rand_even = npy.array([28, 46, 32, 6, 18]) pt.subplot(4, 2, 7) pt.plot(rand_odd,rand_even) #graph 8: odd = npy.array([1, 3, 5, 7, 9]) rev_even = npy.array([10, 8, 6, 4, 2]) pt.subplot(4, 2, 8) pt.plot(odd,rev_even) pt.suptitle('MR.EXAMPLES', C = 'Maroon', fontweight = 'bold') pt.show()

### Example Explanation

The above example is a Python code that creates a figure containing eight subplots using the Matplotlib library. Each subplot contains a plot of two NumPy arrays. The arrays contain randomly generated integers, prime numbers, odd and even numbers, and factorials.

1. The first subplot displays a plot of two arrays named “random1” and “random2.”
2. The second subplot shows a plot of “rand_fact” and “rand_prime” arrays.
3. The third subplot contains a plot of “rand_odd” and “rand_even” arrays.
4. The fourth subplot shows a plot of “even” and “rev_odd” arrays.
5. The fifth subplot displays the same plot as the second subplot.
6. The sixth subplot contains the same plot as the first subplot.
7. The seventh subplot shows the same plot as the third subplot.
8. The eighth subplot displays a plot of “odd” and “rev_even” arrays.

The figure has a super title “MR.EXAMPLES” in maroon color and bold font weight. Finally, the Matplotlib function “show()” is used to display the created figure.

## Matplotlib Subplots Benefits

Matplotlib subplots enable the creation of multiple plots within a single figure, offering various advantages, such as:

• With Matplotlib subplots, multiple plots can be presented in a condensed manner, making it easier to display large amounts of data in a limited space.
• The presence of multiple plots within a single figure simplifies comparing different datasets and exploring their correlations.
• Subplots enhance the overall visualization of data by allowing users to view multiple plots simultaneously, side-by-side.
• Matplotlib subplots offer flexibility in terms of layout customization, enabling users to adjust the number of rows and columns or the size and position of each subplot.
• Subplots can be utilized to tell a more compelling data-driven story by emphasizing different features and comparisons of the data.

### Conclusion

Matplotlib subplots provide numerous advantages for visualizing data. By permitting the creation of multiple plots in one figure, subplots enable users to save space, compare datasets with ease, enhance visualization, customize layouts, and create better stories with data. These benefits make Matplotlib subplots an adaptable and flexible option for presenting insights efficiently, which explains why they are a popular choice for data visualization professionals and enthusiasts.

+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0 