Open In App

How to Plot Multiple DataFrames in Subplots in Python

Last Updated : 09 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Plotting multiple dataframes in subplots is a powerful technique in data visualization, especially when comparing trends or patterns across different datasets. This approach allows you to display multiple plots within a single figure, making it easier to analyze relationships and differences between datasets.

What Does It Mean to Plot Multiple Dataframes in Subplots?

Plotting multiple dataframes in subplots involves creating a single figure that contains multiple smaller plots, each representing data from different dataframes. Each subplot can showcase different aspects of the data, facilitating comparisons and insights.

Three Good Examples of How to Plot Multiple Dataframes in Subplots

Example 1: Line Plots in Subplots

In this example, we'll plot line graphs from different dataframes in subplots using Matplotlib. This example demonstrates how to plot line graphs from different dataframes in separate subplots using Matplotlib. Each subplot represents data from a distinct dataframe (df1, df2, and df3). The plots use different markers, linestyles, and colors to distinguish between datasets

Python
import pandas as pd
import matplotlib.pyplot as plt

# Example dataframes
df1 = pd.DataFrame({'x': range(10), 'y1': range(10)})
df2 = pd.DataFrame({'x': range(10), 'y2': range(10, 20)})
df3 = pd.DataFrame({'x': range(10), 'y3': range(20, 30)})

# Create a figure with subplots
fig, axs = plt.subplots(3, 1, figsize=(8, 10))

# Plot each dataframe on its subplot
axs[0].plot(df1['x'], df1['y1'], marker='o', linestyle='-', color='b', label='df1')
axs[0].set_title('Dataframe 1')
axs[0].legend()

axs[1].plot(df2['x'], df2['y2'], marker='s', linestyle='--', color='r', label='df2')
axs[1].set_title('Dataframe 2')
axs[1].legend()

axs[2].plot(df3['x'], df3['y3'], marker='^', linestyle=':', color='g', label='df3')
axs[2].set_title('Dataframe 3')
axs[2].legend()

# Adjust layout and display the plot
plt.tight_layout()
plt.show()

Output

1

Example 2: Bar Plots in Subplots

This example demonstrates how to create bar plots from multiple dataframes in subplots. In this example, bar plots are created in separate subplots for two different dataframes (df1 and df2). Each subplot displays categorical data (category vs. values) using bars of different colors. Subplot titles and legends are utilized to differentiate between the two datasets (df1 and df2).

Python
import pandas as pd
import matplotlib.pyplot as plt

# Example dataframes
df1 = pd.DataFrame({'category': ['A', 'B', 'C'], 'values': [10, 20, 15]})
df2 = pd.DataFrame({'category': ['A', 'B', 'C'], 'values': [15, 18, 12]})

# Create a figure with subplots
fig, axs = plt.subplots(1, 2, figsize=(12, 6))

# Plot bar charts for each dataframe
axs[0].bar(df1['category'], df1['values'], color='b', alpha=0.7, label='df1')
axs[0].set_title('Dataframe 1')
axs[0].legend()

axs[1].bar(df2['category'], df2['values'], color='r', alpha=0.7, label='df2')
axs[1].set_title('Dataframe 2')
axs[1].legend()

# Adjust layout and display the plot
plt.tight_layout()
plt.show()

Output

2

Example 3: Scatter Plots in Subplots

In this example, we'll create scatter plots from multiple dataframes in subplots. This example showcases scatter plots from two different dataframes (df1 and df2) in separate subplots. Each subplot visualizes relationships between variables (x vs. y1 for df1 and x vs. y2 for df2) using different marker colors. Subplot titles and legends are included to distinguish between the datasets (df1 and df2)

Python
import pandas as pd
import matplotlib.pyplot as plt

# Example dataframes
df1 = pd.DataFrame({'x': range(10), 'y1': range(10)})
df2 = pd.DataFrame({'x': range(10), 'y2': range(10, 20)})

# Create a figure with subplots
fig, axs = plt.subplots(1, 2, figsize=(12, 6))

# Plot scatter plots for each dataframe
axs[0].scatter(df1['x'], df1['y1'], color='b', label='df1')
axs[0].set_title('Dataframe 1')
axs[0].legend()

axs[1].scatter(df2['x'], df2['y2'], color='r', label='df2')
axs[1].set_title('Dataframe 2')
axs[1].legend()

# Adjust layout and display the plot
plt.tight_layout()
plt.show()

Output

3

Conclusion

Plotting multiple dataframes in subplots enhances data visualization by enabling side-by-side comparisons of different datasets. Whether using line plots, bar plots, or scatter plots, the ability to plot dataframes in subplots helps in analyzing trends, relationships, and patterns effectively. By leveraging libraries like matplotlib, you can create insightful visualizations that aid in data exploration and decision-making processes.


Next Article
Practice Tags :

Similar Reads