使用heapq.nlargest在For循环中重叠直方图-Python [英] Overlapping Histogram in For Loop with heapq.nlargest - Python

查看:118
本文介绍了使用heapq.nlargest在For循环中重叠直方图-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我创建了一个for循环,该循环遍历数组并将结果转储到PDF上,但是我很难将其与heapq.nlargest结合使用。
我想将直方图与每个熊猫数组的前10%得分重叠。

At the moment I've created a for loop which cycles through my arrays and dumps out the result on a PDF however I'm having trouble combining this with heapq.nlargest. I want to overlap my histograms with the top 10% scores for each array with pandas.

当前,代码为

  x = list(df1.columns.values)
fig = plt.figure(num=None, figsize=(30, 200),  dpi=80, facecolor='w', edgecolor='w')


for i in range(6):#(len(df1.ix[i])):
    val= x[i]   
    y = df1.iloc[:,i]
    yy = heapq.nlargest(len(df1.iloc[:,i])//10, df1.iloc[:,i])


    ax = fig.add_subplot(len(df1.ix[0]),3,i+1)   
    plt.hist(y, bins=np.logspace(-4, 3, 100))
    plt.hist(yy, bins=np.logspace(-4, 3, 100))

    plt.savefig('D:/All Documents/Frequency_Distribution_Scores.pdf')

当我介绍

yy = heapq.nlargest(len(df1.iloc[:,i])*p//100, (df1.iloc[:,i]))
plt.hist(yy, bins=np.logspace(-4, 3, 100))

似乎只绘制了我所有gra中第一个数组的前10%值phs,而不是找到每个数组的前10%。

It seems to just plot the top 10% of values of the 1st array on all my graphs, rather than finding the top 10% of each array.

有人有指针吗?
欢呼声

Anyone have any pointers? Cheers

推荐答案

解决了!

x = list(df1.columns.values)
fig = plt.figure(num=None, figsize=(30, 200),  dpi=80, facecolor='w',  edgecolor='w')


for i in range(len(df1.ix[i])):
    val= x[i]   
    y = df1.iloc[:,i]
    yy_s = np.sort(df1.iloc[:,i])[::-1]
    yy_s_trim0 = yy_s[np.where(yy_s > 0)]
    yy_10 = yy_s_trim0[0:(len(yy_s_trim0)/10)]

    ax = fig.add_subplot(len(df1.ix[0]),3,i+1)   
    plt.hist(y, bins=np.logspace(-4, 3, 100))
    plt.hist(yy_10, bins=np.logspace(-4, 3, 100))

这篇关于使用heapq.nlargest在For循环中重叠直方图-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆