How to Change the Color of a Seaborn Histogram


You can use the color and edgecolor arguments in seaborn to change the fill color and outline color, respectively, of bars in a histogram:

sns.histplot(data=df, x='some_variable', color='orange', edgecolor='red')

The following example shows how to use these arguments in practice.

Example: Change Colors of Seaborn Histogram

Suppose we have the following pandas DataFrame that contains information about the points scored by 200 different basketball players:

import pandas as pd
import numpy as np

#make this example reproducible
np.random.seed(1)

#create DataFrame
df = pd.DataFrame({'team': np.repeat(['A', 'B'], 100),
                   'points': np.random.normal(size=200, loc=15, scale=4)})

#view head of DataFrame
print(df.head())

  team     points
0    A  21.497381
1    A  12.552974
2    A  12.887313
3    A  10.708126
4    A  18.461631

We can use the following code to create a histogram in seaborn to visualize the distribution of values in the points column:

import seaborn as sns

#create histogram to visualize distribution of points
sns.histplot(data=df, x='points')

By default, seaborn uses blue as the fill color and black as the outline color for the bars in the histogram.

However, we can customize these colors by using the color and edgecolor arguments:

import seaborn as sns

#create histogram to visualize distribution of points
sns.histplot(data=df, x='points', color='orange', edgecolor='red')

seaborn histogram custom colors

Notice that the histogram now has a fill color of orange and and outline color of red.

Also note that you can use hex color codes for even more customization:

import seaborn as sns

#create histogram to visualize distribution of points
sns.histplot(data=df, x='points', color='#DAF7A6', edgecolor='#BB8FCE')

Note: You can find the complete documentation for the seaborn histplot() function here.

Additional Resources

The following tutorials explain how to perform other common functions in seaborn:

How to Set the Color of Bars in a Seaborn Barplot
How to Create a Grouped Barplot in Seaborn
How to Create Multiple Seaborn Plots in One Figure

Leave a Reply

Your email address will not be published. Required fields are marked *