在matplotlib python中绘制离散计数数据的有界CDF

时间:2011-12-13 05:15:38

标签: python graph plot matplotlib

我有一个numpy数组“data”,它只包含一组整数计数。给定另一个数组“bins”,我只想制作一个频率图/ CDF,其中“数据”中总条目的比例至少有[0] - 多个计数,至少二进制数[1] - 很多计数等等并在matplotlib中将其制作成条形图。例如,如果:

data = [1, 4, 5, 10]
bins = [0, 5, 6, 7]

然后结果应该是条形图,其在x轴上具有0,5,6,7,然后是具有值> = 0的数据的分数,然后值> = 5,等等。我在matplotlib中制作了这种带有指定箱子的“离散”CDF条形图?感谢。

1 个答案:

答案 0 :(得分:1)

如果你正在使用matplotlib,我假设你也在使用numpy,所以你可以通过bins计算出data的分数,data>bin,所有{ {1}}中的{1}}以及data中的所有datas。{/ p>

为此,这可能有效:

bin

现在只针对bins绘制import numpy as np # turn data into numpy array for easier manipulation data2 = np.array(data) n = len(data2) # calculate fractions for each bin in bins # astype('float') because otherwise you end up doing integer arithmetic fracs = [ sum(data2>=bin).astype('float')/n for bin in bins ] ,例如

bins