0

I am plotting a histogram with Seaborn (cf. seaborn histplot doc) and the corresponding theoretical gaussian curve using the mean value of my sequence and its standard deviation using Scipy (cf. scipy normal distribution).

import seaborn as sbn, scipy, matplotlib.pyplot as plt

# loads signal from somewhere else, sequence of floats
# replace by scipy.stats.norm.pdf(list(range(len(100)), 1, 6) for example
# although real values are a bit shifted from that
y = get_signal() 
x = list(range(len(y))

# plot histogram with seaborn
fig = plt.figure()
sbn.histplot(y, kde=True, stat="density", label="measured")

# plot normal distribution based on mean and standard deviation values
ax = fig.axes[0]
xmin, xmax = ax.get_xlim()
mean, std = y.mean(), y.std()
x2 = np.linspace(xmin, xmax, 100)
y2 = scipy.stats.norm.pdf(x, mean, std)
plt.plot(x2, y2, color="orange", label="theoretical")

Now, I want to get the fwhm value of both curves so I am thinking of using Scipy's peak_widths method to retrieve the corresponding information. However, I am unable to do so for the histogram, because I don't have a sequence of values such as [(bin0_x, bin0_y), (bin1_x, bin1_y), ...] or [bin0_y, bin1_y, ...].

So I should have two options here : either I compute the histogram distribution with numpy or I find a way to retrieve the distribution from seaborn's histplot. I am interested in the second option because seaborn can determine a default number of bins that I find alright.

I took a look at this post : How to retrieve error bar in seaborn

It says we can retrieve the different lines in the plot through the Axes object. But I am not too sure how to go about this with a histogram type of plot. Also, I would like to know if there was a simpler answer that doesn't need to go through all the axis content.

For example, I am drawing the kde of the histplot with seaborn, but maybe there is a way to draw it with another module and use the resulting sequence of values to compute the fwhd value I want ? This should give better results than basing fwhm calculation on the histogram distribution directly. I'd like that but I am unable to get the kde values from seaborn, as much as I am unable to retrieve the histogram values. Maybe scipy's gaussian_kde() method would do the trick, what do you think ? Do I need to use it ?

SpaceBurger
  • 537
  • 2
  • 12
  • [This comment](https://stackoverflow.com/questions/37374983/get-data-points-from-seaborn-distplot#comment106906723_37375147) on the duplicate will get you the x, width and height of the bars – DavidG Feb 22 '21 at 14:38
  • Thank you, this is exactly what I was looking for ! Sorry for the duplicate too. – SpaceBurger Feb 22 '21 at 15:25

0 Answers0