Place plots side by side in Matplotlib - GeeksforGeeks

19 Aug, 2021
Matplotlib is the most popular Python library for plotting graphs and visualizing our data. In Matplotlib we can create multiple plots by calling them once. To create multiple plots we use the subplot function of pyplot module in Matplotlib.
Syntax: plt.subplot(nrows, .ncolumns, index)
Parameters:
nrows is for number of rows means if the row is 1 then the plots lie horizontally.
ncolumns stands for column means if the column is 1 then the plot lie vertically.
and index is the count/index of plots. It starts with 1.
Approach:
Import libraries and modules.
Create data for plot.
Now, create a subplot using above function.
Give the parameters to the function according to the requirement.
Example 1:
Python3
# importing librariesimport numpy as npimport matplotlib.pyplot as plt  # creating an array of data for x-axisx = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20]) # data for y-axisy_1 = 2*x # dat for y-axis for another ploty_2 = 3*x # using subplot function and creating plot oneplt.subplot(1, 2, 1)  # row 1, column 2, count 1plt.plot(x, y_1, 'r', linewidth=5, linestyle=':')plt.title('FIRST PLOT')plt.xlabel('x-axis')plt.ylabel('y-axis') # using subplot function and creating plot two# row 1, column 2, count 2plt.subplot(1, 2, 2) # g is gor green colorplt.plot(x, y_2, 'g', linewidth=5)plt.title('SECOND PLOT')plt.xlabel('x-axis')plt.ylabel('y-axis') # space between the plotsplt.tight_layout(4) # show plotplt.show()
  Output:
 
Example 2: In vertical form. 
Python3
# importing librariesimport numpy as npimport matplotlib.pyplot as plt # creating an array of data for x-axisx = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20]) # data for y-axisy_1 = 2*x # dat for y-axis for another ploty_2 = 3*x # using subplot function and creating plot one# row 2, column 1, count 1plt.subplot(2, 1, 1)plt.plot(x, y_1, 'r', linewidth=5, linestyle=':')plt.title('FIRST PLOT')plt.xlabel('x-axis')plt.ylabel('y-axis') # using subplot function and creating plot two# row 2, column 1, count 2plt.subplot(2, 1, 2)plt.plot(x, y_2, 'g', linewidth=5)plt.title('SECOND PLOT')plt.xlabel('x-axis')plt.ylabel('y-axis') # space between the plotsplt.tight_layout() # show plotplt.show()
 Output:
 
To increase the size of the plots we can write like this
plt.subplots(figsize(l, b))
Example 3:
Python3
# importing librariesimport numpy as npimport matplotlib.pyplot as plt # creating an array of data for x-axisx = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20]) # data for y-axisy_1 = 2*x # dat for y-axis for another ploty_2 = 3*x # figsize() function to adjust the size# og functionplt.subplots(figsize=(15, 5)) # using subplot function and creating# plot oneplt.subplot(1, 2, 1)plt.plot(x, y_1, 'r', linewidth=5, linestyle=':')plt.title('FIRST PLOT')plt.xlabel('x-axis')plt.ylabel('y-axis') # using subplot function and creating plot twoplt.subplot(1, 2, 2)plt.plot(x, y_2, 'g', linewidth=5)plt.title('SECOND PLOT')plt.xlabel('x-axis')plt.ylabel('y-axis') # space between the plotsplt.tight_layout(4) # show plotplt.show()
Output:
 
anikakapoor
kalrap615
Picked
Python-matplotlib
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()