2016-05-16 71 views
0

我遇到了問題,其中第一次如果用戶想要繪製圖形,它可以正常工作,但如果再次選擇相同的選項,則會看到如下所示的錯誤。對我來說,似乎pyplot所持有的一些資源沒有得到釋放。我的理解是否正確?如何解決?如果錯了會是什麼原因呢?使用pyplot進行數據處理

只是更多的信息 - 它只發生在我試圖繪製圖表 - 如果我只想顯示數據,它工作正常。

代碼:

default_options = {'0' : 'Below are the options', 
        '1' : '1 . Enter File path\'s for processing', 
        '2' : '2 . Display all categories', 
        '3' : '3 . Display type of forms', 
        '4' : '4 . Display for given form annual result', 
        '5' : '5 . Compare two form annual result ', 
        '6' : '6 . Compare two years annual result', 
        '99': '0 . Type 0 to exit ' 
        } 

labels = ["1st_quat", "2nd_quat", "3rd_quat", "4th_quat"] 

def plotBarGraph(self, fiscal_year): 
    index = np.arange(len(labels)) 
    bar_width = 0.1 
    fig, ax = plt.subplots() 
    appli_received = [data[0] for data in fiscal_year] 
    appli_accepted = [data[1] for data in fiscal_year] 
    appli_denied = [data[2] for data in fiscal_year] 
    appli_pending = [data[3] for data in fiscal_year] 

    plt.bar(index, appli_received, bar_width, alpha = 0.5, color='b') 
    plt.bar(index + bar_width, appli_accepted,bar_width, alpha = 0.5, color='r') 
    plt.bar(index + bar_width * 2, appli_denied, bar_width, alpha = 0.5, color='g') 
    plt.bar(index + bar_width * 3, appli_pending,bar_width, alpha = 0.5, color='k') 
    plt.xticks(index, labels, rotation = 35) 
    ax.tick_params(axis='both', which='major') 
    plt.legend(['Received', 'Approved', 'Denied', 'Pending'], loc='upper left') 
    plt.show() 

def getUserInput(self): 
    self.displayUserOption() 
    user_input = int(raw_input()) 
    if self.validateUserInput(user_input): 
     if user_input: 
      self.processForAllFile(user_input) 
      self.getUserInput() 
    else: 
     print " Please enter valid option \n " 
     self.getUserInput() 

錯誤打印時用戶試圖再次繪製圖形(我手動關閉的情節,以及選擇再次繪製它之前)

self.plotBarGraph(report) 

    plt.bar(index, appli_received, bar_width, alpha = 0.5, color='b') 
    File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2089, in bar 
    ret = ax.bar(left, height, width, bottom, **kwargs) 
    File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 4733, in bar 
    nbars) 
AssertionError: incompatible sizes: argument 'height' must be length 4 or scalar 
+0

爲了理解發生了什麼,看起來我們需要看到'plotBarGraph'的代碼 – mobiusklein

+0

@mobiusklein - 我已經更新了代碼請求的問題 – oneday

回答

0

它看起來像你可能有顯示爲全局的labels變量和的尺寸plotBarGraph之間不匹配。變量index的尺寸取決於labels的長度。然後,將fiscal_year平鋪爲未知尺寸的列表,並嘗試將它們用作條形圖的高度,精確到len(labels)

len(labels) != len(fiscal_year)時,matplotlib的高度太多或太少,無法正確繪製條形圖,這就是爲什麼你會看到你所看到的錯誤。

+0

正如我所說的它第一次工作 - 只有當我選擇再次選項它不起作用並拋出錯誤。正如你從我的代碼中可以看到的那樣,用戶可以多次選擇相同的選項,所以第一次情況很好,第二次出現錯誤 – oneday

+0

你能否確認'labels'的值是否與' fiscal_year'? 「標籤」的價值如何確定? – mobiusklein

+0

我已更新標籤值 - 是它的全局變量。很抱歉,我無法理解它是否能夠第一次工作,爲什麼映射必須第二次做任何事情 - 沒有數據發生變化,只有第二次發送相同的請求。理解你的觀點會很棒 – oneday