Reputation: 480
Is there any way to get the values in "x" and "y" of a histogram without graphing** it? I use the function below many times (in each loop) in my code, and I noticed that my code gets slower and slower in each loop.
** I am not sure if what it does internally is to graph but I know that the slowness in my code is related to the function "plt.hist" despite using plt.close(). Thank you.
# a is a list
def function_hist(a, ini, final):
# 12 bins
bins = np.linspace(ini, final, 13)
weightsa = np.ones_like(a)/float(len(a))
y, x, _ = plt.hist(a, bins, weights = weightsa)
plt.close()
Upvotes: 5
Views: 15076
Reputation: 16470
Use numpy.histogram
You can modify your function as below
# a is a list
def function_hist(a, ini, final):
# 12 bins
bins = np.linspace(ini, final, 13)
weightsa = np.ones_like(a)/float(len(a))
return np.histogram(np.array(a), bins, weights = weightsa)
Upvotes: 11