【发布时间】:2020-04-10 23:46:44
【问题描述】:
我有两组数据 (x, y) 对应于两个一维直方图,这些直方图旨在作为子图彼此相邻绘制。 x 和 y 值都不同,因此它们将在不同的轴上表示。每个子图的直方图高度(hist 中的第一项)和相应的 bin 序列(hist 中的第二项)如下所示:
*请注意每个高度对应序列中的bin;每个垃圾箱的高度都是已知的。我只想使用 hist 函数将数据以条形格式放置
array_1 = np.array([ 8.20198063, 8.30645018, 8.30829034, 8.63297701, 0., 0., 10.43478942])
array_random_1 = np.array([ 8.23460584, 8.31556503, 8.3090378, 8.63147021, 0., 0., 10.41481862])
array_2 = np.array([10.4348338, 8.69943553, 8.68710347, 6.67854038])
array_random_2 = np.array([10.41597028, 8.76635268, 8.19516216, 6.68126994])
bins_1, bins_2 = [8.0, 8.6, 9.2, 9.8, 10.4, 11.0, 11.6, 12.2], [0.0, 0.25, 0.5, 0.75, 1.0]
这是我尝试使用 python 中的 hist 函数绘制这两个子图:
fig, (ax1, ax2) = plt.subplots(1, 2, sharex=False, sharey=False, figsize=(12,3))
ax1.hist(array_1, bins_1, ec='blue', fc='none', lw=1.5, histtype='step', label='1')
ax1.hist(array_random_1, bins_1, ec='red', fc='none', lw=1.5, histtype='step', label='Random_1')
ax1.set_xlabel('X1')
ax1.set_ylabel('Y1')
ax2.hist(array_2, bins_2, ec='blue', fc='none', lw=1.5, histtype='step', label='2')
ax2.hist(array_random_2, bins_2, ec='red', fc='none', lw=1.5, histtype='step', label='Random_2')
ax2.set_xlabel('X2')
plt.show()
但是,如您所见,左侧面板中的条未绘制到正确的高度(蓝色条完全丢失),并且第二个面板中的所有内容都丢失了。制作这些一维直方图有什么问题?这是否意味着我不能将 hist 用于我的目的?
我想要的是以下可以使用bar。如何使用hist?
【问题讨论】:
标签: arrays python-3.x numpy histogram sequence