How to draw distribution plot for discrete variables in seaborn

Zealseeker picture Zealseeker · Feb 26, 2018 · Viewed 8.3k times · Source

When I draw displot for discrete variables, the distribution might not be as what I think. For example.

enter image description here We can find that there are crevices in the barplot so that the curve in kdeplot is "lower" in y axis.

In my work, it was even worse: enter image description here

I think it may because the "width" or "weight" was not 1 for each bar. But I didn't find any parameter that can justify it.

I'd like to draw such curve (It should be more smooth) enter image description here

Answer

Diziet Asahi picture Diziet Asahi · Feb 26, 2018

One way to deal with this problem might be to adjust the "bandwidth" of the KDE (see the documentation for seaborn.kdeplot())

n = np.round(np.random.normal(5,2,size=(10000,)))
sns.distplot(n, kde_kws={'bw':1})

enter image description here

EDIT Here is an alternative with a different scale for the bars and the KDE

n = np.round(np.random.normal(5,2,size=(10000,)))
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

sns.distplot(n, kde=False, ax=ax1)
sns.distplot(n, hist=False, ax=ax2, kde_kws={'bw':1})

enter image description here