python - Matplotlib histogram-values getting grouped incorrectly? -
my task create histogram of neutron pulse heights. pulse heights separated 150 "categories", bin_names
variable. number of neutrons detected @ each pulse height expressed counts
variable. graph number of neutrons @ each height. here code:
def make_hist(counts, bin_names, start_time, end_time, path): ''' graph title--later start_time = dt.datetime.fromtimestamp(start_time).strftime('%y/%m/%d %h:%m:%s') end_time = dt.datetime.fromtimestamp(end_time).strftime('%y/%m/%d %h:%m:%s') ''' print('counts', counts)
[0, 0, 0, 5, 5, 6, 8, 6, 10, 7, 8, 5, 10, 8, 6, 9, 10, 12, 6, 13, 6, 14, 9, 10, 17, 16, 6, 18, 15, 11, 3, 16, 8, 8, 9, 10, 12, 13, 13, 5, 12, 6, 4, 12, 5, 6, 8, 6, 3, 3, 6, 1, 1, 5, 4, 0, 2, 3, 1, 3, 1, 2, 2, 0, 2, 2, 1, 2, 0, 0, 0, 1, 0, 0, 0, 3, 2, 1, 2, 1, 2, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 2, 0, 1, 2, 3, 1, 1, 2, 2, 2, 0, 2, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
print('bin_names', bin_names)
[0.0, 0.0278, 0.055601, 0.083401, 0.1112, 0.139, 0.1668, 0.1946, 0.2224, 0.2502, 0.278, 0.3058, 0.3336, 0.36141, 0.38921, 0.41701, 0.44481, 0.47261, 0.50041, 0.52821, 0.55601, 0.58381, 0.61161, 0.63941, 0.66721, 0.69501, 0.72281, 0.75061, 0.77841, 0.80621, 0.83401, 0.86181, 0.88961, 0.91741, 0.94521, 0.97301, 1.0008, 1.0286, 1.0564, 1.0842, 1.112, 1.1398, 1.1676, 1.1954, 1.2232, 1.251, 1.2788, 1.3066, 1.3344, 1.3622, 1.39, 1.4178, 1.4456, 1.4734, 1.5012, 1.529, 1.5568, 1.5846, 1.6124, 1.6402, 1.668, 1.6958, 1.7236, 1.7514, 1.7792, 1.807, 1.8348, 1.8626, 1.8904, 1.9182, 1.946, 1.9738, 2.0016, 2.0294, 2.0572, 2.085, 2.1128, 2.1406, 2.1684, 2.1962, 2.224, 2.2518, 2.2796, 2.3074, 2.3352, 2.363, 2.3908, 2.4186, 2.4464, 2.4742, 2.502, 2.5298, 2.5576, 2.5854, 2.6132, 2.641, 2.6688, 2.6966, 2.7244, 2.7522, 2.78, 2.8078, 2.8356, 2.8634, 2.8912, 2.919, 2.9468, 2.9746, 3.0024, 3.0302, 3.058, 3.0858, 3.1136, 3.1414, 3.1692, 3.197, 3.2248, 3.2526, 3.2804, 3.3082, 3.336, 3.3638, 3.3916, 3.4194, 3.4472, 3.475, 3.5029, 3.5307, 3.5585, 3.5863, 3.6141, 3.6419, 3.6697, 3.6975, 3.7253, 3.7531, 3.7809, 3.8087, 3.8365, 3.8643, 3.8921, 3.9199, 3.9477, 3.9755, 4.0033, 4.0311, 4.0589, 4.0867, 4.1145, 4.1423]
ind = np.array(bin_names[:-1]) width = np.array([bin_names[i+1]-bin_names[i] in range(len(bin_names)-1)]) fig, ax = plt.subplots(figsize=(35,10)) ax.hist(counts, bins=150, rwidth=1, range=(0,4.1423)) ax.set_xticks(ind+width/2) ax.set_xticklabels(bin_names, rotation=90) plt.show() fig.savefig(path + 'neutron_hist.png')
i replaced line
ax.hist(counts, bins=150, rwidth=1, range=(0,4.1423))
with
ax.bar(bin_names, counts)
this results in following:
zoomed-in graph
full version
that seemed fix grouping issue having when used histogram function, wasn't sure how set new width of ticks. doing
ax.bar(bin_names, counts, width=width)
raises
traceback (most recent call last): file "<ipython-input-7-589941283d14>", line 1, in <module> runfile('y:/rws_programs/read_neutrons.py', wdir='y:/rws_programs') file "c:\program files\anaconda3\lib\site- packages\spyder\utils\site\sitecustomize.py", line 688, in runfile execfile(filename, namespace) file "c:\program files\anaconda3\lib\site- packages\spyder\utils\site\sitecustomize.py", line 101, in execfile exec(compile(f.read(), filename, 'exec'), namespace) file "y:/rws_programs/read_neutrons.py", line 117, in <module> make_hist(phd_bins, phd_bin_names, now, ten_m_ago, 'y:/rws_programs/') file "y:/rws_programs/read_neutrons.py", line 56, in make_hist ax.bar(bin_names, counts, width=width) file "c:\program files\anaconda3\lib\site-packages\matplotlib\__init__.py", line 1891, in inner return func(ax, *args, **kwargs) file "c:\program files\anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 2082, in bar "must length %d or scalar" % nbars) valueerror: incompatible sizes: argument 'width' must length 150 or scalar
Comments
Post a Comment