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 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:

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 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:

### 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.**