【问题标题】:What does indexing the matplotlib axis do in a loop?在循环中索引 matplotlib 轴有什么作用?
【发布时间】: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_collectiontype(pie_wedge_collection)。然后你看,为什么这个元组中的元素 0 和元素 1 不同。

标签: python matplotlib indexing pie-chart axes


【解决方案1】:

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

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

您的代码需要处理由pie() 返回的Wedge 对象的边缘和面颜色,这些对象位于返回值pie_wedge_collection 的第一个列表(零索引)中。

【讨论】:

    猜你喜欢
    • 2018-11-15
    • 2012-10-20
    • 2019-06-14
    • 2020-08-25
    • 2012-09-02
    • 2015-09-22
    • 1970-01-01
    • 2020-08-07
    • 2017-11-15
    相关资源
    最近更新 更多