我的任务是创建一个中子脉冲高度的直方图。脉冲高度分为150个“类别”,即bin_names
变量。在每个脉冲高度处检测到的中子数由counts
变量表示。我想绘制每个高度的中子数。这是我的代码:
def make_hist(counts, bin_names, start_time, end_time, path):
'''
FOR 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,5,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,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]
print('bin_names', bin_names)
[0.0,0.0278,0.055601,0.083401,0.1112,0.139,0.1668,0.1496,0.2224,0.2502,0.278,0.3058,0.3336,0.36141,0.38921,0.41701,0.44481,0.47261,0.54141,0.52821,0.55601,0.58381,0.61161, [自然教授。 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.7772,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.1692,2.224,2.2518,2.2796,2.3074,2.3332,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.363,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] for 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')
我替换了
行ax.hist(counts, bins=150, rwidth=1, range=(0,4.1423))
与
ax.bar(bin_names, counts)
这导致以下结果:
放大图
完整版
这似乎解决了我在使用直方图函数时遇到的分组问题,但我不确定如何设置刻度线的新宽度。做
ax.bar(bin_names, counts, width=width)
加注
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 be length %d or scalar" % nbars)
ValueError: incompatible sizes: argument 'width' must be length 150 or scalar