3

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?

idazuwaika
  • 2,749
  • 7
  • 38
  • 46

3 Answers3

3

Well, this is two questions. But as far as the category names overlapping, you can fix that by setting the tick label position to LOW:

from pptx.enum.chart import XL_TICK_LABEL_POSITION

category_axis = chart.category_axis
category_axis.tick_label_position = XL_TICK_LABEL_POSITION.LOW

On the transparent (or perhaps white) colored bars, I suspect you might be seeing a PowerPoint version-dependent non-conformance with the spec that's cropped up elsewhere. I'd open it in PowerPoint and see what you can see on the properties dialog, whether the invert if negative option is appearing the way you set it and whether it operates when you set it manually using the PowerPoint UI.

If the "Invert if negative" checkbox is not checked in the Data Series > Fill (format) dialog when you've set it to True using python-pptx AND clicking that checkbox on solves the display issue, you can open a case on the python-pptx GitHub issues list and we'll get a fix into the next release.

Basically, certain versions of PowerPoint interpret a boolean element type such as this inconsistently, depending on the element (<c:invertIfNegative> in this case), so while it passes the tests, it doesn't behave as expected in all PowerPoint versions. It could be that's what you're seeing.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • The option 'Invert if negative' is checked in the UI, when I set the option to True in python-pptx. The bars however are still transparent. When I unchecked the box in the UI, the bars gained the color as defined in python-pptx. I use PowerPoint 2016. – idazuwaika May 28 '17 at 11:05
  • @idazuwaika What about when you assign False to `.invert_if_negative` in python-pptx? What is the checkbox then? I think it might be time to inspect the underlying XML for the chart with `opc-diag` and compare the before and after cases to see what PowerPoint does to the XML to effect the behavior you're expecting. – scanny May 28 '17 at 18:57
  • Is there a solution to this 4,5 years later? It seems like this is still an issue.. – Quastiat Nov 29 '21 at 15:22
  • @Quastiat as far as I know this is working properly. If you're having trouble then raise a new question describing your particular situation and we'll have a look. – scanny Nov 29 '21 at 17:30
1

try to dig a little into the xml:

    ccaxis = chart.category_axis
    ticklblPos = ccaxis._element.find('{http://schemas.openxmlformats.org/drawingml/2006/chart}tickLblPos')
    ticklblPos.set('val','low')
lkonweb
  • 27
  • 8
0

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
Dadep
  • 2,796
  • 5
  • 27
  • 40