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
How to Add Outline or Edge Color to Histogram in Seaborn?
While Seaborn makes it easy to create histograms with a variety of styles and options, by default, histograms do not have an outline or edge color. Adding an outline or edge color can help make the plot more visually appealing and easier to interpret. In this article, we will explore how to add an outline or edge color to a histogram in Seaborn using histplot() and the deprecated distplot() method.
What is a Histogram?
Histograms are graphical tools that show how a set of continuous data is distributed. Seaborn helps us plot both the histogram bars and a density curve. The histplot() function in Seaborn is the modern way to create histograms, while distplot() is deprecated but still functional.
Using histplot() with Edge Color
The recommended approach is to use sns.histplot() with the edgecolor parameter ?
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# Generate sample data
data = np.random.normal(100, 15, 1000)
# Create histogram with edge color
sns.histplot(data, bins=30, edgecolor='black', linewidth=1.2, color='skyblue')
plt.title('Histogram with Edge Color')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()
Using distplot() with Edge Color (Deprecated)
While distplot() is deprecated, you can still add edge colors using the hist_kws parameter ?
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# Generate sample data
data = np.random.normal(100, 15, 1000)
# Create distplot with edge color
sns.distplot(data, hist_kws={'edgecolor': 'purple', 'linewidth': 2})
plt.title('Distplot with Edge Color')
plt.xlabel('Value')
plt.ylabel('Density')
plt.show()
Customization Options
You can customize the edge appearance with various parameters ?
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# Generate sample data
data = np.random.normal(50, 10, 500)
# Create histogram with custom edge styling
sns.histplot(data, bins=25,
edgecolor='red',
linewidth=1.5,
color='lightgreen',
alpha=0.7)
plt.title('Customized Histogram Edges')
plt.xlabel('Value')
plt.ylabel('Count')
plt.show()
Real-World Example with Dataset
Using the built-in tips dataset to demonstrate edge colors ?
import seaborn as sns
import matplotlib.pyplot as plt
# Load tips dataset
tips = sns.load_data('tips')
# Create histogram with edge color
sns.histplot(tips['total_bill'], bins=20,
edgecolor='darkblue',
linewidth=1,
color='lightcoral')
plt.title('Restaurant Bills Distribution')
plt.xlabel('Total Bill ($)')
plt.ylabel('Frequency')
plt.show()
Key Parameters
| Parameter | Function | Description |
|---|---|---|
edgecolor |
histplot() | Sets the edge/outline color |
linewidth |
histplot() | Controls edge thickness |
hist_kws |
distplot() | Dictionary of histogram styling options |
alpha |
Both | Controls transparency (0-1) |
Conclusion
Use sns.histplot() with edgecolor and linewidth parameters for modern histogram edge styling. While distplot() works with hist_kws, it's deprecated in favor of histplot().
