pythonxlwings

How to create a combo / combination chart in xlwings?


I'm trying to create a combo chart in excel through xlwings and I'm not able to do it. In the documentation it says you can set the chart_type to 'combination' but when running the code:

wb = xw.Book()
ws = wb.sheets['Sheet1']

chart = ws.charts.add()
chart.set_source_data(ws.range('B3:E4'))
chart.chart_type = 'combination'

Where the range B3:E4 is just sample data. I get the following error:

self.xl.ChartType = chart_types_s2i[chart_type]

KeyError: 'combination'

Has anyone been able to successfully create a combo chart using xlwings that would be able to fix this code or provide sample code that worked for them instead?


Solution

  • 'combination' in the list may be misleading. You can see how this done using VBA, two chart types are combined. There is no valid chart type called 'combination'. (Looking a bit deeper at the xlwings code, chart type 'combination' might be valid for MACOS but not Windows).

    Create two series and add to the chart with Chart type for each series.
    In this example a clustered column chart (xlColumnClustered, type 51) is combined with a line chart (xlLine, type 4).
    This example uses the xlwings API so uses similar commands to VBA. The ChartType numbers are from the 'XlChartType enumeration' list. These are in the xlwings constants 'ChartType' class so you can import the constants and use the name instead if you wish.

    import xlwings as xw
    
    
    with xw.App(visible=False) as app:
        wb = xw.Book()
        wb.sheets.add()
        ws = wb.sheets[0]
    
        # Add the worksheet data to be plotted.
        data_list = [
            ['Salesman', 'Net Sales', 'Target'],
            ['Wilham', 2600, 3500],
            ['Simon', 11500, 14000],
            ['Frank', 13500, 15000],
            ['Nathan', 17000, 19500],
            ['Jason', 5500, 7000],
            ['Anthony', 10000, 13500],
        ]
       
        ### Add data to Sheet from cell A1
        ws.range('A1').value = data_list
    
        ### Create new chart object 'chart1' and set positioning on sheet
        chart1 = ws.charts.add(left=350, top=10, width=520, height=380)
    
        ### Add Chart Series 1 and set type
        chart1.api[1].SeriesCollection().NewSeries()
        chart1.api[1].FullSeriesCollection(1).ChartType = 51  # xlColumnClustered
    
        ### Series 1 Data
        series1_x = ws.range('A2').expand("down")
        series1_y = ws.range('B2').expand("down")
    
        ### Add Series 1 data to Chart
        chart1.api[1].SeriesCollection(1).XValues = series1_x.api
        chart1.api[1].SeriesCollection(1).Values = series1_y.api
        chart1.api[1].FullSeriesCollection(1).Name = "Column Chart"
    
        ### Add Chart Series 2 and set type
        chart1.api[1].SeriesCollection().NewSeries()
        chart1.api[1].FullSeriesCollection(2).ChartType = 4  # xlLine
    
        ### Series 2 Data
        series2_x = ws.range('A2').expand("down")
        series2_y = ws.range('C2').expand("down")
    
        ### Add Series 2 data to Chart
        chart1.api[1].SeriesCollection(2).XValues = series2_x.api
        chart1.api[1].SeriesCollection(2).Values = series2_y.api
        chart1.api[1].FullSeriesCollection(2).Name = "Line Chart"
    
        wb.save('xlwings_combo_chart.xlsx')
        wb.close()
    

    Output from the above code sample
    If you select 'Change Chart Type' in Excel it will be listed as a 'combo chart'.
    enter image description here