Python PPTX Bar Chart negative values

Viewed 943

I use the following code to give specific color for the bars, for a bar chart generated by python-pptx.

chart.series[0].format.fill.solid()
chart.series[0].format.fill.fore_color.rgb = RGBColor(160,190,230)

This looks fine most of time. However, then the bar charts have negative values, the bars, several issues occur.

  • The negative bars have no colors and is transparent
  • The bars overlap with the category names

sample image

I've tried both of below options to no effect.

chart.series[0].invert_if_negative = True
chart.series[0].invert_if_negative = False

Is there a way to make the chart renders properly, with color and not overlapping with category names?

3 Answers

Replacing fill.solid() by fill.patterned() worked for me :

color_red = RGBColor(254, 98, 94)
color_green = RGBColor(1, 194, 170)

for idx, point in enumerate(series.points):
    fill = point.format.fill
    fill.patterned()
    if data[idx]<0 :
        fill.fore_color.rgb = color_red   
    else:
        fill.back_color.rgb = color_green
Related