我遇到了这样一个问题:如果用户第一次想要绘制图形,它可以正常工作,但如果再次选择相同的选项,我会看到如下错误。在我看来,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
看起来
labels
变量(看起来是全局变量)和plotBarGraph
中fiscal_year
的维度之间可能不匹配。变量index
的维数取决于labels
的长度。然后将fiscal_year
展平为未知维度的列表,并尝试将它们用作具有len(labels)
条的条形图的高度当
len(labels) != len(fiscal_year)
时,有太多或太少的高度matplotlib
无法正确绘制条形图,这就是您看到错误的原因相关问题 更多 >
编程相关推荐