简体   繁体   English

Python PPTX 中颜色特定的条形图不同

[英]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?在 python-pptx 中是否可以将条形图中的特定条形着色与其他条形不同? 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)下面是我当前的代码,所有条形都为蓝色,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)

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. series.points 的文档中有一个空白,这就是您可能没有找到它的原因。

Let us know how you go :)让我们知道你的去向:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM