【问题标题】:fill the slices with different colors on a pie chart在饼图上用不同颜色填充切片
【发布时间】:2018-03-01 22:40:28
【问题描述】:

我有一个有 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

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

【问题讨论】:

  • 你看过the documentation吗?那里的例子特别包括这个。
  • 如果一次只能设置一种颜色,请使用循环。

标签: python graph reportlab


【解决方案1】:

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

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

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

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

【讨论】:

  • 迭代slices 不是更安全吗?并使用% 作为颜色列表。然后,您不再需要与切片一样多(或更少!)颜色。
猜你喜欢
  • 1970-01-01
  • 2015-12-27
  • 1970-01-01
  • 2011-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多