Color Specific Bar Chart Differently in Python PPTX

萝らか妹 提交于 2019-12-20 02:36:29

问题


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)

回答1:


I haven't tested this, but I believe it will work:

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 :)



来源:https://stackoverflow.com/questions/42181540/color-specific-bar-chart-differently-in-python-pptx

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