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 create a seaborn.heatmap() with frames around the tiles?
To create frames around the tiles in a Seaborn heatmap, we can use the linewidths and linecolor parameters in the heatmap() method. This adds visual separation between cells, making the data more readable.
Basic Syntax
sns.heatmap(data, linewidths=width, linecolor='color')
Parameters
- linewidths − Width of the lines that will divide each cell (float)
- linecolor − Color of the lines that will divide each cell (string or RGB)
Example with Green Frames
Here's how to create a heatmap with green frames around each tile −
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data
df = pd.DataFrame(np.random.random((5, 5)),
columns=["col1", "col2", "col3", "col4", "col5"])
# Create heatmap with green frames
sns.heatmap(df, linewidths=4, linecolor='green')
plt.show()
Different Frame Styles
You can customize the appearance with different colors and widths −
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
data = np.random.randint(1, 100, (4, 4))
df = pd.DataFrame(data, columns=['A', 'B', 'C', 'D'])
# Create subplots for comparison
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
# Thin white lines
sns.heatmap(df, linewidths=0.5, linecolor='white', ax=axes[0])
axes[0].set_title('Thin White Lines')
# Medium black lines
sns.heatmap(df, linewidths=2, linecolor='black', ax=axes[1])
axes[1].set_title('Medium Black Lines')
# Thick red lines
sns.heatmap(df, linewidths=3, linecolor='red', ax=axes[2])
axes[2].set_title('Thick Red Lines')
plt.tight_layout()
plt.show()
Comparison Table
| Line Width | Effect | Best Use |
|---|---|---|
| 0.5-1 | Subtle separation | Clean, minimal look |
| 2-3 | Clear distinction | Standard presentations |
| 4+ | Bold separation | High contrast needs |
Conclusion
Use linewidths and linecolor parameters to add frames around heatmap tiles. Choose appropriate width values based on your visualization needs − thin lines for subtle separation, thicker lines for bold emphasis.
Advertisements
