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 make custom grid lines in Seaborn heatmap?
To make custom grid lines in Seaborn heatmap, we can use linewidths and linecolor parameters in the heatmap() method. These parameters allow you to control the thickness and color of the lines that separate cells in the heatmap.
Basic Custom Grid Lines
Here's how to create a heatmap with custom grid lines ?
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 custom grid lines
sns.heatmap(df, linewidths=4, linecolor='green')
plt.show()
Different Grid Line Styles
You can experiment with different line widths and colors ?
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, size=(4, 4))
df = pd.DataFrame(data,
columns=['A', 'B', 'C', 'D'],
index=['Row1', 'Row2', 'Row3', 'Row4'])
# Create subplots for comparison
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
# Thin white lines
sns.heatmap(df, linewidths=1, linecolor='white', ax=axes[0])
axes[0].set_title('Thin White Lines')
# Medium blue lines
sns.heatmap(df, linewidths=2.5, linecolor='blue', ax=axes[1])
axes[1].set_title('Medium Blue Lines')
# Thick black lines
sns.heatmap(df, linewidths=5, linecolor='black', ax=axes[2])
axes[2].set_title('Thick Black Lines')
plt.tight_layout()
plt.show()
Parameters
| Parameter | Description | Example Values |
|---|---|---|
linewidths |
Width of lines separating cells | 0.5, 1, 2.5, 4 |
linecolor |
Color of the grid lines | 'white', 'black', 'blue', '#FF5733' |
Advanced Grid Customization
Combine grid lines with other styling options for better visualization ?
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create correlation matrix data
np.random.seed(42)
data = np.random.randn(6, 4)
df = pd.DataFrame(data, columns=['Feature_A', 'Feature_B', 'Feature_C', 'Feature_D'])
corr_matrix = df.corr()
# Create heatmap with custom styling
plt.figure(figsize=(8, 6))
sns.heatmap(corr_matrix,
annot=True, # Show correlation values
linewidths=2, # Grid line width
linecolor='gray', # Grid line color
cmap='coolwarm', # Color scheme
center=0, # Center colormap at 0
square=True) # Square cells
plt.title('Correlation Matrix with Custom Grid Lines')
plt.show()
Conclusion
Use linewidths to control grid line thickness and linecolor to set their color in Seaborn heatmaps. Combine these with other styling parameters like annot and cmap for professional-looking visualizations.
Advertisements
