3个元素=字典中的3个键 2个元素=字典中的2个键
当试图显示2个元素时,一切正常。 当用3尝试相同的事情时,没有任何事情发生(图片不会生成)
from pylab import *
binNames_tuple = ()
binResult_list = []
dic = {'7.2': '2', '7.1': '1', '7.3': '3'}
for item in dic:
#append to tuple ( labels for the pi chart)
binNames_tuple = binNames_tuple + (item,)
#append to list (the results to display )
binResult_list.append(dic[item])
print binResult_list
print binNames_tuple
mycolors=['#9d1507', '#71c42c','#0099cc']
try :
figure(1, figsize=(3, 3), dpi=70,)
ax = axes([0.1, 0.1, 0.8, 0.8])
# The slices will be ordered and plotted counter-clockwise.
fracs = binResult_list
explode=(0, 0)
pie(fracs, explode=explode,labels = binNames_tuple , autopct='%1.1f%%', shadow=True, startangle=90,colors=mycolors)
savefig(('abcd.png'), transparent=True)
close()
except Exception as e :
print str (e)
答案 0 :(得分:0)
您的尝试,除了语句无法引发断言错误assert(len(x)==len(explode))
。在您的示例中,explode
列的大小应与dict
相同,例如
from pylab import *
binNames_tuple = ()
binResult_list = []
dic = {'7.2': '2', '7.1': '1', '7.3': '3'}
for item in dic:
#append to tuple ( labels for the pi chart)
binNames_tuple = binNames_tuple + (item,)
#append to list (the results to display )
binResult_list.append(dic[item])
print binResult_list
print binNames_tuple
mycolors=['#9d1507', '#71c42c','#0099cc']
try :
figure(1, figsize=(3, 3), dpi=70,)
ax = axes([0.1, 0.1, 0.8, 0.8])
# The slices will be ordered and plotted counter-clockwise.
fracs = binResult_list
explode=(0, 0, 0)
pie(fracs, explode=explode,labels = binNames_tuple , autopct='%1.1f%%', shadow=True, startangle=90,colors=mycolors)
savefig(('abcd.png'), transparent=True)
close()
except Exception as e :
print str (e)
raise
请注意,这会将脚本abcd.png保存在脚本目录中。如果您想要显示图片,则应使用plt.show()
。