Pandas: How to Plot Multiple DataFrames in Subplots


You can use the following basic syntax to plot multiple pandas DataFrames in subplots:

import matplotlib.pyplot as plt

#define subplot layout
fig, axes = plt.subplots(nrows=2, ncols=2)

#add DataFrames to subplots
df1.plot(ax=axes[0,0])
df2.plot(ax=axes[0,1])
df3.plot(ax=axes[1,0])
df4.plot(ax=axes[1,1])

The following example shows how to use this syntax in practice.

Example: Plot Multiple Pandas DataFrames in Subplots

Suppose we have four pandas DataFrames that contain information on sales and returns at four different retail stores:

import pandas as pd

#create four DataFrames
df1 = pd.DataFrame({'sales': [2, 5, 5, 7, 9, 13, 15, 17, 22, 24],
                    'returns': [1, 2, 3, 4, 5, 6, 7, 8, 7, 5]})

df2 = pd.DataFrame({'sales': [2, 5, 11, 18, 15, 15, 14, 9, 6, 7],
                    'returns': [1, 2, 0, 2, 2, 4, 5, 4, 2, 1]})

df3 = pd.DataFrame({'sales': [6, 8, 8, 7, 8, 9, 10, 7, 8, 12],
                    'returns': [1,0, 1, 1, 1, 2, 3, 2, 1, 3]})

df4 = pd.DataFrame({'sales': [10, 7, 7, 6, 7, 6, 4, 3, 3, 2],
                    'returns': [4, 4, 3, 3, 2, 3, 2, 1, 1, 0]})

We can use the following syntax to plot each of these DataFrames in a subplot that has a layout of 2 rows and 2 columns:

import matplotlib.pyplot as plt

#define subplot layout
fig, axes = plt.subplots(nrows=2, ncols=2)

#add DataFrames to subplots
df1.plot(ax=axes[0,0])
df2.plot(ax=axes[0,1])
df3.plot(ax=axes[1,0])
df4.plot(ax=axes[1,1])

pandas subplots

Each of the four DataFrames is displayed in a subplot.

Note that we used the axes argument to specify where each DataFrame should be placed.

For example, the DataFrame called df1 was placed in the position with a row index value of 0 and a column index value of 0 (e.g. the subplot in the upper left corner).

Also note that you can change the layout of the subplots by using the nrows and ncols arguments.

For example, the following code shows how to arrange the subplots in four rows and one column:

import matplotlib.pyplot as plt

#define subplot layout
fig, axes = plt.subplots(nrows=4, ncols=1)

#add DataFrames to subplots
df1.plot(ax=axes[0])
df2.plot(ax=axes[1])
df3.plot(ax=axes[2])
df4.plot(ax=axes[3])

The subplots are now arranged in a layout with four rows and one column.

Note that if you’d like the subplots to have the same y-axis and x-axis scales, you can use the sharey and sharex arguments.

For example, the following code shows how to use the sharey argument to force all of the subplots to have the same y-axis scale:

import matplotlib.pyplot as plt

#define subplot layout, force subplots to have same y-axis scale
fig, axes = plt.subplots(nrows=4, ncols=1, sharey=True)

#add DataFrames to subplots
df1.plot(ax=axes[0])
df2.plot(ax=axes[1])
df3.plot(ax=axes[2])
df4.plot(ax=axes[3])

Notice that the y-axis for each subplot now ranges from 0 to 20.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Create Pie Chart from Pandas DataFrame
How to Make a Scatterplot From Pandas DataFrame
How to Create a Histogram from Pandas DataFrame

Leave a Reply

Your email address will not be published.