Skip to Content

Pandas – Plot Multiple Dataframes in Subplots

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 –

  1. 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.
  2. For each dataframe, use the pandas DataFrame.plot() function to plot it on separate Axes object (generated in step 2), and use the ax 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.

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:

multiple dataframes on a plot with subplots

You can see that each dataframe is plotted on a separate subplot.

In the above example, we –

  1. Import the required modules.
  2. Create 4 dataframes, with random values generated using the numpy.random.rand() function.
  3. Create a 2×2 subplot grid using the matplotlib.pyplot.subplots() method.
  4. Plot each dataframe on a separate subplot using the DataFrame.plot() function and specifying Axes of the subplot using the ax 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:

subplots with custom styling

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:

the resulting grid of subplots with x column values as x-axis and y column values as y-axis

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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Authors

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

  • Chaitanya Betha

    I'm an undergrad student at IIT Madras interested in exploring new technologies. I have worked on various projects related to Data science, Machine learning & Neural Networks, including image classification using Convolutional Neural Networks, Stock prediction using Recurrent Neural Networks, and many more machine learning model training. I write blog articles in which I would try to provide a complete guide on a particular topic and try to cover as many different examples as possible with all the edge cases to understand the topic better and have a complete glance over the topic.