seaborn: Selected KDE bandwidth is 0. Cannot estimate density

What's going on here is that Seaborn (or rather, the library it relies on to calculate the KDE - scipy or statsmodels) isn't managing to figure out the "bandwidth", a scaling parameter used in the calculation. You can pass it manually. I played with a few values and found 1.5 gave a graph at the same scale as your previous:

sns.kdeplot(ser_test, cumulative=True, bw=1.5)

See also here. Worth installing statsmodels if you don't have it.


The problem occurs because of statsmodels.

Anyway, to solve the issue for seaborn version starting from 0.10.0, just place diag_kws={'bw': 1} as arg.

Try to figure out the optimal value for bandwidth.


you have three options to try

first: showing KDE lumps with the default settings

sns.distplot(ser_test, hist = False, rug = True, rug_kws = {'color' : 'r'})

second: KDE with narrow bandwidth to show individual probability lumps

sns.distplot(ser_test, hist = False, rug = True, rug_kws = {'color' : 'r'}, kde_kws = {'bw' : 1})

third: choosing a different, triangular kernel function (lump shape)

sns.distplot(ser_test, hist = False, rug = True, rug_kws = {'color' : 'r'}, kde_kws = {'bw' : 1.5, 'kernel' : 'tri'})


if you don't want to wait for the seaborn git update to get released in a stable version, you can try one of the solutions in the issue page. specifically henrymartin1's suggestion to try manually passing in a small bandwidth inside a try/catch block (suggested by ahartikainen) which grabs the text of this specific error (so other errors still get raised):

try:
    sns.distplot(df)
except RuntimeError as re:
    if str(re).startswith("Selected KDE bandwidth is 0. Cannot estimate density."):
        sns.distplot(df, kde_kws={'bw': 0.1})
    else:
        raise re

This worked for me.