matplotlib直方图中的非均匀轴 [英] Non-uniform axis in matplotlib histogram

查看:73
本文介绍了matplotlib直方图中的非均匀轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Matplotlib 绘制具有非均匀 x 轴的直方图.例如,考虑以下直方图:

 将matplotlib.pyplot导入为plt值= [0.68、0.28、0.31、0.5、0.25、0.5、0.002、0.13、0.002、0.2、0.3、0.45,0.56、0.53、0.001、0.44、0.008、0.26、0.,0.37、0.03、0.002、0.19、0.18,0.04、0.31、0.006、0.6、0.19、0.3、0、0.46、0.2、0.004、0.06、0.]plt.hist(值)plt.show()

第一个 bin 密度很高,所以我想放大那里.

理想情况下,我想将 x 轴中的值更改为 [0, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1] ,保留 bin宽度在图表中保持不变(当然不是数字形式).有没有简单的方法来实现这一目标?欢迎提出任何意见或建议.

解决方案

安德烈(André)的解决方案很好,但是垃圾箱宽度不是恒定的.使用log2 x轴可以满足我的需求.我使用

I would like to plot a histogram with a non-uniform x-axis using Matplotlib. For example, consider the following histogram:

import matplotlib.pyplot as plt
values = [0.68, 0.28, 0.31, 0.5, 0.25, 0.5, 0.002, 0.13, 0.002, 0.2, 0.3, 0.45,
      0.56, 0.53, 0.001, 0.44, 0.008, 0.26, 0., 0.37, 0.03, 0.002, 0.19, 0.18,
      0.04, 0.31, 0.006, 0.6, 0.19, 0.3, 0., 0.46, 0.2, 0.004, 0.06, 0.]
plt.hist(values)
plt.show()

The first bin has high density, so I would like to zoom in there.

Ideally, I would like to change the values in the x-axis to something like [0, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1], keeping the bin widths constant within the graph (but not numerically, of course). Is there an easy way to achieve this? Any comments or suggestions are welcome.

解决方案

The solution from André is nice, but the bin widths are not constant. Working with a log2 x-axis suits what I was looking for. I use np.logspace to make the bin widths constant in the graph.

That's what I ended up doing:

import matplotlib.pyplot as plt
values = [0.68, 0.28, 0.31, 0.5, 0.25, 0.5, 0.002, 0.13, 0.002, 0.2, 0.3, 0.45,
        0.56, 0.53, 0.001, 0.44, 0.008, 0.26, 0., 0.37, 0.03, 0.002, 0.19, 0.18,
        0.04, 0.31, 0.006, 0.6, 0.19, 0.3, 0., 0.46, 0.2, 0.004, 0.06, 0.]
bins = np.logspace(-10, 1, 20, base=2)
bins[0]=0
fig, ax = plt.subplots()
plt.hist(values, bins=bins)
ax.set_xscale('log', basex=2)
ax.set_xlim(2**-10, 1)
plt.show()

这篇关于matplotlib直方图中的非均匀轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆