使用百分比单位绘制数据

2024-04-20 09:40:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我用谷歌搜索,但没有找到答案。在

我的问题是:

我有数据数组,我想用百分比单位绘图。示例:

数据:[1,3,1,3,3,2,4,5]

  • 1:0.25

  • 2:0.125

  • 3:0.375

  • 4:0.125

  • 5:0.125

PS:我不想只使用R python、matplotlib和numpy


Tags: 数据答案numpy绘图示例matplotlib单位数组
2条回答

编辑:抱歉误读了你的问题,我以为你说的只是python。有人希望发布matplotlib或numpy解决方案。在

以下是一种通过对列表进行排序的方法:

>>> 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}

同时:

^{pr2}$

相关问题 更多 >