2024-04-20 09:40:08 发布
网友
我用谷歌搜索,但没有找到答案。在
我的问题是:
我有数据数组,我想用百分比单位绘图。示例:
数据:[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
看看matplotlib中的hist函数:http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.hist Numpy还有一个直方图函数:http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html
编辑:抱歉误读了你的问题,我以为你说的只是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}
同时:
看看matplotlib中的hist函数:http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.hist Numpy还有一个直方图函数:http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html
编辑:抱歉误读了你的问题,我以为你说的只是python。有人希望发布matplotlib或numpy解决方案。在
以下是一种通过对列表进行排序的方法:
同时:
^{pr2}$相关问题 更多 >
编程相关推荐