2011-05-08 71 views
1

很抱歉的混亂標題:■從分離代碼功能,而在使用Python中值函數

for x in frequency: 
     alphab = [x] 
     frequencies = [frequency[x]] 
     print alphab, frequencies 

我將如何能夠將下面的代碼從上面的代碼,同時仍然使用從輸出分離for x in frequency:如果我運行這裏的內容,將爲每個x的值打開直方圖,而不是整個字符串。 如果我在下面縮進任何東西,如此處所示,直方圖也只運行x的第一個值。 有沒有用整個字符串,而無需內for

pos = np.arange(len(alphab)) 
width = 1.0  

ax = plt.axes() 
ax.set_xticks(pos + (width/2)) 
ax.set_xticklabels(alphab) 
plt.xlabel('Letter') 
plt.ylabel('Absolute Frequency') 
plt.title("Absolute Frequency of letters in text") 
plt.bar(pos, frequencies, width, color='r') 
plt.show() 
+1

我不認爲有人真的得到你要找的東西... – 2011-05-08 11:07:13

+0

它不是已經分開?你必須更準確地解釋你想要做什麼。 – sth 2011-05-08 11:09:08

+0

哪一個是你的整個字符串,直方圖在哪裏?對不起,太難理解和錯誤的標籤了。 (我正在糾正它) – 2011-05-08 11:42:33

回答

0
def plotfreq(frequency, alphab): 

    pos = np.arange(len(alphab)) 
    width = 1.0  

    ax = plt.axes() 
    ax.set_xticks(pos + (width/2)) 
    ax.set_xticklabels(alphab) 
    plt.xlabel('Letter') 
    plt.ylabel('Absolute Frequency') 
    plt.title("Absolute Frequency of letters in text") 
    plt.bar(pos, frequency, width, color='r') 
    plt.show() 

for x in frequencies: 

    plotfreq(x, frequencies[x]) 

縮進直方圖功能,這是不是你正在尋找任何可能的方式?

0

我想你想在調用繪圖函數之前填充數組,例如

alphab = [] 
frequencies = [] 
for x in frequency: 
     alphab.append(x) 
     frequencies.append(frequency[x]) 

# .. some more code here .. 
plt.bar(pos, frequencies, width, color='r')