How To Set Axes Object and Axis Object Using xlwings?

Method

In Excel, the **Axis** object represents a single axis, and its plural form, the **Axes** object, represents multiple axes and the coordinate system they form. For a 2D coordinate system, there are horizontal and vertical axes; for a 3D coordinate system, there are axes in three directions.

To access the **Axis** object using the API, the syntax is as follows:

axs=cht.Axes(Type,AxisGroup)

Where `cht` is the **Chart** object, and the two parameters are:

– **Type** – A required parameter, with values 1, 2, or 3. When `Type = 1`, the axis displays categories (usually for the horizontal axis in a chart); when `Type = 2`, the axis displays values (usually for the vertical axis); when `Type = 3`, the axis displays data series (only used for 3D charts).

– **AxisGroup** – An optional parameter that specifies whether the axis is primary or secondary. If set to 2, it means the axis is a secondary axis. If set to 1, it means the axis is a primary axis.

You can use the **Chart** object’s **Axes** property to get the horizontal and vertical axes and set their properties. The **Border** property can be used to modify the axis’s color, line style, width, etc.

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

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

                 200,20,300,200,True).Chart      #Add chart

Example

Code

#Coordinate system - Create chart

import xlwings as xw
import os

root = os.getcwd()
app = xw.App(visible=True, add_book=False)
wb=app.books.open(root+r'/P1P2.xlsx',read_only=False)
sht=wb.sheets(1)

sht.api.Range('A1:B7').Select()      #Data
cht=sht.api.Shapes.AddChart2(-1,xw.constants.ChartType.xlColumnClustered,\
                 200,20,300,200,True).Chart      #Add chart

#wb.save()
#wb.close()
#app.kill()
March 31, 2026 (0)


Leave a Reply

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