0

1)How I can to set color for each series in column chart as posted below? 2) do exist a way to set distance between columns and their width?

Below an sample of code

from pptx import Presentation 
from   pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches 
# create presentation with 1 slide ------ 
prs = Presentation() 
slide = prs.slides.add_slide(prs.slide_layouts[5]) # define chart data --------------------- 
chart_data = ChartData()
chart_data.categories = ['East', 'West', 'Midwest'] 
chart_data.add_series('Q1 Sales', (19.2, 21.4, 16.7)) 
chart_data.add_series('Q2 Sales', (22.3, 28.6, 15.2)) 
chart_data.add_series('Q3 Sales', (20.4, 26.3, 14.2))
graphic_frame = slide.shapes.add_chart( XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data ) 
chart = graphic_frame.chart
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
EugenS
  • 83
  • 11

1 Answers1

1

In PowerPoint, by default, the colors assigned to each series in a chart are the theme colors Accent 1 through Accent 6, in that order.

In general, the best strategy for determining chart colors is to change these theme accent colors in the "base" or "template" presentation you open with Presentation(...).

It is, however, possible to assign specific colors to individual data points (bar, line, pie segment, etc.) for at least some chart types.

from pptx import Presentation 
from pptx.chart.data import ChartData
from pptx.dml.color import RGBColor
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches 

prs = Presentation() 
slide = prs.slides.add_slide(prs.slide_layouts[5])
chart_data = ChartData()
chart_data.categories = ['East', 'West', 'Midwest'] 
chart_data.add_series('Q1 Sales', (19.2, 21.4, 16.7)) 
chart_data.add_series('Q2 Sales', (22.3, 28.6, 15.2)) 
chart_data.add_series('Q3 Sales', (20.4, 26.3, 14.2))
chart = slide.shapes.add_chart(
    XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
).chart 

points = chart.plots[0].series[0].points
for point in points:
    fill = point.format.fill
    fill.solid()
    fill.fore_color.rgb = RGBColor(255, 0, 0)

Column width in a column chart is set automatically to fill the available space.

Bar-type plots (including column-type, as here) have a .gap_width() method that allows the gap between bar "clusters" to be specified. There is also a .overlap() method that allows gap between individual bars in a cluster to be specified. This is really a second question, so I just provide the link here. If you want a fuller answer please post a separate question.
http://python-pptx.readthedocs.io/en/latest/api/chart.html#pptx.chart.plot.BarPlot.gap_width

scanny
  • 26,423
  • 5
  • 54
  • 80