我遇到了問題,其中第一次如果用戶想要繪製圖形,它可以正常工作,但如果再次選擇相同的選項,則會看到如下所示的錯誤。對我來說,似乎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
爲了理解發生了什麼,看起來我們需要看到'plotBarGraph'的代碼 – mobiusklein
@mobiusklein - 我已經更新了代碼請求的問題 – oneday