Search code examples
pythonexcelchartsxlwings

Assign axes to the chart in xlwings


By using the script below I can generate the chart but it assigns columns to abscissa. How can I assign the rows to it, i.e. x,y,z as abscissa and A,B,C as ordinates.

enter image description here

import xlwings as xw

wb = xw.Book(r"tests.xlsx")
ws = wb.sheets['Sheet1']

# adding a plot
cp = (10, 10)
ch = ws.charts.add(top=ws.range(cp).top, left=ws.range(cp).left, width=800, height=300)
ch.chart_type = 'line_markers'
ch.set_source_data(ws.range('A1:D3'))
# putting the chart title
ch.api[1].SetElement(2)
ch.api[1].ChartTitle.Text = 'Test' 

Solution

  • Using .api and Chart.PlotBy

    ch.api[0].Chart.PlotBy = 2
    

    where 2 corresponds to the constant xlColumns.

    Output:

    enter image description here