Integrate histogram in python?

前端 未结 2 1342
自闭症患者
自闭症患者 2021-01-13 17:48

Is there a simple command in matplotlib that let\'s me take the integral of histogram over a certain range? If I plot a histogram with: fig = plt.hist(x, bins) Then, is

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-13 17:58

    The plt.hist command returns all the data you need to make one. If out = plt.hist(...), the bin heights are in out[0] and the bin widths are diff(out[1]). E.g.,

    sum(out[0][4:7]*diff(out[1][4:8]))
    

    for the integral over bins 4-6 inclusive. diff calculates each bin-width, so it handles bins of different widths, and the multiplication happens element-wise, so calculates the areas of each rectangle in the histogram.

提交回复
热议问题