当一些箱子支配其他箱子时如何绘制直方图

时间:2013-11-30 08:59:51

标签: python matplotlib histogram

我想绘制一个直方图,解释数据的分布方式。我的问题是大多数数据的值都很小。因此,如果你使用10个箱子,它就不会那么描述;大多数数据挤压在0.0-0.1 bin。如果使用1000个分档,则由于xlabels,直方图看起来不太好 因为我们有太多的垃圾桶,所以有些垃圾箱与其他垃圾箱重叠。

我尝试使用诸如对数标度,标准化版本,但我仍然无法获得信息性直方图。我已经计算了(1000)箱和计数。读取数据的代码如下。您可以运行它:./sub-histogram.py hist-data.txt 2500 0。 0表示您使用原始计数(第一行)。最后一行包含bin值。

第一个想法是将计数和箱子合并到某个阈值。如果计数小于某个阈值,则累计此计数并跳过此bin。我现在还没有进一步的想法,但我相信如果你使用直方图,你就会遇到这个问题。在那儿 这种情况的任何解决方案?数据和所有内容均为here

import sys
from itertools import izip
import matplotlib.pyplot as plt
import numpy as np

lines = open(sys.argv[1]).readlines()
threshold = float(sys.argv[2])
count_type = int(sys.argv[3]) # 0 for raw counts, 1 for normalized counts, 2 for log counts

# reading 
C = map(float, lines[count_type][1:-2].replace(",", "").split())
B = map(float, lines[3][1:-2].replace(",", '').split())

# merging method. 
# accumulate the counts with respect to threshold.
counts = []
bins = []
ct = 0
for c, b in izip(C,B):
    ct += c
    if ct >= threshold:
        counts.append(ct)
        bins.append(b)
        ct = 0

if ct > 0:
    counts.append(ct)
    bins.append(b)
    ct = 0

print counts
print bins

bar_width= 0.005
plt.xticks(np.linspace(0,2,41))
plt.bar(bins, counts, bar_width)
plt.show()

1 个答案:

答案 0 :(得分:0)

我建议为你的小值和一个比bin更大的bin有多个bin。 100个箱子的值在0.000到0.200之间,间隔为0.002,一个箱子用于0.200以上的所有物品(你可能有10个箱子用于0.000-0.009,10个用于0.010-0.090等),你需要覆盖X轴上的标签,但ax.set_xticklabels允许你这样做。