Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pptx custom color for each category

I'm looking at example here: 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

But I can't understand how to make each category with custom not automatic color: west is yellow, east is blue, north is grey, south is red, other as brown for example.

like image 730
sevatster Avatar asked Feb 08 '23 07:02

sevatster


2 Answers

I've responded to this question on Github, it is now possible to modify pie charts colors.

As the pie chart is only one serie of multiple points, you'll need to modify every point individually. This can be done by itering through each points of the first Serie (as it's the only one you have in a pie chart) and changing the color with whatever you like. The point color is in the .format.fill argument, that you can interact with easily using the links scanny provided above.

Here is a simple snippet for your use case:

        # [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 !

like image 78
Milo Parigi Avatar answered Feb 24 '23 10:02

Milo Parigi


UPDATE: Access to pie-sector fill was added in release after the original answer:

This makes the first pie-chart sector red:

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)

Repeat the last three lines for each additional desired point, or perhaps something fancier like this to apply the theme colors:

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

Custom coloring on a series is accomplished using the .fill attribute on the series.

Unfortunately that attribute hasn't been implemented yet for pie charts, only for bar and column charts. http://python-pptx.readthedocs.org/en/latest/api/chart.html#barseries-objects

It is possible to change the default coloring though, in the "template" .pptx file you start with, which accomplishes the same thing for many folks. All the charts in the file will have the same coloring, but it doesn't have to be the built-in defaults.

like image 36
scanny Avatar answered Feb 24 '23 09:02

scanny