Python PPTX Bar Chart negative values

一个人想着一个人 提交于 2019-12-01 23:42:32

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.

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')
Julien Krywyk

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!