Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

When I try to plot my data in the form of histogram using seaborn displot:

plot = sns.displot(
    data=z, kde=True, kind="hist", bins=3000, legend=True, aspect=1.8
).set(title='Error Distribution')

The curve for KDE is plotted in the form of straight lines instead of curves like here: Is there a way to make the KDE lines cover all the bins of the histogram in a curved manner?

The sample image doesn't seem to correspond to the sample code. With bins=3000, extremely thin bins would be shown. Did you zoom in rather hard? Also note that sns.displot(....) returns a FacetGrid. – JohanC Mar 7, 2022 at 13:20 @JohanC Yes, there were many bins with count close to 0, so I zoomed in near the centre. I guess that's why the reason for this KDE. – Raghav Arora Mar 7, 2022 at 13:40

Instead of zooming in, you could use the bins to restrict to a certain range (via binrange=...). To limit the range of the kde, you can use the clip keyword. Here is an example, first without setting the range:

from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
# first, create some test data
slatm = np.random.normal(-.9, .4, size=(10000, 10)).max(axis=1)
split = np.random.normal(-.1, .1, size=(10000, 10)).max(axis=1)
split[0] = 200  # ad an extreme far value to the dataset
z = pd.DataFrame({'slatm': slatm, 'split': split})
g = sns.displot(data=z, kde=True, kind="hist", bins=3000, legend=True, aspect=1.8)
g.set(title='Error Distribution')
g.ax.set_xlim(-1, 0.5) # zoom in via the x limits

Here is how it would look with limiting the ranges for the histogram and the kde:

min_x, max_x = -1, 0.5
g = sns.displot(data=z, kde=True, kind="hist", bins=30, binrange=(min_x, max_x), legend=True, aspect=1.8,
                kde_kws={'clip': (min_x, max_x)})
g.set(title='Error Distribution')
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.