【问题标题】:Python pptx custom color for each category每个类别的Python pptx自定义颜色
【发布时间】:2015-12-14 10:29:10
【问题描述】:

我在这里查看示例: 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

但我不明白如何使用自定义而不是自动颜色来制作每个类别: 西黄,东蓝,北灰,南红,其他如棕色。

【问题讨论】:

标签: python python-pptx


【解决方案1】:

我已经在Github回复了这个问题,现在可以修改饼图颜色了。

由于饼图只是一系列的多个点,您需要单独修改每个点。这可以通过遍历第一个 Serie 的每个点来完成(因为它是饼图中唯一的一个),并随心所欲地更改颜色。点颜色在 .format.fill 参数中,您可以使用上面提供的链接 scanny 轻松与之交互。

这里是您的用例的简单 sn-p:

        # [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])

干杯!

【讨论】:

    【解决方案2】:

    更新:在原始答案之后的版本中添加了对饼形填充的访问:

    这会使第一个饼图扇区变为红色:

    from pptx.dml.color import RGBColor
    
    points = pie_chart.plots[0].series[0].points
    fill = points[0].format.fill
    fill.solid()
    fill.fore_color.rgb = RGBColor(255, 0, 0)
    

    为每个额外的所需点重复最后三行,或者可能像这样更花哨的东西来应用主题颜色:

    from pptx.enum.dml import MSO_THEME_COLOR
    
    accent_colors = (
        MSO_THEME_COLOR.ACCENT_1,
        MSO_THEME_COLOR.ACCENT_2,
        MSO_THEME_COLOR.ACCENT_3,
        MSO_THEME_COLOR.ACCENT_4,
        MSO_THEME_COLOR.ACCENT_5,
        MSO_THEME_COLOR.ACCENT_6,
    )
    
    pie_chart_points = pie_chart.plots[0].series[0].points
    
    for point, accent_color in zip(pie_chart_points, accent_colors):
        fill = point.format.fill
        fill.solid()
        fill.fore_color.theme_color = accent_color
    

    系列的自定义着色是使用系列的.fill 属性完成的。

    不幸的是,该属性尚未针对饼图实现,仅针对条形图和柱形图。 http://python-pptx.readthedocs.org/en/latest/api/chart.html#barseries-objects

    但是,可以在您开始使用的“模板”.pptx 文件中更改默认颜色,这对许多人来说完成了同样的事情。文件中的所有图表都将具有相同的颜色,但不必是内置默认值。

    【讨论】:

    【解决方案3】:

    可以在折线图中更改线条的颜色,因为我尝试了很多建议,但都没有成功,例如以下代码:

                _green = RGBColor(156, 213, 91)
                plot = chart.plots[0]
                series = plot.series[0]
                line = series.format.line
                line.fill.rgb = _green 
    

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      相关资源
      最近更新 更多