1 year ago

#383043

test-img

Roosha

Why the output of ax.hist and physt.hist are not identical?

I have an array and I would like to place it into 7 bins and then calculate the mean and standard deviation (standard in the error) corresponding to each bin so that I can plot both the histogram as well as the errorbars. While the numpy histogram readily outputs the mean values of bins, it is not meant to produce the errorbars (unless I am wrong). This is why I want to use the physt python package to directly extract the mean and errors corresponding to each bin for the purpose of errorbars. But, I just noticed that the two methodology are not agreeing with each other in the first place; they don't even produce the same mean values (heights) as expected. Now, I am kind of confused. I would truly appreciate your help.

import numpy as np
from physt import h1
import matplotlib.pyplot as plt

x_arr = np.array([
    0, 32, 28, 15, 19, 22, 18, 16, 13, 35, 21, 32, 23, 11, 17, 3, 17, 3, 21, 43, 32, 15, 16, 18,
    28, 9, 33, 16, 20, 19, 35, 37, 32, 26, 30, 30, 28, 30, 22, 25, 21, 26, 41, 41, 12, 3, 5, 6, 5,
    17, 16, 16, 16, 7, 2, 15, 16, 15, 15, 15, 7, 5
])

bins = np.array([0, 2, 3, 5, 9, 17, 33, 65])

ax = plt.axes()
heights, bins, patches = ax.hist(x_arr, bins, density=True)
print('numpy: \n', heights)

hist = h1(x_arr, bins, density=True)
print('physt: \n', hist.frequencies / sum(hist.frequencies))

And here are the outputs which are interestingly different:

numpy:
 [0.00806452 0.01612903 0.02419355 0.02419355 0.03427419 0.02721774
 0.00352823]
physt:
 [0.01612903 0.01612903 0.0483871  0.09677419 0.27419355 0.43548387
 0.11290323]

python-3.x

numpy

matplotlib

histogram

binning

0 Answers

Your Answer

Accepted video resources