Why do seaborn countplots and histplots display the same hexadecimal color differently?

Viewed 701

I'm trying to keep a singular color palette in my thesis, and I noticed that the blue of my histplots and the blue of my countplots are slightly different shades, even though I set them to the exact same hexadecimal value.

Is there a setting that I'm missing or do these different plots not just show the hexadecimal as given? I've tried playing around with the countplot saturation but it doesn't match the color. Ideally all of my histplots would have the same color as my countplots (and bar plots which use the countplot coloring too).

Below a minimum code example:

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(rc={'figure.figsize':(20,10)}, font_scale=2)
plt.rcParams['axes.grid'] = False
titanic = sns.load_dataset('titanic')
fig, ax = plt.subplots(1,2)
sns.countplot(x="class", data=titanic, ax=ax[0], color='#5975a4')
sns.histplot(x="who", data=titanic, ax=ax[1], color='#5975a4')

It produces the following figure:

Different Blues

1 Answers

The countplot has a saturation parameter (more saturation is more "real" color, less saturation is closer to grey). Seaborn uses saturation in bar plots to make the default colors look "smoother". The default saturation is 0.75; it can be set to 1 to get the "true" color.

The histplot has an alpha parameter, making the color semi-transparent. The color gets mixed with the background, so it looks different depending on the background color. In this case, the alpha seems to default to 0.75. As that also has an effect similar to saturation, the histplot doesn't use saturation. The transparency is especially useful when multiple histograms are drawn in the same subplot.

To get both in "real" color, set both the saturation of the countplot and the alpha of the histplot to 1:

import seaborn as sns
import matplotlib.pyplot as plt

sns.set(rc={'figure.figsize': (20, 10)}, font_scale=2)
plt.rcParams['axes.grid'] = False
titanic = sns.load_dataset('titanic')
fig, ax = plt.subplots(1, 2)
sns.countplot(x="class", data=titanic, ax=ax[0], color='#5975a4', saturation=1)
sns.histplot(x="who", data=titanic, ax=ax[1], color='#5975a4', alpha=1)
plt.show()

sns.histplot and sns.countplot with "full" color

PS: By default, a countplot uses only 80% of the width, while a histogram uses the full width. If desired, the histogram bars can be shrunk, e.g. sns.histplot(..., shrink=0.8), to get the same width as the countplot.

Related