In this tutorial, we will look at how to plot multiple pandas dataframes on a grid of subplots (each dataframe on a separate subplot) with the help of some examples.
Steps to plot dataframes on different subplots
To plot multiple dataframe on a subplot, take the following steps –
- Create a grid of subplots using the
matplotlib.pyplot.subplots()
function. This will return the figure object and an array of Axes objects. Each Axes object represents a subplot. - For each dataframe, use the pandas
DataFrame.plot()
function to plot it on separate Axes object (generated in step 2), and use theax
parameter to specify the Axes object to use.
The following is the syntax –
Basic Syntax:
DataFrame.plot(*args, **kwargs)
Parameters:
- data – Series or DataFrame: The object for which the method is called.
- x – label or position: Only used if data is a DataFrame.
- y – label, position or list of label, positions: Allows plotting of one column versus another. Only used if data is a DataFrame.
- ax – matplotlib axes object: An axes of the current figure.
For more details about the parameters, refer this.
Let us now look at some examples of using the above steps.
Example 1 – Plot different dataframes in the same figure
Let’s create four dataframes and plot them using the DataFrame.plot()
function (each column will be plotted as a line) and plot them on a 2×2 subplot grid.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
import pandas as pd import numpy as np import matplotlib.pyplot as plt #create four DataFrames df1 = pd.DataFrame({'x': np.random.rand(10), 'y': np.random.rand(10)}) df2 = pd.DataFrame({'x': np.random.rand(10), 'y': np.random.rand(10)}) df3 = pd.DataFrame({'x': np.random.rand(10), 'y': np.random.rand(10)}) df4 = pd.DataFrame({'x': np.random.rand(10), 'y': np.random.rand(10)}) # generating subplots fig,axes = plt.subplots(nrows=2,ncols=2) #plotting the dataframes df1.plot(ax=axes[0][0]) df2.plot(ax=axes[0][1]) df3.plot(ax=axes[1][0]) df4.plot(ax=axes[1][1])
Output:
You can see that each dataframe is plotted on a separate subplot.
In the above example, we –
- Import the required modules.
- Create 4 dataframes, with random values generated using the
numpy.random.rand()
function. - Create a 2×2 subplot grid using the
matplotlib.pyplot.subplots()
method. - Plot each dataframe on a separate subplot using the
DataFrame.plot()
function and specifying Axes of the subplot using theax
parameter.
Example 2 – Plot different dataframes in the same figure using custom styling
You can also customize how each subplot is plotted. For example, you can change the line color, the line style, add titles, etc. Let’s plot a figure with multiple dataframe plots but this time add some styling to each of the subplots.
import pandas as pd import numpy as np import matplotlib.pyplot as plt #create four DataFrames df1 = pd.DataFrame({'x': np.linspace(0,10,10), 'y': np.random.rand(10)}) df2 = pd.DataFrame({'x': np.linspace(0,10,10), 'y': np.random.rand(10)}) df3 = pd.DataFrame({'x': np.linspace(0,10,10), 'y': np.random.rand(10)}) df4 = pd.DataFrame({'x': np.linspace(0,10,10), 'y': np.random.rand(10)}) # generating subplots fig,axes = plt.subplots(nrows=2,ncols=2) #plotting the dataframes with custom styling df1.plot(ax=axes[0][0], marker='.') df2.plot(ax=axes[0][1], marker='*') df3.plot(ax=axes[1][0], linestyle='dotted') df4.plot(ax=axes[1][1], linestyle='dashed')
Output:
Example 3 – Customize plot information
In the above examples, each dataframe is plotted in a subplot, and notice that each subplot contains a line (curve) for each column in the dataframe.
You can customize how the plot is actually built, for example, you can create only a single line plot and specify which column values to use on the x-axis and which column values to use on the y-axis.
Let’s replot the above figure with the x
column values plotted on the x-axis and the y
column values plotted on the y-axis in each of the subplots.
import pandas as pd import numpy as np import matplotlib.pyplot as plt #create four DataFrames df1 = pd.DataFrame({'x': np.linspace(0,10,10), 'y': np.random.rand(10)}) df2 = pd.DataFrame({'x': np.linspace(0,10,10), 'y': np.random.rand(10)}) df3 = pd.DataFrame({'x': np.linspace(0,10,10), 'y': np.random.rand(10)}) df4 = pd.DataFrame({'x': np.linspace(0,10,10), 'y': np.random.rand(10)}) # generating subplots fig,axes = plt.subplots(nrows=2,ncols=2) #plotting the dataframes df1.plot(x='x', y='y', ax=axes[0][0]) df2.plot(x='x', y='y', ax=axes[0][1]) df3.plot(x='x', y='y', ax=axes[1][0]) df4.plot(x='x', y='y', ax=axes[1][1])
Output:
Here, we use the values in the x
column as the x-axis values and the values in the y
column as the y-axis values.
You might also be interested in –
- How to Create Multiple Matplotlib Plots in One Figure?
- How To Make a Bubble Plot in Python with Matplotlib?
- Matplotlib – Create a Plot with two Y Axes and shared X Axis
- Add Title to Each Subplot in Matplotlib
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.