Home > Blockchain >  How to draw histograms under a curve?
How to draw histograms under a curve?

Time:01-03

Suppose we have a function (like pdf of a normal distribution), and we want to approximate it with histograms under the function. I wanna specify the number of bins and draw histograms under the curve. How is it possible to do in Python? For example, a graph like below, but all spikes are under the curve, and the number of bins is a parameter.

enter image description here

CodePudding user response:

You can use the pdf to decide the heights of the bars:

from scipy.stats import norm
import numpy as np
N = 20
x = np.linspace(norm.ppf([0.001, 0.999]), N)
y = norm.pdf(x)

Each center of a bar will be just as high as the pdf, so the bars will cut the curve. To only touch the curve, one could calculate the pdf at the lowest point, being x width/2 for positive points. As the pdf is symmetric, abs can be used to create a single expression for both positive and negative x-values.

Here is an animation created via the histogram along gaussian pdf

PS: Here is a list of related questions (collected by @TrentonMcKinney), where you can find additional explanation and ideas:

  • Related