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
Combining two heatmaps in seaborn
Combining two heatmaps in seaborn allows you to display and compare related datasets side by side. This is useful for analyzing correlations, patterns, or differences between two datasets.
Basic Approach
To combine two heatmaps, we use matplotlib subplots and create separate heatmaps on each subplot. Here's the step-by-step process ?
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
# Set figure size
plt.rcParams["figure.figsize"] = [10, 4]
# Create sample datasets
np.random.seed(42)
df1 = pd.DataFrame(np.random.rand(8, 4), columns=["A", "B", "C", "D"])
df2 = pd.DataFrame(np.random.rand(8, 4), columns=["W", "X", "Y", "Z"])
# Create subplots
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 4))
# Create heatmaps
sns.heatmap(df1, annot=True, fmt='.2f', cmap="Blues", ax=ax1, cbar=False)
sns.heatmap(df2, annot=True, fmt='.2f', cmap="Reds", ax=ax2, cbar=False)
# Set titles
ax1.set_title("Dataset 1")
ax2.set_title("Dataset 2")
# Adjust layout
plt.tight_layout()
plt.show()
Advanced Customization
You can customize the appearance by adjusting spacing, colors, and tick positions ?
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
# Create sample correlation matrices
np.random.seed(123)
data1 = np.random.randn(50, 4)
data2 = np.random.randn(50, 4)
df1 = pd.DataFrame(data1, columns=['Feature1', 'Feature2', 'Feature3', 'Feature4'])
df2 = pd.DataFrame(data2, columns=['Metric1', 'Metric2', 'Metric3', 'Metric4'])
# Calculate correlations
corr1 = df1.corr()
corr2 = df2.corr()
# Create figure with subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Plot heatmaps with shared colorbar
sns.heatmap(corr1, annot=True, cmap='coolwarm', center=0,
square=True, ax=ax1, cbar=False)
sns.heatmap(corr2, annot=True, cmap='coolwarm', center=0,
square=True, ax=ax2, cbar=True)
# Customize appearance
ax1.set_title('Correlation Matrix - Dataset 1', fontsize=14)
ax2.set_title('Correlation Matrix - Dataset 2', fontsize=14)
# Move y-axis labels to the right for second plot
ax2.yaxis.tick_right()
ax2.yaxis.set_label_position("right")
# Adjust spacing between subplots
plt.subplots_adjust(wspace=0.05)
plt.show()
Key Parameters
| Parameter | Purpose | Example Values |
|---|---|---|
ncols |
Number of subplot columns | 2 for side-by-side |
cbar |
Show colorbar | False for left, True for right |
wspace |
Width spacing between subplots | 0.01 to 0.3 |
cmap |
Color scheme | 'Blues', 'Reds', 'coolwarm' |
Common Use Cases
Before/After Analysis: Comparing data before and after processing
Model Comparison: Visualizing performance metrics from different models
Correlation Analysis: Comparing correlation matrices of different datasets
Time Series: Showing patterns across different time periods
Conclusion
Combining heatmaps in seaborn using matplotlib subplots enables effective side-by-side comparison of datasets. Use cbar=False on the left plot and customize spacing with wspace for optimal presentation.
