Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color Specific Bar Chart Differently in Python PPTX

Is it possible in python-pptx to color a specific bar in a bar chart different from others? My intention is to color the bar based on values. By default, all is blue, but if the value is below certain threshold, the bar is colored red.

Below is my current code with all bars colored blue-ish, rgb(65,105,225)

 # HUMIDITY CHART
    chart_data = ChartData()              
    chart_data.categories = list(m.columns)

    chart_data.add_series('Humidity', list(m.loc[svc,'Humidity']), 3)
    graphic_frame = s.placeholders[14].insert_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, chart_data)
    chart = graphic_frame.chart

    plot = chart.plots[0]
    plot.has_data_labels = True
    plot.overlap = 0
    plot.gap_width = 30
    data_labels = plot.data_labels
    data_labels.font.size = Pt(11)
    data_labels.font.color.rgb = RGBColor(0,0,0)
    data_labels.number_format = "#,##0"

    chart.series[0].fill.solid()
    chart.series[0].fill.fore_color.rgb = RGBColor(65,105,225)
like image 654
idazuwaika Avatar asked Feb 11 '17 21:02

idazuwaika


1 Answers

I believe this will do the trick:

bar_idx = the_offset_of_the_bar_I_want_to_set_to_a_different_color
point = series.points[bar_idx]  # a (data) point in a bar chart is a bar
fill = point.format.fill
fill.solid()
fill.fore_color.rgb = RGBColor(65, 105, 225)

There's a gap in the documentation for series.points, which is why you probably didn't find this.

Let us know how you go :)

like image 67
scanny Avatar answered Nov 06 '22 06:11

scanny