【发布时间】:2021-11-01 20:33:34
【问题描述】:
我不太擅长编程,我正在为实验室做数据分析。我正在尝试在 Python 上生成一个带有一组 bin 的直方图,每个 bin 具有相同的 bin 宽度,理论上应该捕获所有数据。但往往有大约 10 个数据点未包含在直方图中。有没有办法将这些数据点推到最外面的 bin 中,具体取决于它们是小于还是大于直方图的整体宽度?
如果这个问题太基本或在其他地方得到了回答,我深表歉意,但我可能缺乏自己找到答案的词汇。
这是有问题的代码。我认为它使用了我的大学制作的特殊工具包,但我认为它仍然可以理解:
#sort out N by histogram
binwidth = 2.5*sig_Q
low_center = min(Q_exp) + binwidth
def set_range(first_bin = low_center, bin_width = binwidth, Nbins = 10):
"""
helper function to set the range and bin width
input : first_bin = bin_center of the first bin, bin_width = the bin width, Nbins = total number of bins
returns: a tuple that you can use in the range key word when defining a histogram.
NOTE: for the histogram use the same number of bins:
example: h = histo( r, range = set_range(-5., 1, 11), bins = 11)
this created a histogram where the first bin is centered at -5. , the next at -4. etc. a total of 11 bins are
created and the bin center of the last one is at 5. = first_bin + (Nbins-1)*bin_width
"""
rmin = first_bin - bin_width/2.
rmax = rmin + Nbins*bin_width
return (rmin,rmax)
h = B.histo(Q_exp, range = set_range(low_center, bin_width = binwidth, Nbins = 10), bins = 10)
hx = h.bin_center
hy = h.bin_content
B.pl.ylabel("Counts", fontsize = 20)
B.pl.xlabel("Gaussian Deviates", fontsize = 20)
B.pl.title("Monte Carlo Millikan Oil-Drop Simulation", fontsize = 22)
h.plot()
B.pl.show()
最好的,
【问题讨论】:
标签: python histogram data-analysis