Search code examples
pythonpython-3.xmatplotlibseaborn

have some indication for overlapping bars in seaborn histplot


In histograms plotted with seaborn, when the bars overlap becuase of using hue, the colors change, often indistinguishable. That makes it hard to explain the plots to people. this gets increasingly difficult when there are 10 classes drawn with gradient and understanding which color is on which gets harder. So, how do I show on plots or in legend that the resulting colors are because of the overalap of multiple bars.

# imports
from sklearn.datasets import load_iris
from matplotlib import pyplot as plt
%matplotlib inline
import seaborn as sns

# getting the data
iris = load_iris(as_frame=True)['frame']

# make the histogram
sns.set(rc={'figure.figsize':(20, 5)})
sns.histplot(data=iris, x='sepal length (cm)', hue='target')
plt.show()

that displays this with overlapping in highlited area: enter image description here

I would like to stick to using histograms.

How do I do this?


Solution

  • You can remove the transparency using the alpha=1 parameter:

    sns.histplot(data=iris, x='sepal length (cm)', hue='target', alpha=1)
    

    Note, however, that if bars from series displayed in the back might become invisible if the bars of the series on top of it are higher.

    histplot

    An alternative then would be to have one graph per group using displot:

    sns.displot(data=iris, x='sepal length (cm)', row='target', alpha=1, kind='hist')
    

    displot

    NB. I used iris = sns.load_dataset('iris') to load the dataset, the the different names in the graphs.

    iris = sns.load_dataset('iris')
    sns.histplot(data=iris, x='sepal_length', hue='species', alpha=1)
    sns.displot(data=iris, x='sepal_length', row='species', alpha=1, kind='hist')