The Wayback Machine - https://web.archive.org/web/20241115004843/https://www.geeksforgeeks.org/place-plots-side-by-side-in-matplotlib/
Open In App

Place plots side by side in Matplotlib

Last Updated : 09 Aug, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 libraries
import numpy as np
import matplotlib.pyplot as plt
 
 
# creating an array of data for x-axis
x = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
 
# data for y-axis
y_1 = 2*x
 
# data for y-axis for another plot
y_2 = 3*x
 
# using subplot function and creating plot one
plt.subplot(1, 2, 1)  # row 1, column 2, count 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 1, column 2, count 2
plt.subplot(1, 2, 2)
 
# g is for green color
plt.plot(x, y_2, 'g', linewidth=5)
plt.title('SECOND PLOT')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
 
# space between the plots
plt.tight_layout(4)
 
# show plot
plt.show()


 
 Output:

 

Example 2: In vertical form. 

Python3




# importing libraries
import numpy as np
import matplotlib.pyplot as plt
 
# creating an array of data for x-axis
x = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
 
# data for y-axis
y_1 = 2*x
 
# data for y-axis for another plot
y_2 = 3*x
 
# using subplot function and creating plot one
# row 2, column 1, count 1
plt.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 2
plt.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 plots
plt.tight_layout()
 
# show plot
plt.show()


 Output:

 

To increase the size of the plots we can write like this

plt.subplots(figsize(l, b))

Example 3:

Python3




# importing libraries
import numpy as np
import matplotlib.pyplot as plt
 
# creating an array of data for x-axis
x = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
 
# data for y-axis
y_1 = 2*x
 
# data for y-axis for another plot
y_2 = 3*x
 
# figsize() function to adjust the size
# of function
plt.subplots(figsize=(15, 5))
 
# using subplot function and creating
# plot one
plt.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 two
plt.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 plots
plt.tight_layout(4)
 
# show plot
plt.show()


Output:

 



Similar Reads

Surface plots and Contour plots in Python
Matplotlib was introduced keeping in mind, only two-dimensional plotting. But at the time when the release of 1.0 occurred, the 3d utilities were developed upon the 2d and thus, we have 3d implementation of data available today! The 3d plots are enabled by importing the mplot3d toolkit. In this article, we will discuss the surface plots and contour
4 min read
Visualising ML DataSet Through Seaborn Plots and Matplotlib
Working on data can sometimes be a bit boring. Transforming a raw data into an understandable format is one of the most essential part of the whole process, then why to just stick around on numbers, when we can visualize our data into mind-blowing graphs which are up for grabs in python. This article will focus on exploring plots which could make y
7 min read
Style Plots using Matplotlib
Matplotlib is the most popular package or library in Python which is used for data visualization. By using this library we can generate plots and figures, and can easily create raster and vector files without using any other GUIs. With matplotlib, we can style the plots like, an HTML webpage is styled by using CSS styles. We just need to import sty
3 min read
How to Remove Ticks from Matplotlib Plots?
Matplotlib is a Python library that offers us various functions using which we can plot our data and visualize it graphically. But often plotting a graph using the Matplotlib library, we get ticks in our plot which are marked by default on both sides of the plot on the x and y-axis. There can be cases when we don't want to show these ticks in our p
4 min read
How to Annotate Matplotlib Scatter Plots?
A scatter plot uses dots to represent values for two different numeric variables. In Python, we have a library matplotlib in which there is a function called scatter that helps us to create Scatter Plots. Here, we will use matplotlib.pyplot.scatter() method to plot. Syntax : matplotlib.pyplot.scatter(x,y) Parameters: x and y are float values and ar
3 min read
How to Show Gridlines on Matplotlib Plots?
In this article, we will see how we can add gridlines to a matplotlib graph and various configurable parameters that can be used in Python. Example: Create a matplotlib plot using sample data. Python Code # Importing the library import matplotlib.pyplot as plt # Define X and Y data points X = [12, 34, 23, 45, 67, 89] Y = [1, 3, 67, 78, 7, 5] # Plot
4 min read
Animating Scatter Plots in Matplotlib
An animated scatter plot is a dynamic records visualization in Python that makes use of a series of frames or time steps to reveal data points and exchange their positions or attributes over time. Each body represents a second in time, and the scatter plot is up to date for each frame, allowing you to peer traits, fluctuations, or moves in the info
3 min read
Changing the Datetime Tick Label Frequency for Matplotlib Plots
Matplotlib, a comprehensive Python library for creating static, animated, and interactive visualizations, provides extensive control over the appearance of plots, including the frequency of datetime tick labels. This article will delve into the techniques for customizing the datetime tick label frequency in Matplotlib plots, ensuring that your visu
3 min read
Plot multiple plots in Matplotlib
Prerequisites: Matplotlib In Matplotlib, we can draw multiple graphs in a single plot in two ways. One is by using subplot() function and other by superimposition of second graph on the first i.e, all graphs will appear on the same plot. We will look into both the ways one by one. Multiple Plots using subplot () FunctionA subplot () function is a w
6 min read
Changing the Color of Matplotlib's Violin Plots
Violin plots are a powerful visualization tool that combines box plots and density plots, making them ideal for displaying the distribution of a dataset across different categories. In this article, we will explore how to create and customize violin plots using Matplotlib, with a specific focus on changing their colors. Table of Content Creating a
4 min read
How to Place Legend Outside of the Plot in Matplotlib?
In this article, we will see how to put the legend outside the plot. Let's discuss some concepts : Matplotlib: Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John H
3 min read
Stem and Leaf Plots in Python
Stem and Leaf Plot is a way of representing the data. This plot is used to show the absolute frequency in different classes similar to the frequency distribution table or a histogram. It presents the quantitative data in the graphical format, and the stem-and-leaf plot of quantitative data is said as textual graph as that presents the data accordin
3 min read
Change Axis Labels, Set Title and Figure Size to Plots with Seaborn
Seaborn is Python's visualization library built as an extension to Matplotlib. Seaborn has Axes-level functions (scatterplot, regplot, boxplot, kdeplot, etc.) as well as Figure-level functions (lmplot, factorplot, jointplot, relplot etc.). Axes-level functions return Matplotlib axes objects with the plot drawn on them while figure-level functions i
4 min read
Show legend and label axes in 3D scatter plots in Python Plotly
In this article, we will learn how to show legend and label axes in 3D scatter plots in Python using the Plotly library. To create a 3D scatter plot in Plotly Express, you will need to provide the data for the x, y, and z coordinates of the points you want to plot. You can also customize the appearance of the plot, such as the size and color of the
3 min read
Ternary Plots in Plotly
A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. Ternary Plots in Plotly A ternary plot is also
2 min read
3D Volume Plots using Plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization library. Volume Plot in Plotly A volume plot is a plot wi
3 min read
3D Line Plots using Plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library Line Plot in Plotly Line plot in plotly is much ac
2 min read
3D Cone Plots using Plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. 3D Cone Plot in Plotly In plotly, a cone plot is
2 min read
Visualizing Relationship between variables with scatter plots in Seaborn
To understand how variables in a dataset are related to one another and how that relationship is dependent on other variables, we perform statistical analysis. This Statistical analysis helps to visualize the trends and identify various patterns in the dataset. One of the functions which can be used to get the relationship between two variables in
2 min read
3D Surface Plots using Plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. Surface Plot plotly Surface plot is those plot w
2 min read
3D Streamtube Plots using Plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. 3D Streamtube Plots In plotly, the streamtube pl
2 min read
Streamline Plots in Plotly using Python
A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. Streamline Plots in Plotly In plotly, streamli
2 min read
Quiver Plots using Plotly in Python
A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. Quiver Plots A quiver plot displays velocity v
2 min read
Contour Plots using Plotly in Python
A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. Contour Plots in Plotly A contour plot has a f
4 min read
How to make Log Plots in Plotly - Python?
A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. What are the log plots? A log plot is a way of
2 min read
Carpet Plots using Plotly in Python
A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization library. Carpet Plots A carpet plot is any of a few dif
3 min read
3D Mesh Plots using Plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library 3D Mesh Plots A Mesh plot is a plot that has thre
4 min read
How to Add a Title to Seaborn Plots?
In this article, we will discuss how to add a title to Seaborn Plots in Python. Method 1: Adding title using set methodset method takes 1 argument "title" as a parameter which stores Title of a plot. Syntax set(title="Title of a plot") Example: Here, we are going to create a dataframe with months and average temperatures C/C++ Code # import necessa
2 min read
How to Make Histograms with Density Plots with Seaborn histplot?
Histograms are visualization tools that represent the distribution of a set of continuous data. In a histogram, the data is divided into a set of intervals or bins (usually on the x-axis) and the count of data points that fall into each bin corresponding to the height of the bar above that bin. These bins may or may not be equal in width but are ad
3 min read
Density Plots with Pandas in Python
Density Plot is a type of data visualization tool. It is a variation of the histogram that uses 'kernel smoothing' while plotting the values. It is a continuous and smooth version of a histogram inferred from a data. Density plots uses Kernel Density Estimation (so they are also known as Kernel density estimation plots or KDE) which is a probabilit
3 min read
three90RightbarBannerImg