显示具有非常不均匀的箱宽度的直方图

时间:2015-11-03 11:06:05

标签: python matplotlib histogram

这是直方图 enter image description here

为了生成这个情节,我做了:

bins = np.array([0.03, 0.3, 2, 100])
plt.hist(m, bins = bins, weights=np.zeros_like(m) + 1. / m.size)

然而,正如您所注意到的,我想绘制每个数据点的相对频率的直方图,只有3个不同大小的分档:

bin1 = 0.03 - > 0.3

bin2 = 0.3 - > 2

bin3 = 2 - > 100

直方图看起来很丑,因为最后一个bin的大小相对于其他bin非常大。如何修复直方图?我想改变箱子的宽度,但我不想改变每个箱子的范围。

2 个答案:

答案 0 :(得分:6)

正如@cel指出的那样,这不再是直方图,但您可以使用vfszip:/C:/java/jboss/jboss-5.1.0.GA/server/myapp/deploy/myapp.ear/mymodule.jar/../mypackage/plt.bar执行您要求的操作。然后,您只需将np.histogram设置为描述bin边缘的字符串。例如:

xticklabels

enter image description here

修改

如果你更新到import numpy as np import matplotlib.pyplot as plt bins = [0.03,0.3,2,100] # your bins data = [0.04,0.07,0.1,0.2,0.2,0.8,1,1.5,4,5,7,8,43,45,54,56,99] # random data hist, bin_edges = np.histogram(data,bins) # make the histogram fig,ax = plt.subplots() # Plot the histogram heights against integers on the x axis ax.bar(range(len(hist)),hist,width=1) # Set the ticks to the middle of the bars ax.set_xticks([0.5+i for i,j in enumerate(hist)]) # Set the xticklabels to a string that tells us what the bin edges were ax.set_xticklabels(['{} - {}'.format(bins[i],bins[i+1]) for i,j in enumerate(hist)]) plt.show() ,你会发现matplotlib v1.5.0现在需要一个kwarg bar,这可以使这个绘图更容易(see here):

tick_label

答案 1 :(得分:2)

如果您的箱的实际值不重要但您想要具有完全不同数量级的值的直方图,则可以使用沿x轴的对数缩放。这里为您提供宽度相等的条形

import numpy as np
import matplotlib.pyplot as plt

data = [0.04,0.07,0.1,0.2,0.2,0.8,1,1.5,4,5,7,8,43,45,54,56,99]

plt.hist(data,bins=10**np.linspace(-2,2,5)) 
plt.xscale('log')

plt.show()

当您必须使用bin值时,您可以

import numpy as np
import matplotlib.pyplot as plt

data = [0.04,0.07,0.1,0.2,0.2,0.8,1,1.5,4,5,7,8,43,45,54,56,99]
bins = [0.03,0.3,2,100] 

plt.hist(data,bins=bins) 
plt.xscale('log')

plt.show()

然而,在这种情况下,宽度不完全相等但仍然可读。如果宽度必须相等,你必须使用你的垃圾箱,我推荐@ tom的解决方案。