【发布时间】:2013-09-30 19:46:00
【问题描述】:
假设我有两个列表:
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])。
如何有效地将其转换为直方图?简单的方法如下,但这可能效率低下(创建第三个对象并循环)并且会伤害我,因为我有巨大的数据。
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()
【问题讨论】:
-
不是
x2已经是直方图吗? -
@tcaswell 怎么样?如果您建议绘制条形图
x2,这对我不起作用,因为x1具有重复值。 -
对不起,还没有喝咖啡,快读吧。看我的回答。
标签: python matplotlib plot histogram