本文介绍了带有单独列表的直方图表示频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个列表:

    x1 = [1,2,3,4,5,6,7,8,1,10]
    x2 = [2,4,2,1,1,1,1,1,2,1]

在此,列表的每个索引i是时间点,并且x2[i]表示在时间i观察到的比x1[i]的次数(频率).还要注意x1 [0] = 1和x1 [8] = 1,总频率为4(= x2 [0] + x2 [8]).

Here, each index i of the list is a point in time, and x2[i] denotes the number of times (frequency) than x1[i] was observed was observed at time i. Note also that x1[0] = 1 and x1[8] = 1, with a total frequency of 4 (= x2[0] + x2[8]).

如何有效地将其转换为直方图?下面是一种简单的方法,但这可能效率不高(创建第三个对象并循环执行),并且由于我拥有巨大的数据,这会对我造成伤害.

How do I efficiently turn this into a histogram? The easy way is below, but this is probably inefficient (creating third object and looping) and would hurt me since I have gigantic data.

import numpy as np
import matplotlib.pyplot as plt

x3 = []
for i in range(10):
    for j in range(x2[i]):
        x3.append(i)

hist, bins = np.histogram(x1,bins = 10)
width = 0.7*(bins[1]-bins[0])
center = (bins[:-1]+bins[1:])/2
plt.bar(center, hist, align = 'center', width = width)
plt.show()

推荐答案

一种方法是使用x3 = np.repeat(x1,x2)并使用x3制作直方图.

One way is to use x3 = np.repeat(x1,x2) and make a histogram with x3.

这篇关于带有单独列表的直方图表示频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 04:03