How To Set Multiple Axes Chart Using xlwings?

【Method】

– Bind a series to the primary or secondary axis 

The **AxisGroup** property of the **Series** object is used to assign a series to either the primary axis (when the value is 1) or the secondary axis (when the value is 2).

– Set up the axes 

You can access the **Axis** object through the **Chart** object with the following syntax:

axs=cht.Axes(Type,AxisGroup)

Where `cht` is the **Chart** object. The **Type** parameter indicates the type of axis. A value of 1 means a category axis, while 2 means a value axis. The **AxisGroup** parameter specifies whether the axis is primary (1) or secondary (2). By default, the primary axis is displayed on the left, and the secondary axis is displayed on the right. This allows the creation of a dual-axis chart, where two charts are overlaid using the same horizontal axis and different vertical axes.

sht.api.Range(‘A1:B7’).Select()    #Data

cht=sht.api.Shapes.AddChart2(-1,xw.constants.ChartType.xlColumnClustered,\

                        20,20,350,200,True).Chart

cht.SeriesCollection(1).AxisGroup=1    #Y axis for series 1

cht.SeriesCollection(2).AxisGroup=2    #Y axis for series 2

cht.SeriesCollection(2).ChartType=xw.constants.ChartType.xlLine

cht.SeriesCollection(2).MarkerStyle=xw.constants.MarkerStyle.xlMarkerStyleTriangle

cht.SeriesCollection(2).MarkerForegroundColor=xw.utils.rgb_to_int((0,0,255))

cht.SeriesCollection(2).MarkerSize=8

cht.SeriesCollection(2).HasDataLabels=True

cht.SeriesCollection(1).HasDataLabels=True

 

axs1=cht.Axes(2,1)

axs1.MinimumScale=0

axs1.MaximumScale=60

axs1.HasTitle=True

axs1.AxisTitle.Text=’Y Axis 1′

 

axs2=cht.Axes(2,2)

axs2.MinimumScale=10

axs2.MaximumScale=160

axs2.HasTitle=True

axs2.AxisTitle.Text=’Y Axis 2′

 

【Example】

【Code】

#Coordinate system - Multi-axis chart

import xlwings as xw
import os

root=os.getcwd()
app=xw.App(visible=True,add_book=False)
wb=app.books.open('multiaxis.xlsx',read_only=False)
sht=wb.sheets('Sheet1')

sht.api.Range('A1:B7').Select()    #Data
cht=sht.api.Shapes.AddChart2(-1,xw.constants.ChartType.xlColumnClustered,\
                        20,20,350,200,True).Chart
cht.SeriesCollection(1).AxisGroup=1    #Y axis for series 1
cht.SeriesCollection(2).AxisGroup=2    #Y axis for series 2
cht.SeriesCollection(2).ChartType=xw.constants.ChartType.xlLine
cht.SeriesCollection(2).MarkerStyle=xw.constants.MarkerStyle.xlMarkerStyleTriangle
cht.SeriesCollection(2).MarkerForegroundColor=xw.utils.rgb_to_int((0,0,255))
cht.SeriesCollection(2).MarkerSize=8
cht.SeriesCollection(2).HasDataLabels=True
cht.SeriesCollection(1).HasDataLabels=True

axs1=cht.Axes(2,1)
axs1.MinimumScale=0
axs1.MaximumScale=60
axs1.HasTitle=True
axs1.AxisTitle.Text='Y Axis 1'

axs2=cht.Axes(2,2)
axs2.MinimumScale=10
axs2.MaximumScale=160
axs2.HasTitle=True
axs2.AxisTitle.Text='Y Axis 2'
  
cht.ChartTitle.Caption='Multi-axis Plot'

#wb.save()
#wb.close()
#app.kill()
April 4, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *