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
How to plot two histograms side by side using Matplotlib?
To plot two histograms side by side using Matplotlib, you can use subplots to create multiple plotting areas. This technique is useful for comparing distributions of different datasets visually.
Basic Side-by-Side Histograms
Here's how to create two histograms using pandas DataFrames ?
import matplotlib.pyplot as plt
import pandas as pd
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data
df1 = pd.DataFrame(dict(a=[1, 1, 1, 1, 3, 2, 2, 3, 1, 2]))
df2 = pd.DataFrame(dict(b=[1, 1, 2, 1, 3, 3, 2, 2, 3, 1]))
# Create subplots
fig, axes = plt.subplots(1, 2)
# Plot histograms
df1.hist('a', ax=axes[0], title='Dataset A')
df2.hist('b', ax=axes[1], title='Dataset B')
plt.show()
Using Matplotlib Directly
You can also create side-by-side histograms without pandas ?
import matplotlib.pyplot as plt
import numpy as np
# Generate sample data
data1 = np.random.normal(100, 15, 1000)
data2 = np.random.normal(80, 10, 1000)
# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
# Plot histograms
ax1.hist(data1, bins=30, alpha=0.7, color='blue')
ax1.set_title('Distribution 1')
ax1.set_xlabel('Value')
ax1.set_ylabel('Frequency')
ax2.hist(data2, bins=30, alpha=0.7, color='red')
ax2.set_title('Distribution 2')
ax2.set_xlabel('Value')
ax2.set_ylabel('Frequency')
plt.tight_layout()
plt.show()
Customizing the Histograms
You can customize colors, bins, and labels for better visualization ?
import matplotlib.pyplot as plt
import numpy as np
# Sample data
scores_math = [85, 90, 88, 92, 87, 89, 91, 86, 88, 90]
scores_science = [78, 82, 85, 79, 81, 83, 80, 84, 82, 85]
# Create figure with subplots
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
# Customize first histogram
axes[0].hist(scores_math, bins=5, color='skyblue', edgecolor='black', alpha=0.8)
axes[0].set_title('Math Scores Distribution', fontsize=14)
axes[0].set_xlabel('Score')
axes[0].set_ylabel('Frequency')
# Customize second histogram
axes[1].hist(scores_science, bins=5, color='lightgreen', edgecolor='black', alpha=0.8)
axes[1].set_title('Science Scores Distribution', fontsize=14)
axes[1].set_xlabel('Score')
axes[1].set_ylabel('Frequency')
# Adjust spacing
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Description | Example |
|---|---|---|
bins |
Number of histogram bins | bins=30 |
alpha |
Transparency (0-1) | alpha=0.7 |
color |
Histogram color | color='blue' |
edgecolor |
Border color of bars | edgecolor='black' |
Conclusion
Use plt.subplots(1, 2) to create side-by-side histograms. This approach allows easy comparison of different datasets and provides flexibility for customization with colors, titles, and labels.
Advertisements
