Search code examples
pythonmatplotlibseabornprobability-distribution

How to draw distribution plot for discrete variables in seaborn


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


Solution

  • If the problem is that there are some emptry bins in the histogram, it probably makes sense to specify the bins to match the data. In this case, use bins=np.arange(0,16) to get the bins for all integers in the data.

    import numpy as np; np.random.seed(1)
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    n = np.random.randint(0,15,10000)
    sns.distplot(n, bins=np.arange(0,16), hist_kws=dict(ec="k"))
    
    plt.show()
    

    enter image description here