我有多个数据集,我想将它们组合成直方图。不幸的是,我的数据显示不同的范围。
使用简单的
range=(0,1)
参数会触发奇怪的行为。
它应该是这样的:
当前数据范围为 0-1 时会出现这种情况。
这里我想强制0-1范围。
但是当我这样做时
axs.hist(plot_data, range=(0,1),color=colors,label=plot_label, histtype='bar')
我无法解释为什么会发生这种情况。
这是代码:
fig, axs = plt.subplots(1,1,tight_layout=True)
fig.set_size_inches(7, 4)
# Define colors for each histogram
colors = ['g', 'b', 'r', 'purple']
# make combined histogram for all error conditions
# plot the histogram
axs.hist(plot_data,color=colors,label=plot_label, histtype='bar')
# Put a legend to the right of the current axis
axs.legend(loc='center left',bbox_to_anchor=(1, 0.5))
# set the axis labels
axs.set_xlabel(f"Similarity: {wildcards.metric}")
axs.set_ylabel("Frequency")
# have the x-axis go increasing from left to right
axs.invert_xaxis()
# ensure proper layout
fig.tight_layout()
# save the histogram
fig.savefig(Path(output.hist))
一些示例数据是:
[array([
'1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0','1.0',
'1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0',
'1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0',
'1.0', '1.0', '1.0'], dtype='<U46'),
array(['
1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0',
'1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0',
'1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0',
'1.0', '1.0', '1.0'], dtype='<U46'),
array([
'0.25', '0.3', '0.3', '0.3', '0.3', '0.3', '0.0', '0.0', '0.3',
'0.0', '0.3', '0.3', '0.0', '0.0', '0.3', '0.5', '0.4', '0.0',
'0.5', '0.5', '0.5', '0.0', '0.4', '0.0', '0.0', '0.6', '0.3',
'0.1', '0.0', '0.0'], dtype='<U46'),
array([
'0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0','0.0','0.0',
'0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0',
'0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0',
'0.0', '0.0', '0.0'], dtype='<U46')]
我已经尝试了所有可能的
range
和bin
组合。
您的数组充满了字符串值,但您传递的范围参数需要 float/int 值。我不确定为什么你的值是字符串,但是使用浮点值(而不是使用
dtype = '<U46'
)会导致图表的行为与范围参数的预期一致:
plot_data = [np.array([
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0]),
np.array([
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0]),
np.array([
0.25, 0.3, 0.3, 0.3, 0.3, 0.3, 0.0, 0.0, 0.3,
0.0, 0.3, 0.3, 0.0, 0.0, 0.3, 0.5, 0.4, 0.0,
0.5, 0.5, 0.5, 0.0, 0.4, 0.0, 0.0, 0.6, 0.3,
0.1, 0.0, 0.0]),
np.array([
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0,0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0])]
fig, axs = plt.subplots(1,1,tight_layout=True)
fig.set_size_inches(7, 4)
# Define colors for each histogram
colors = ['g', 'b', 'r', 'purple']
plot_label = ['IDEAL', 'TYPICAL', 'LARGE', 'EXTREME']
# make combined histogram for all error conditions
# plot the histogram
axs.hist(plot_data, range=(0,1),color=colors,label=plot_label, histtype='bar')
#axs.hist(plot_data,histtype='bar', color=colors, label=plot_label)
# Put a legend to the right of the current axis
axs.legend(loc='center left',bbox_to_anchor=(1, 0.5))
# set the axis labels
axs.set_xlabel(f"Similarity:")
axs.set_ylabel("Frequency")
# have the x-axis go increasing from left to right
axs.invert_xaxis()
# ensure proper layout
fig.tight_layout()
plt.xlim(0, 1)
plt.show()