修复具有多个数据集的Matplotlib直方图的范围?

huangapple go评论91阅读模式
英文:

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')]

我已尝试了所有可能的rangebin组合。


<details>
<summary>英文:</summary>
I have multiple datasets of that I&#39;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=&#39;bar&#39;)`
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 = [&#39;g&#39;, &#39;b&#39;, &#39;r&#39;, &#39;purple&#39;]
# make combined histogram for all error conditions
# plot the histogram
axs.hist(plot_data,color=colors,label=plot_label, histtype=&#39;bar&#39;)
# Put a legend to the right of the current axis
axs.legend(loc=&#39;center left&#39;,bbox_to_anchor=(1, 0.5))
# set the axis labels
axs.set_xlabel(f&quot;Similarity: {wildcards.metric}&quot;)
axs.set_ylabel(&quot;Frequency&quot;)
# 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([
&#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;,&#39;1.0&#39;,
&#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;,
&#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;,
&#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;], dtype=&#39;&lt;U46&#39;),
array([&#39;
1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;,
&#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;,
&#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;,
&#39;1.0&#39;, &#39;1.0&#39;, &#39;1.0&#39;], dtype=&#39;&lt;U46&#39;),
array([
&#39;0.25&#39;, &#39;0.3&#39;, &#39;0.3&#39;, &#39;0.3&#39;, &#39;0.3&#39;, &#39;0.3&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.3&#39;,
&#39;0.0&#39;, &#39;0.3&#39;, &#39;0.3&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.3&#39;, &#39;0.5&#39;, &#39;0.4&#39;, &#39;0.0&#39;,
&#39;0.5&#39;, &#39;0.5&#39;, &#39;0.5&#39;, &#39;0.0&#39;, &#39;0.4&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.6&#39;, &#39;0.3&#39;,
&#39;0.1&#39;, &#39;0.0&#39;, &#39;0.0&#39;], dtype=&#39;&lt;U46&#39;),
array([
&#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;,&#39;0.0&#39;,&#39;0.0&#39;,
&#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;,
&#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;,
&#39;0.0&#39;, &#39;0.0&#39;, &#39;0.0&#39;], dtype=&#39;&lt;U46&#39;)]

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()

修复具有多个数据集的Matplotlib直方图的范围?

英文:

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 = &#39;&lt;U46&#39;) 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 = [&#39;g&#39;, &#39;b&#39;, &#39;r&#39;, &#39;purple&#39;]
plot_label = [&#39;IDEAL&#39;, &#39;TYPICAL&#39;, &#39;LARGE&#39;, &#39;EXTREME&#39;]
# make combined histogram for all error conditions
# plot the histogram
axs.hist(plot_data, range=(0,1),color=colors,label=plot_label, histtype=&#39;bar&#39;)
#axs.hist(plot_data,histtype=&#39;bar&#39;, color=colors, label=plot_label)
# Put a legend to the right of the current axis
axs.legend(loc=&#39;center left&#39;,bbox_to_anchor=(1, 0.5))
# set the axis labels
axs.set_xlabel(f&quot;Similarity:&quot;)
axs.set_ylabel(&quot;Frequency&quot;)
# 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()

修复具有多个数据集的Matplotlib直方图的范围?

huangapple
  • 本文由 发表于 2023年8月5日 01:19:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定