Matplotlib subplots() Function

Matplotlib subplots() Function

Let’s learn about the Matplotlib subplots() function. Matplotlib is a Python library for making 2D plots. In this tutorial, we will learn how to plot multiple plots in the same figure.

subplots() Function

Matplotlib subplots() is a function that allows us to create a grid of subplots in a single figure. This can be useful when we want to display multiple plots. We can plot several data on the same figure but split a figure into several sub-plots.

Syntax

The general syntax of the function is as follows:

plt.subplots()

Example

# Matlotlib Subplots
# Script to plot sin(x) and cos(x)
# Matplotlib Tutorials – www.TestingDocs.com

# Initialize modules
import numpy as np
import matplotlib.pyplot as plt

# Prepare data
fig, ax = plt.subplots()
X = np.linspace(0, 4 * np.pi, 500)
Y1= np.sin(X)
Y2= np.cos(X)

# Render the plots
ax.plot(X, Y1, X, Y2)
plt.show()

Matplotlib subplots() Function

Python Tutorials

Python Tutorial on this website can be found at:

https://www.testingdocs.com/python-tutorials/

More information on Python is available at the official website:

https://www.python.org