【问题标题】:Histogram with percentage bins in Python/numpy?Python / numpy中带有百分比箱的直方图?
【发布时间】:2016-03-02 08:57:26
【问题描述】:

我需要从一组 2D 数据中创建一个包含百分比箱​​的直方图,如下所示(这基本上是一组来自各种设备的报告,每一行都是一个设备报告其在给定小时内的状态):

# hour # parameter (in percents)
00     10
00     20
00     30
01     40
01     50
...

这样就会有一个按小时和百分位分类的设备报告的堆叠直方图摘要,就像下面的 gnuplot 示例一样,其中 bin 代表报告所属的百分位数(例如 0

现在我只想创建一个二维数组并将其全部输入 gnuplot,如下所示:

#!/usr/bin/python

import numpy as np
import sys

data = np.loadtxt('mac-quality.csv')
out = [ [ 0 for k in xrange(10) ] for i in (xrange(24) ) ]

for i in data:
    hour = i[0].astype(int)
    quality = i[1].astype(int)
    for bin in xrange(10):
        pct = bin * 10
        if quality > pct and quality < (pct + 10):
            print('Data: %s, H: %s Percentile: %s:') % (i, hour, pct)
            out[hour][bin] += 1
# print(out)

从 python 中生成这些直方图的正确方法是什么?

【问题讨论】:

  • 能否提供cvs 数据的摘录?

标签: python numpy gnuplot histogram


【解决方案1】:

这完全使用了您的 python 代码,但使用一些 Matplotlib 库代码对其进行了扩展,这些代码通常用于在 python 中绘图。这通常会替换 python 中的 gnuplot。

import numpy as np
import sys
import matplotlib.pyplot as plt

data = np.loadtxt('mac-quality.csv')
out = [ [ 0 for k in xrange(10) ] for i in (xrange(24) ) ]

# Number of bins you have
nBins = 10

for i in data:
    hour = i[0].astype(int)
    quality = i[1].astype(int)
    for bin in xrange(10):
        pct = bin * 10
        if quality > pct and quality < (pct + 10):
            print('Data: %s, H: %s Percentile: %s:') % (i, hour, pct)
            out[hour][bin] += 1


plt.hist(data, nBins, normed=1, histtype='bar', stacked=True)
plt.title('Some Title')
plt.show()

【讨论】:

  • 不完全是这样,它绘制的是按百分位分箱的数据,而我想要一个按小时分箱的堆叠百分位直方图(小时是 X 轴,百分位是堆叠直方图条内的箱)。不过谢谢,这是一个好的开始。
猜你喜欢
  • 2019-03-12
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
  • 2022-08-14
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 2019-11-01
相关资源
最近更新 更多