索引 matplotlib 轴在循环中有什么作用?

What does indexing the matplotlib axis do in a loop?

我看到 post 关于在 Matplotlib 中的多个饼图中分配相同的颜色

但是对于轴对象的索引我有些不明白。

代码如下:

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]

,该代码将不起作用

这里的ax对象不是只有一个plot吗?所以我不明白索引是干什么的。

根据 Matplotlib 文档,pie() return 两个或三个列表:

  1. 列表matplotlib.patches.Wedge
  2. matplotlib.text.Text 个标签列表
  3. (有条件地)matplotlib.text.Text 数据标签列表

您的代码需要操纵 Wedge 对象 return 由 pie() 编辑的边缘和面颜色,它们位于 [= 中的第一个列表(零索引)中29=]值,pie_wedge_collection.