A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/matplotlib-pyplot-subplot-function-in-python/ below:

Matplotlib.pyplot.subplot() function in Python - GeeksforGeeks

Matplotlib.pyplot.subplot() function in Python

Last Updated : 20 May, 2022

Prerequisites: matplotlib

subplot() function adds subplot to a current figure at the specified grid position.  It is similar to the subplots() function however unlike subplots() it adds one subplot at a time. So to create multiple plots you will need several lines of code with the subplot() function. Another drawback of the subplot function is that it deletes the preexisting plot on your figure. Refer to example 1.

It is a wrapper of Figure.add_subplot.

Syntax:

subplot(nrows, ncols, index, **kwargs)

subplot(pos, **kwargs) 

subplot(ax)

Parameters : 

Returns :  An axes.SubplotBase subclass of Axes or a subclass of Axes. The returned axes base class depends on the projection used.

Implementation of the function is given below:

Example 1: subplot() will delete the pre-existing plot.

Python3
# importing the module
import matplotlib.pyplot as plt

# Data to display on plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 1, 2, 1]

# plot() will create new figure and will add axes object (plot) of above data
plt.plot(x, y, marker="x", color="green")

# subplot() will add plot to current figure deleting existing plot
plt.subplot(121)

Output: We can see that the first plot got set aside by the subplot() function. 

subplot_gfg

If you want to see the first plot comment out plt.subplot() line and you will see the following plot

plot_gfg

Example 2:

Python3
import matplotlib.pyplot as plt
# data to display on plots

x = [3, 1, 3]
y = [3, 2, 1]
z = [1, 3, 1]

# Creating figure object
plt.figure()

# adding first subplot
plt.subplot(121)
plt.plot(x, y, color="orange", marker="*")

# adding second subplot
plt.subplot(122)
plt.plot(z, y, color="yellow", marker="*")

Output :

multiple_subplots

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4