在饼图上用不同颜色填充切片

fill the slices with different colors on a pie chart

我有一个包含 6 个切片的饼图,我期待更改饼图中显示的默认颜色。 据我所知,使用 .fillColor 我可以更改切片的颜色。

pie_chart = Drawing(200, 200)
pc = Pie()
pc.x = 65
pc.y = 15
pc.width = 150
pc.height = 150
pc.data = [65,13,12,9,1]
pc.labels = ['Name','Another','Yet Another','Test','Stack','Flow')

We can change the color of a slice using:
pc.slices[1].fillColor=red

但是我期待改变所有 6 个切片的颜色,因此我尝试了:

colores=[red,brown,violet,yellow,green,pink]
pc.slices.fillColors=colores

并输出错误:

AttributeError: Illegal attribute 'fillColors' in class WedgeProperties

我的问题是:如何在一行代码中更改每个切片的颜色?有没有属性可以这样做?

没有属性fillColors:是单数,fillColor;您不能简单地更改名称并期望解析器弄清楚您的意思。

正如 TemporalWolf 已经引导您走向的那样,该文档展示了如何使用循环执行此操作。在您的情况下,类似于:

for index, color in enumerate(colores):
    pc.slices[i].fillColor  = colores[i]

假设您的图表中有 len(colores) 个切片。