开发者

Plotting data using percent unit

I have used go开发者_开发知识库ogle for search but i do not found an answer.

My problem:

I have data array and i would like plot by using percent unit. Example:

data: [1, 3, 1, 3, 3, 2, 4, 5]

  • 1: 0.25

  • 2: 0.125

  • 3: 0.375

  • 4: 0.125

  • 5: 0.125

PS: i do not want use R only python, matplotlib and if need numpy


Take a look at the hist function in matplotlib: http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.hist Numpy also has a histogram function: http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html


edit: sorry misread your question, i thought you meant only python. Someone will hopefully post a matplotlib or numpy solution.

here's one way to do it by sorting the list:

>>> a = [1, 3, 1, 3, 3, 2, 4, 5]
>>> 
>>> def unit_percents(L1):
...     ret = {}
...     L = L1[:]
...     sorted(L)
...     if L:
...         cur_count = 1
...         for i in range(len(L)-1):
...             cur_count+=1
...             if L[i] != L[i+1]:
...                 ret[L[i]]=float(cur_count)/len(L)
...                 cur_count=1
...         ret[L[-1]]=float(cur_count)/len(L)
...     return ret
... 
>>> unit_percents(a)
{1: 0.25, 2: 0.25, 3: 0.375, 4: 0.25, 5: 0.125}

also:

>>> dict([(x,float(a.count(x))/len(a)) for x in set(a)])
{1: 0.25, 2: 0.125, 3: 0.375, 4: 0.125, 5: 0.125}
>>> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜