显示具有非常不均匀的 bin 宽度的直方图
display a histogram with very non-uniform bin widths
这是直方图
为了生成这个图,我做了:
bins = np.array([0.03, 0.3, 2, 100])
plt.hist(m, bins = bins, weights=np.zeros_like(m) + 1. / m.size)
但是,正如您所注意到的,我想绘制每个数据点的相对频率的直方图,其中只有 3 个具有不同大小的 bin:
bin1 = 0.03 -> 0.3
bin2 = 0.3 -> 2
bin3 = 2 -> 100
直方图看起来很难看,因为最后一个 bin 的大小相对于其他 bin 非常大。如何修复直方图?我想更改垃圾箱的宽度,但我不想更改每个垃圾箱的范围。
正如@cel 指出的那样,这不再是直方图,但您可以使用 plt.bar
和 np.histogram
来完成您的要求。然后,您只需将 xticklabels
设置为描述 bin 边缘的字符串。例如:
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
,你会发现 bar
现在需要一个 kwarg tick_label
,这可以使这个绘图更容易(see here):
hist, bin_edges = np.histogram(data,bins)
ax.bar(range(len(hist)),hist,width=1,align='center',tick_label=
['{} - {}'.format(bins[i],bins[i+1]) for i,j in enumerate(hist)])
如果您的 bin 的实际值并不重要,但您想要具有完全不同数量级的值的直方图,则可以沿 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 的解决方案。
这是直方图
为了生成这个图,我做了:
bins = np.array([0.03, 0.3, 2, 100])
plt.hist(m, bins = bins, weights=np.zeros_like(m) + 1. / m.size)
但是,正如您所注意到的,我想绘制每个数据点的相对频率的直方图,其中只有 3 个具有不同大小的 bin:
bin1 = 0.03 -> 0.3
bin2 = 0.3 -> 2
bin3 = 2 -> 100
直方图看起来很难看,因为最后一个 bin 的大小相对于其他 bin 非常大。如何修复直方图?我想更改垃圾箱的宽度,但我不想更改每个垃圾箱的范围。
正如@cel 指出的那样,这不再是直方图,但您可以使用 plt.bar
和 np.histogram
来完成您的要求。然后,您只需将 xticklabels
设置为描述 bin 边缘的字符串。例如:
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
,你会发现 bar
现在需要一个 kwarg tick_label
,这可以使这个绘图更容易(see here):
hist, bin_edges = np.histogram(data,bins)
ax.bar(range(len(hist)),hist,width=1,align='center',tick_label=
['{} - {}'.format(bins[i],bins[i+1]) for i,j in enumerate(hist)])
如果您的 bin 的实际值并不重要,但您想要具有完全不同数量级的值的直方图,则可以沿 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 的解决方案。