“How can I use fig.add_subplot in Matplotlib?” 2

“How can I use fig.add_subplot in Matplotlib?”

Fig.add_subplot is a function in the Matplotlib library that allows for the creation of multiple subplots within a single figure. This function takes in three parameters: the number of rows, the number of columns, and the index of the subplot to be created. By using this function, users can efficiently plot and visualize multiple data sets or variables in a single figure, making it a useful tool for data analysis and presentation. The resulting subplots are arranged in a grid-like fashion, making it easy to compare and analyze the data.

Use fig.add_subplot in Matplotlib


You can use the following basic syntax to create subplots in Matplotlib:

import matplotlib.pyplotas plt

#define figure
fig = plt.figure()

#add first subplot in layout that has 3 rows and 2 columns
fig.add_subplot(321)

#add fifth subplot in layout that has 3 rows and 2 columns
fig.add_subplot(325)

...

The following examples show how to use this syntax in practice.

Example 1: Add Subplots with Even Layout

The following code shows how to create six subplots in a layout that has 3 rows and 2 columns:

import matplotlib.pyplotas plt

#define figure
fig = plt.figure()

#add subplots
fig.add_subplot(321).set_title('321')
fig.add_subplot(322).set_title('322')
fig.add_subplot(323).set_title('323')
fig.add_subplot(324).set_title('324')
fig.add_subplot(325).set_title('325')
fig.add_subplot(326).set_title('326')

#display plots
plt.show()

fig.add_subplot in Matplotlib

Notice that the result is six subplots displayed in a layout that has 3 rows and 2 columns.

Example 2: Add Subplots with Uneven Layout

The following code shows how to create four subplots in the following manner:

  • Three of the plots are created in a grid with 3 rows and 2 columns.
  • The fourth plot is created in a grid with 1 row and 2 columns.
import matplotlib.pyplotas plt

#define figure
fig = plt.figure()

#add subplots
fig.add_subplot(321).set_title('321')
fig.add_subplot(323).set_title('323')
fig.add_subplot(325).set_title('325')
fig.add_subplot(122).set_title('122')

#display plots
plt.show()

The end result is three subplots displayed in a 3×2 grid while the last subplot is displayed in a 1×2 grid.

Additional Resources

Cite this article

stats writer (2024). “How can I use fig.add_subplot in Matplotlib?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-fig-add_subplot-in-matplotlib/

stats writer. "“How can I use fig.add_subplot in Matplotlib?”." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-fig-add_subplot-in-matplotlib/.

stats writer. "“How can I use fig.add_subplot in Matplotlib?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-fig-add_subplot-in-matplotlib/.

stats writer (2024) '“How can I use fig.add_subplot in Matplotlib?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-fig-add_subplot-in-matplotlib/.

[1] stats writer, "“How can I use fig.add_subplot in Matplotlib?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. “How can I use fig.add_subplot in Matplotlib?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top