How to Add Outline/Edge Color to Histogram in Seaborn?

Add Edge Color to Histogram with Seaborn
Histogram with edge line: Seaborn

In this tutorial, we will see an example of adding edge color/edge line to histograms made with Seaborn.

Let us load the packages needed to make histogram with outlines using Seaborn.

 
import seaborn as sns 
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

We will use Seattle weather data from vega_datasets to make histograms.

 
from vega_datasets import data
seattle_weather = data.seattle_weather()
print(seattle_weather.head(n=3))

Let us first make a histogram with Seaborn’s distplot.

 
sns.distplot(seattle_weather['temp_max'])
plt.title('Seattle Weather Data', fontsize=18)
plt.xlabel('temp_max', fontsize=16)
plt.ylabel('Density', fontsize=16)

By default, distplot() fills the bars in histogram with blue color. However, it does not have any outline to the edges of the bar. And thus makes the histogram bars look continuous.

Basic Histogram without edge color: Seaborn

We can add outline or edge line with colors using hist_kws as argument to distplot() function. We should specify hist_kws as dictionary with properties for it. For example, in our example we specify the edgecolor and linewidth.

 
sns.distplot(seattle_weather['temp_max'],
             hist_kws=dict(edgecolor="black", linewidth=2))
plt.title('Seattle Weather Data', fontsize=18)
plt.xlabel('temp_max', fontsize=16)
plt.ylabel('Density', fontsize=16)
Histogram with edge line: Seaborn
Exit mobile version