The Python Oracle

Add density curve on the histogram

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Realization

--

Chapters
00:00 Question
00:36 Accepted answer (Score 3)
01:57 Answer 2 (Score 3)
02:24 Thank you

--

Full question
https://stackoverflow.com/questions/6446...

Accepted answer links:
[distplot]: https://seaborn.pydata.org/generated/sea...
[image]: https://i.stack.imgur.com/r4fJP.png
[removed in a future version of seaborn]: https://seaborn.pydata.org/generated/sea...
[histplot]: https://seaborn.pydata.org/generated/sea...
[displot]: https://seaborn.pydata.org/generated/sea...
[image]: https://i.stack.imgur.com/yOZrL.png
[image]: https://i.stack.imgur.com/Pnkce.png

Answer 2 links:
[image]: https://i.stack.imgur.com/VikWQ.png

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #numpy #matplotlib #seaborn

#avk47



ACCEPTED ANSWER

Score 5


distplot has been removed: removed in a future version of seaborn. Therefore, alternatives are to use histplot and displot.

sns.histplot

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

df = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))
X = df['A']

sns.histplot(X, kde=True, bins=20)
plt.show()

enter image description here

sns.displot

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

df = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))
X = df['A']

sns.displot(X, kde=True, bins=20)
plt.show()

enter image description here


distplot has been removed

Here is an approach using distplot method of seaborn. Also, mentioned in the comments:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

df = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))
X = df['A']

sns.distplot(X, kde=True, bins=20, hist=True)
plt.show()

enter image description here




ANSWER 2

Score 4


Pandas also has kde plot:

hist, bins = np.histogram(X, bins=10,density=True)
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width, zorder=1)

# density plot
df['A'].plot.kde(zorder=2, color='C1')
plt.show()

Output:

enter image description here