如何使用Pychart 填充饼图的颜色

1 投票
3 回答
1422 浏览
提问于 2025-04-16 18:36

我正在使用Python的Pychart库。
我写了以下代码来生成饼图:

from pychart import *
import sys

data = [("foo", 10),("bar", 20), ("baz", 30), ("ao", 40)]
theme.get_options()    
ar = area.T(size=(150,150), legend=legend.T(),
            x_grid_style = None, y_grid_style = None)

plot = pie_plot.T(data=data, arc_offsets=[0,0,0,0],
                  shadow = (0, 0, fill_style.gray50),
                  label_offset = 25,
                  arrow_style = arrow.a3)
ar.add_plot(plot)
ar.draw()

这段代码生成的饼图是灰度的。
我想要的是不同颜色的饼图,而不是灰度的。
我该怎么做呢?

3 个回答

1

你需要用 fill_styles 这个参数来指定填充颜色(这个列表的长度要和数据的长度一致):

from pychart import *
import sys

data = [("foo", 10),("bar", 20), ("baz", 30), ("ao", 40)]
theme.get_options()    
ar = area.T(size=(150,150), legend=legend.T(),
            x_grid_style = None, y_grid_style = None)

plot = pie_plot.T(data=data, arc_offsets=[0,0,0,0],
                  fill_styles = [fill_style.red, fill_style.blue, fill_style.green, fill_style.yellow],
                  shadow = (0, 0, fill_style.gray50),
                  label_offset = 25,
                  arrow_style = arrow.a3)
ar.add_plot(plot)
ar.draw()

这样就可以用颜色来显示了 :)

颜色列表

1

看起来pychart这个库自2006年以来就没有更新过了。我建议你看看matplotlib,这可能是Python中最好的绘图库。

饼图的例子可以在这里找到。

2

在调用 get_options() 之前,添加 theme.use_color = True

关于主题的说明:

use_color

这个变量的默认值是 False。如果设置为 True,PyChart 会给一些默认的对象上色。如果设置为 False,PyChart 就会使用灰度来显示这些对象的颜色。

撰写回答