【发布时间】:2018-01-19 01:56:35
【问题描述】:
我看到一篇关于在 Matplotlib here 中为多个饼图分配相同颜色的帖子
但是关于索引轴对象有一些我不明白的地方。
代码如下:
import numpy as np
import matplotlib.pyplot as plt
def mypie(slices,labels,colors):
colordict={}
for l,c in zip(labels,colors):
print l,c
colordict[l]=c
fig = plt.figure(figsize=[10, 10])
ax = fig.add_subplot(111)
pie_wedge_collection = ax.pie(slices, labels=labels, labeldistance=1.05)#, autopct=make_autopct(slices))
for pie_wedge in pie_wedge_collection[0]:
pie_wedge.set_edgecolor('white')
pie_wedge.set_facecolor(colordict[pie_wedge.get_label()])
titlestring = 'Issues'
ax.set_title(titlestring)
return fig,ax,pie_wedge_collection
slices = [37, 39, 39, 38, 62, 21, 15, 9, 6, 7, 6, 5, 4, 3]
cmap = plt.cm.prism
colors = cmap(np.linspace(0., 1., len(slices)))
labels = [u'TI', u'Con', u'FR', u'TraI', u'Bug', u'Data', u'Int', u'KB', u'Other', u'Dep', u'PW', u'Uns', u'Perf', u'Dep']
fig,ax,pie_wedge_collection = mypie(slices,labels,colors)
plt.show()
在行中:for pie_wedge in pie_wedge_collection[0] 索引 [0] 有什么作用?如果我不使用它或使用pie_wedge_collection[1],代码将不起作用@
这里的斧头不是只有一个地块吗?所以我不明白索引在做什么。
【问题讨论】:
-
API-docs: 只看返回值。
-
或者打印出
pie_wedge_collection和type(pie_wedge_collection)。然后你看,为什么这个元组中的元素 0 和元素 1 不同。
标签: python matplotlib indexing pie-chart axes