How to Plot Multiple DataFrames in Subplots in Python
Last Updated :
09 Jul, 2024
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
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
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
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.
Similar Reads
How to Create Multiple Subplots in Matplotlib in Python? To create multiple plots use matplotlib.pyplot.subplots method which returns the figure along with the objects Axes object or array of Axes object. nrows, ncols attributes of subplots() method determine the number of rows and columns of the subplot grid. By default, it returns a figure with a single
3 min read
How to Create Subplots in Matplotlib with Python? Matplotlib is a widely used data visualization library in Python that provides powerful tools for creating a variety of plots. One of the most useful features of Matplotlib is its ability to create multiple subplots within a single figure using the plt.subplots() method. This allows users to display
6 min read
How to Stack Multiple Pandas DataFrames? In this article, we will see how to stack Multiple Pandas Dataframe. Stacking means appending the dataframe rows to the second dataframe and so on. If there are 4 dataframes, then after stacking the result will be a single dataframe with an order of dataframe1,dataframe2,dataframe3,dataframe4. Panda
6 min read
Python Plotly - How to add multiple Y-axes? In this article let's see how to add multiple y-axes of different scales in Plotly charts in Python. Currently, Plotly Express does not support multiple Y axes on a single figure. So, we shall use Plotly go. Plotly provides a function called make_subplots() to plot charts with multiple Y - axes. Sy
5 min read
How to Add Multiple Axes to a Figure in Python In this article, we are going to discuss how to add multiple axes to a figure using matplotlib in Python. We will use the add_axes() method of matplotlib module to add multiple axes to a figure. Step-by-step Approach: Import required modulesAfter importing matplotlib, create a variable fig and equal
3 min read
Read multiple CSV files into separate DataFrames in Python Sometimes you might need to read multiple CSV files into separate Pandas DataFrames. Importing CSV files into DataFrames helps you work on the data using Python functionalities for data analysis. In this article, we will see how to read multiple CSV files into separate DataFrames. For reading only o
2 min read
Python Plotly - Subplots and Inset Plots Perquisites: Python Plotly One of the most deceptively-powerful features of Plotly data visualization is the ability for a viewer to quickly analyze a sufficient amount of information about data when pointing the cursor over the point label appears. In this article, we are going to see about the Su
3 min read
How to set the spacing between subplots in Matplotlib in Python? Let's learn how to set the spacing between the subplots in Matplotlib to ensure clarity and prevent the overlapping of plot elements, such as axes labels and titles.Let's create a simple plot with the default spacing to see how subplots can sometimes become congested.Pythonimport numpy as np import
3 min read
How to Plot Multiple Series from a Pandas DataFrame? In this article, we will discuss how to plot multiple series from a dataframe in pandas. Series is the range of the data  that include integer points we cab plot in pandas dataframe by using plot() function Syntax: matplotlib.pyplot(dataframe['column_name']) We can place n number of series and we ha
2 min read
How to plot multiple data columns in a DataFrame? Python comes with a lot of useful packages such as pandas, matplotlib, numpy, etc. To use DataFrame, we need a Pandas library and to plot columns of a DataFrame, we require matplotlib. Pandas has a tight integration with Matplotlib. You can plot data directly from your DataFrame using the plot() met
3 min read