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 hide the colorbar of a Seaborn heatmap?
To hide the colorbar of a Seaborn heatmap, we can use cbar=False in the heatmap() method. This is useful when you want to focus on the pattern visualization without showing the color scale legend.
Basic Example
Here's how to create a heatmap without a colorbar −
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Set figure size
plt.figure(figsize=(8, 6))
# Create sample data
data = np.random.random((4, 4))
df = pd.DataFrame(data, columns=["A", "B", "C", "D"])
# Create heatmap without colorbar
sns.heatmap(df, cbar=False, annot=True, fmt='.2f')
plt.title("Heatmap without Colorbar")
plt.show()
Comparison with Colorbar
Let's see the difference between heatmaps with and without colorbars −
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
np.random.seed(42)
data = np.random.random((5, 5))
df = pd.DataFrame(data, columns=["Col1", "Col2", "Col3", "Col4", "Col5"])
# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Heatmap with colorbar
sns.heatmap(df, ax=ax1, cbar=True, annot=True, fmt='.2f')
ax1.set_title("With Colorbar")
# Heatmap without colorbar
sns.heatmap(df, ax=ax2, cbar=False, annot=True, fmt='.2f')
ax2.set_title("Without Colorbar")
plt.tight_layout()
plt.show()
Additional Customization
You can combine cbar=False with other styling options −
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create correlation matrix
np.random.seed(123)
data = {
'Temperature': np.random.normal(25, 5, 100),
'Humidity': np.random.normal(60, 10, 100),
'Pressure': np.random.normal(1013, 20, 100),
'Wind_Speed': np.random.normal(15, 8, 100)
}
df = pd.DataFrame(data)
correlation_matrix = df.corr()
# Create customized heatmap without colorbar
plt.figure(figsize=(8, 6))
sns.heatmap(correlation_matrix,
cbar=False,
annot=True,
fmt='.3f',
square=True,
cmap='coolwarm',
linewidths=0.5)
plt.title("Correlation Matrix (No Colorbar)")
plt.show()
When to Use cbar=False
| Scenario | Use cbar=False | Reason |
|---|---|---|
| Multiple subplots | Yes | Cleaner layout, shared colorbar possible |
| Focus on patterns | Yes | Less visual clutter |
| Limited space | Yes | More room for the heatmap itself |
| Exact values needed | No | Colorbar helps interpret values |
Conclusion
Use cbar=False in sns.heatmap() to hide the colorbar when you want cleaner visualizations or when working with multiple subplots. This parameter is especially useful when focusing on data patterns rather than exact color-value mappings.
Advertisements
