2014-06-18 46 views
-1

我有一個情節,一組應用程序上行/下行,分佈與CDF一起顯示,我想通過在其圖例中添加更多信息來改善情節,例如I希望它給我看每個變量, 點的數量的代碼,而不是顯示我:使用Matplotlib向圖例添加信息

--- Uplink 
___ Downlink 

我想看到這一點:

--- Uplink(54) 
___ Downlink(88) 

,其中54是繪製數點在這個上行鏈路中,其中88是t中繪製點的數量他的下行

這裏是我的代碼,我想提高:

import csv 
import matplotlib.pyplot as plt 
import pylab 
#---Reading from a csv file 
def getColumn(filename, column): 
    results = csv.reader(open(filename), delimiter="\t") 
    return [result[column] for result in results] 
#---the cdf Function 
def cdf(inputList): 
    outputList = [] 
    inputList.sort() 
    for i in range(0, len(inputList)): 
     outputList.append([inputList[i], (i+1.0)/len(inputList)]) 
    return outputList 
#--getting the data i want from the csv file 
fgfS=getColumn("dataFG.csv",2) 
bgfS=getColumn("dataBG.csv",2) 
fgifS=getColumn("dataiFG.csv",2) 
bgifS=getColumn("dataiBG.csv",2) 
#---the points to plot: 
fgfS1=[] 
fgfS2=[] 

for i in cdf(map(float,fgfS)): 
    fgfS1.append(i[0]) 
    fgfS2.append(i[1]) 

bgfS1=[] 
bgfS2=[] 
for i in cdf(map(float,bgfS)): 
    bgfS1.append(i[0]) 
    bgfS2.append(i[1]) 

fgifS1=[] 
fgifS2=[] 

for i in cdf(map(float,fgifS)): 
    fgifS1.append(i[0]) 
    fgifS2.append(i[1]) 

bgifS1=[] 
bgifS2=[] 
for i in cdf(map(float,bgifS)): 
    bgifS1.append(i[0]) 
    bgifS2.append(i[1]) 


fig2, (ax2,ax02) = plt.subplots(1,2, sharey=True) 
labels=('Uplink','Downlink') 
fig2.suptitle('Applications Down/Up link', fontsize=20) 
#******************  
ax2.set_title('App 1') 
#ax2.set_xlabel('seconds') 
ax2.set_ylabel('CDF') 
ax2.grid(True) 
ax2.plot(fgfS1,fgfS2, 'b') 
ax2.plot(bgfS1,bgfS2, 'c') 
ax2.set_xscale('log') 
ax2.legend(labels,loc='best') 
#****************** 
ax02.set_title('App 3') 
ax02.set_xlabel('seconds') 
ax02.set_ylabel('CDF') 
ax02.grid(True) 
ax02.plot(fgifS1,fgifS2, 'b') 
ax02.plot(bgifS1,bgifS2, 'c') 
ax02.set_xscale('log') 
ax02.legend(labels,loc='best') 

所以我想補充LEN(bgfS1)爲例,但我仍然奧斯卡最佳噸弄清楚在何處以及如何....

+2

你的問題不涉及CDF,你提供太多不相干的代碼。基本上你想要的是用圖例顯示附加信息 - 這是一個有效的問題,如果你的答案和代碼是簡明扼要的,而不僅僅是整個代碼,你將增加獲得答案的機會。 – Korem

回答

0

只需將其附加到您的labels列表組件中,即可確定最終顯示的內容。

所以,你會想:

ax2.legend(['{} ({})'.format(lab, cnt) for lab,cnt in izip(labels, (len(fgfS1), len(bgfS1)))],loc='best') 

(這是本質上只是追加數到每一個標籤)