Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Plot multiple boxplots in one graph in Pandas or Matplotlib
To plot multiple boxplots in one graph in Pandas or Matplotlib, you can create side-by-side boxplots to compare distributions across different datasets or categories.
Using Pandas DataFrame.plot()
The simplest approach is using Pandas' built-in plotting functionality with kind='box' parameter ?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
np.random.seed(42)
data = pd.DataFrame({
"Dataset_A": np.random.normal(50, 15, 100),
"Dataset_B": np.random.normal(60, 10, 100),
"Dataset_C": np.random.normal(45, 20, 100)
})
# Plot multiple boxplots
ax = data.plot(kind='box', title='Multiple Boxplots Comparison')
plt.ylabel('Values')
plt.show()
Displays three boxplots side by side comparing the distributions of Dataset_A, Dataset_B, and Dataset_C
Using Matplotlib Directly
For more control over the appearance, you can use Matplotlib's boxplot() function directly ?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
np.random.seed(42)
group1 = np.random.normal(100, 10, 50)
group2 = np.random.normal(90, 15, 50)
group3 = np.random.normal(110, 8, 50)
# Create the boxplot
plt.figure(figsize=(8, 6))
plt.boxplot([group1, group2, group3],
labels=['Group 1', 'Group 2', 'Group 3'],
patch_artist=True)
plt.title('Multiple Boxplots using Matplotlib')
plt.ylabel('Values')
plt.grid(True, alpha=0.3)
plt.show()
Displays three customized boxplots with different colors and grid lines
Plotting from Different Columns
You can also create boxplots by selecting specific columns from a larger DataFrame ?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create a DataFrame with multiple columns
np.random.seed(42)
df = pd.DataFrame({
'Temperature': np.random.normal(25, 5, 80),
'Humidity': np.random.normal(60, 10, 80),
'Pressure': np.random.normal(1013, 20, 80),
'Wind_Speed': np.random.normal(15, 8, 80)
})
# Select specific columns and plot
selected_columns = ['Temperature', 'Humidity', 'Wind_Speed']
df[selected_columns].plot(kind='box', figsize=(10, 6))
plt.title('Weather Data Comparison')
plt.ylabel('Measurements')
plt.xticks(rotation=45)
plt.show()
Shows boxplots for Temperature, Humidity, and Wind_Speed with rotated x-axis labels
Key Parameters
| Parameter | Description | Example |
|---|---|---|
kind='box' |
Specifies boxplot type | Required for Pandas plot() |
figsize |
Sets figure dimensions | (10, 6) for width×height |
title |
Chart title | 'Comparison Plot' |
labels |
X-axis labels (Matplotlib) | ['Group A', 'Group B'] |
Conclusion
Use DataFrame.plot(kind='box') for quick multiple boxplots from Pandas data. For advanced customization, use plt.boxplot() directly with Matplotlib to control colors, labels, and styling options.
Advertisements
