英文:
Fix range of matplotlib histogram with multiple datasets?
问题
抱歉,我只翻译代码部分,以下是代码的翻译:
我有多个数据集,我想将它们合并成直方图。不幸的是,我的数据显示不同的范围。
使用简单的`range=(0,1)`参数会触发奇怪的行为。
这是它应该看起来的样子:
[![在此输入图像描述](https://i.stack.imgur.com/xjk4t.png)](https://i.stack.imgur.com/xjk4t.png)
当数据范围为0-1时,它目前看起来确实是这样的。
但有时它的范围不同,我得到了:[![在此输入图像描述](https://i.stack.imgur.com/rUOSN.png)](https://i.stack.imgur.com/rUOSN.png)
在这里,我想强制范围为0-1。
但当我使用以下代码时:
`axs.hist(plot_data, range=(0,1),color=colors,label=plot_label, histtype='bar')`
我得到了[![在此输入图像描述](https://i.stack.imgur.com/FwutW.png)](https://i.stack.imgur.com/FwutW.png)
我无法解释为什么会发生这种情况。
这是代码:
```python
fig, axs = plt.subplots(1,1,tight_layout=True)
fig.set_size_inches(7, 4)
# 为每个直方图定义颜色
colors = ['g', 'b', 'r', 'purple']
# 为所有错误条件制作组合直方图
# 绘制直方图
axs.hist(plot_data,color=colors,label=plot_label, histtype='bar')
# 在当前轴的右侧放置图例
axs.legend(loc='center left',bbox_to_anchor=(1, 0.5))
# 设置坐标轴标签
axs.set_xlabel(f"相似性:{wildcards.metric}")
axs.set_ylabel("频率")
# 让x轴从左到右递增
axs.invert_xaxis()
# 确保正确的布局
fig.tight_layout()
# 保存直方图
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
组合。
<details>
<summary>英文:</summary>
I have multiple datasets of that I'd like to combine into a histogram. Unfortunately, my data shows different ranges.
Using the simple `range=(0,1)` argument triggers weird behaviour.
This is what it should look like:
[![enter image description here](https://i.stack.imgur.com/xjk4t.png)](https://i.stack.imgur.com/xjk4t.png)
and it currently does when the data has the range 0-1.
But sometimes it has not and I get: [![enter image description here](https://i.stack.imgur.com/rUOSN.png)](https://i.stack.imgur.com/rUOSN.png)
Here I would like to force the 0-1 range.
But when I do with
`axs.hist(plot_data, range=(0,1),color=colors,label=plot_label, histtype='bar')`
I get [![enter image description here](https://i.stack.imgur.com/FwutW.png)](https://i.stack.imgur.com/FwutW.png)
I cannot explain why that happens.
This is the code:
```python
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))
and some example data is:
[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')]
I have tried every possible range
and bin
combination.
答案1
得分: 1
你的数组中充满了字符串值,但你正在传递一个期望浮点数/整数值的范围参数。我不确定为什么你的值是字符串,但使用浮点值(而不是使用 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)
# 为每个直方图定义颜色
colors = ['g', 'b', 'r', 'purple']
plot_label = ['IDEAL', 'TYPICAL', 'LARGE', 'EXTREME']
# 创建所有错误条件的组合直方图
# 绘制直方图
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)
# 在当前轴的右侧放置图例
axs.legend(loc='center left', bbox_to_anchor=(1, 0.5))
# 设置坐标轴标签
axs.set_xlabel(f"相似度:")
axs.set_ylabel("频率")
# 使x轴从左到右递增
axs.invert_xaxis()
# 确保正确的布局
fig.tight_layout()
plt.xlim(0, 1)
plt.show()
英文:
Your arrays are full of string values but you're passing a range parameter that is expecting float/int values. I'm not sure why your values are strings, but using float values (and not using dtype = '<U46'
) leads to the graph behaving as expected with the range parameter:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论