我正在看这里的例子:https://python-pptx.readthedocs.org/en/latest/user/charts.html?highlight=color#pie-chart https://python-pptx.readthedocs.org/en/latest/user/charts.html?highlight=color#pie-chart
chart_data = ChartData()
chart_data.categories = ['West', 'East', 'North', 'South', 'Other']
chart_data.add_series('Series 1', (0.135, 0.324, 0.180, 0.235, 0.126))
chart = slide.shapes.add_chart(
XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data
).chart
chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.include_in_layout = False
chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%'
data_labels.position = XL_LABEL_POSITION.OUTSIDE_END
但我不明白如何使每个类别具有自定义而不是自动颜色:
西边是黄色,东边是蓝色,北边是灰色,南边是红色,其他的例如棕色。
我已经回答过这个问题Github https://github.com/scanny/python-pptx/issues/357#issuecomment-468697153,现在可以修改饼图颜色。
由于饼图只是一系列多个点,因此您需要单独修改每个点。这可以通过迭代第一个系列的每个点(因为它是饼图中唯一的一个)并根据您喜欢的颜色更改颜色来完成。点颜色位于 .format.fill 参数中,您可以使用上面提供的链接 scanny 轻松与之交互。
这是您的用例的一个简单片段:
# [yellow, blue, grey, red, brown]
color_list = ["ffff00", "0000ff", "D3D3D3", "ff0000", "A52A2A"]
# Go through every point of the first serie and modify the color
for idx, point in enumerate(chart.series[0].points):
col_idx = idx % len(color_list)
point.format.fill.solid()
point.format.fill.fore_color.rgb = RGBColor.from_string(color_list[col_idx])
Cheers !
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)