gpt4 book ai didi

每个类别的Python pptx自定义颜色

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:52 25 4
gpt4 key购买 nike

我在这里看例子: 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 上回答了这个问题,现在可以修改饼图颜色。

由于饼图只是一系列的多个点,您需要单独修改每个点。这可以通过遍历第一个 Serie 的每个点(因为它是饼图中唯一的点)并根据需要更改颜色来完成。点颜色在 .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])

干杯!

关于每个类别的Python pptx自定义颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34264715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com