Archive

How To Set Logarithmic Scale Chart Using xlwings?

【Method】

The **ScaleType** property of the **Axis** object returns or sets the scale type for the value axis, as shown in the table below. When the **ScaleType** property is set to `xw.constants.ScaleType.xlScaleLogarithmic`, the axis uses a logarithmic scale, allowing you to create a logarithmic scale chart.

Name

Value

Description

xlScaleLinear

-4132

Linear scale

xlScaleLogarithmic

-4133

Logarithmic scale

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

cht=sht.api.Shapes.AddChart().Chart

cht.Axes(2).ScaleType=xw.constants.ScaleType.xlScaleLogarithmic    #Logarithmic scale

cht.Axes(2).HasMinorGridlines=True

 

【Example】

【Code】

#Coordinate system - Logarithmic scale 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()
cht=sht.api.Shapes.AddChart().Chart
cht.Axes(2).ScaleType=xw.constants.ScaleType.xlScaleLogarithmic    #Logarithmic scale
cht.Axes(2).HasMinorGridlines=True

#wb.save()
#wb.close()
#app.kill()

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()

How To Set Gridlines Using xlwings?

【Method】

Gridlines are represented by the **Gridlines** object. You can use its **Border** or **Format** properties to set the gridline color, line style, width, and other attributes. The **MajorGridlines** and **MinorGridlines** properties of the **Axis** object return **Gridlines** objects for the major and minor gridlines, respectively. Before setting these properties, the **HasMajorGridlines** and/or **HasMinorGridlines** properties of the **Axis** object must be set to `True`.

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

cht=sht.api.Shapes.AddChart().Chart    #Add chart

axs=cht.Axes(1)    #Horizontal axis

axs2=cht.Axes(2)    #Vertical axis

axs.HasMajorGridlines=True    #Show major gridlines for horizontal axis

axs.MajorGridlines.Border.ColorIndex =3    #Red

axs.MajorGridlines.Border.LineStyle = xw.constants.LineStyle.xlDash    #Line style

axs2.HasMajorGridlines=True    #Show major gridlines for vertical axis

axs2.MajorGridlines.Border.ColorIndex = 3    #Red

axs2.MajorGridlines.Border.LineStyle = xw.constants.LineStyle.xlDash    #Line style

#Can also use the following code for setting

#axs.MajorGridlines.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0))

#axs.MajorGridlines.Format.Line.DashStyle=4

#axs2.MajorGridlines.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0))

#axs2.MajorGridlines.Format.Line.DashStyle=4

 

【Example】

【Code】

#Coordinate system - Gridlines

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.AddChart().Chart    #Add chart
axs=cht.Axes(1)    #Horizontal axis
axs2=cht.Axes(2)    #Vertical axis
axs.HasMajorGridlines=True    #Show major gridlines for horizontal axis
axs.MajorGridlines.Border.ColorIndex =3    #Red
axs.MajorGridlines.Border.LineStyle = xw.constants.LineStyle.xlDash    #Line style
axs2.HasMajorGridlines=True    #Show major gridlines for vertical axis
axs2.MajorGridlines.Border.ColorIndex = 3    #Red
axs2.MajorGridlines.Border.LineStyle = xw.constants.LineStyle.xlDash    #Line style
#Can also use the following code for setting
#axs.MajorGridlines.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0))
#axs.MajorGridlines.Format.Line.DashStyle=4
#axs2.MajorGridlines.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0))
#axs2.MajorGridlines.Format.Line.DashStyle=4

#wb.save()
#wb.close()
#app.kill()

How To Set Tick Labels Using xlwings?

【Method】

The text labels corresponding to the positions of the major tick marks on the axis are called **tick labels**. These labels annotate the values or categories corresponding to the major tick marks.

For a category axis, the text of the tick labels represents the names of the associated categories in the chart. By default, the tick labels for a category axis are numbers, which start from 1 and increase in order from left to right. The **TickLabelSpacing** property can be used to set how many categories are displayed before a tick label is shown.

For a value axis, the text labels correspond to the major unit, minimum scale, and maximum scale properties of the axis. To change the tick label text for a value axis, you need to modify the values of these properties.

The **TickLabels** property of the **Axis** object returns a **TickLabels** object that represents the tick labels on the axis. By using the properties and methods of the **TickLabels** object, you can set various properties of the tick labels, such as font, number format, display direction, offset, and alignment.

Use the **TickLabelPosition** property to specify the position of the tick labels on the axis. The values for this property are constants or values as listed in the table below:

Name

Value

Description

xlTickLabelPositionHigh

-4127

Top or right side of the chart

xlTickLabelPositionLow

-4134

Bottom or left side of the chart

xlTickLabelPositionNextToAxis

4

Next to the axis (where the axis is not at any side of the chart)

xlTickLabelPositionNone

-4142

No tick labels

The **TickLabelSpacing** property allows you to return or set the number of categories or data series between each tick label. This property is only applicable for category axes and series axes and can be set to a value between 1 and 31999.

When the **TickLabelSpacingIsAuto** property is set to `True`, Excel will automatically adjust the spacing of the tick labels.

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

cht=sht.api.Shapes.AddChart().Chart    #Add chart

axs=cht.Axes(1)    #Horizontal axis

axs2=cht.Axes(2)    #Vertical axis

tl=axs2.TickLabels    #Vertical axis tick labels

tl.NumberFormat = ‘0.00’    #Number format

axs2.TickLabelPosition=xw.constants.Constants.xlHigh

 

【Example】

【Code】

#Axis - Tick labels

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.AddChart().Chart    #Add chart
axs=cht.Axes(1)    #Horizontal axis
axs2=cht.Axes(2)    #Vertical axis
tl=axs2.TickLabels    #Vertical axis tick labels
tl.NumberFormat = '0.00'    #Number format

axs2.TickLabelPosition=xw.constants.Constants.xlHigh

#wb.save()
#wb.close()
#app.kill()

How To Set Tick Marks Using xlwings?

【Method】

Tick marks are short lines on the axis used to help determine the position of data points on the chart. There are major and minor tick marks. Use the **MajorTickMark** and **MinorTickMark** properties of the **Axis** object to set the major and minor tick marks. 

The possible values for **MajorTickMark** and **MinorTickMark** are as follows:

Name

Value

Description

xlTickMarkCross

4

Across the axis

xlTickMarkInside

2

Inside the axis

xlTickMarkNone

-4142

No marker

xlTickMarkOutside

3

Outside the axis

Use the **TickMarkSpacing** property to return or set the spacing of major tick marks, indicating how often a major tick mark is displayed for every set of data points. This is only applicable for category axes and series axes and can be a value between 1 and 31999.

Use the **MajorUnit** and **MinorUnit** properties to set the units for the major and minor tick marks on the value axis.

Set **MajorUnitIsAuto** and **MinorUnitIsAuto** properties to `True` to have Excel automatically calculate the major and minor tick units for the value axis.

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

cht=sht.api.Shapes.AddChart().Chart    #Add chart

axs=cht.Axes(1)    #Horizontal axis

axs2=cht.Axes(2)    #Vertical axis

axs.MajorTickMark = 4

axs.MinorTickMark = 2

axs.TickMarkSpacing = 1

axs2.MajorUnit = 40

axs2.MinorUnit = 10

axs2.MajorUnitIsAuto=True

axs2.MinorUnitIsAuto=True

 

【Example】

【Code】

#Axis - Major tick marks

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.AddChart().Chart    #Add chart
axs=cht.Axes(1)    #Horizontal axis
axs2=cht.Axes(2)    #Vertical axis
axs.MajorTickMark = 4
axs.MinorTickMark = 2
axs.TickMarkSpacing = 1
axs2.MinorTickMark = 2
axs2.MajorUnit = 40
axs2.MinorUnit = 10
#axs2.MajorUnitIsAuto=True
#axs2.MinorUnitIsAuto=True

#wb.save()
#wb.close()
#app.kill()

How To Set Value Range of Value Axis Using xlwings?

【Method】

For the vertical axis (which is a value axis), use the **MinimumScale** and **MaximumScale** properties of the vertical axis object to set the minimum and maximum values for the value axis.

 

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

cht=sht.api.Shapes.AddChart().Chart    #Add chart

axs=cht.Axes(1)    #Horizontal axis

axs2=cht.Axes(2)    #Vertical axis

axs.HasTitle=True    #Horizontal axis has title

axs.AxisTitle.Caption=’X Axis Title’    #Title text

axs.AxisTitle.Font.Italic=True    #Italic font

axs.AxisTitle.Font.Color=xw.utils.rgb_to_int((255,0,0))    #Red

axs2.HasTitle=True    #Vertical axis has title

axs2.AxisTitle.Caption=’Y Axis Title’    #Title text

axs2.AxisTitle.Font.Bold=True    #Bold font

axs2.MinimumScale=10

axs2.MaximumScale=200

 

【Example】

【Code】

#Axis - Value Axis Range

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.AddChart().Chart    #Add chart
axs=cht.Axes(1)    #Horizontal axis
axs2=cht.Axes(2)    #Vertical axis
axs.HasTitle=True    #Horizontal axis has title
axs.AxisTitle.Caption='X Axis Title'    #Title text
axs.AxisTitle.Font.Italic=True    #Italic font
axs.AxisTitle.Font.Color=xw.utils.rgb_to_int((255,0,0))    #Red
axs2.HasTitle=True    #Vertical axis has title
axs2.AxisTitle.Caption='Y Axis Title'    #Title text
axs2.AxisTitle.Font.Bold=True    #Bold font
axs2.MinimumScale=10
axs2.MaximumScale=200

#wb.save()
#wb.close()
#app.kill()

How To Set Axis Title Using xlwings?

【Method】

Use the **HasTitle** property of the **Axis** object to set whether the axis title is displayed, and the **AxisTitle** property to set the text content of the axis title. Note that the **HasTitle** property must be set to `True` before the **AxisTitle** property can be set. The **AxisTitle** property returns an **AxisTitle** object, which you can use to set the title text and font for the axis.

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

cht=sht.api.Shapes.AddChart().Chart    #Add chart

axs=cht.Axes(1)    #Horizontal axis

axs2=cht.Axes(2)    #Vertical axis

axs.HasTitle=True    #Horizontal axis has title

axs.AxisTitle.Caption=’X Axis Title’    #Title text

axs.AxisTitle.Font.Italic=True    #Italic font

axs.AxisTitle.Font.Color=xw.utils.rgb_to_int((255,0,0))    #Red

axs2.HasTitle=True    #Vertical axis has title

axs2.AxisTitle.Caption=’Y Axis Title’    #Title text

axs2.AxisTitle.Font.Bold=True    #Bold font

 

【Example】

【Code】

#Axis Titles

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.AddChart().Chart    #Add chart
axs=cht.Axes(1)    #Horizontal axis
axs2=cht.Axes(2)    #Vertical axis
axs.HasTitle=True    #Horizontal axis has title
axs.AxisTitle.Caption='X Axis Title'    #Title text
axs.AxisTitle.Font.Italic=True    #Italic font
axs.AxisTitle.Font.Color=xw.utils.rgb_to_int((255,0,0))    #Red
axs2.HasTitle=True    #Vertical axis has title
axs2.AxisTitle.Caption='Y Axis Title'    #Title text
axs2.AxisTitle.Font.Bold=True    #Bold font

#wb.save()
#wb.close()
#app.kill()

How To Set Axis Using xlwings?

【Method】

The Border property returns the axis’s border properties.

– ColorIndex property

– Weight property

– DashStyle property

 

sht.api.Range(“A1:B7”).Select()    #Data

cht=sht.api.Shapes.AddChart().Chart    #Add chart

axs=cht.Axes(1)    #Horizontal axis

axs.Border.ColorIndex=3    #Red

axs.Border.Weight=3    #Line width

axs.HasMinorGridlines=True    #Show minor gridlines

axs2=cht.Axes(2)    #Vertical axis

axs2.Border.Color=xw.utils.rgb_to_int((0,0,255))    #Blue

axs2.Border.Weight=3    #Line width

axs2.HasMinorGridlines=True    #Show minor gridlines

 

【Example】

【Code】

#Axis Settings

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.AddChart().Chart    #Add chart
axs=cht.Axes(1)    #Horizontal axis
axs.Border.ColorIndex=3    #Red
axs.Border.Weight=3    #Line width
axs.HasMinorGridlines=True    #Show minor gridlines
axs2=cht.Axes(2)    #Vertical axis
axs2.Border.Color=xw.utils.rgb_to_int((0,0,255))    #Blue
axs2.Border.Weight=3    #Line width
axs2.HasMinorGridlines=True    #Show minor gridlines

#wb.save()
#wb.close()
#app.kill()

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()

How To Set Custom Styles Using xlwings?

【Example】

【Code】

import xlwings as xw
import os

def set_style(cht):
    cht.ChartArea.Format.Line.Visible=False
    cht.PlotArea.Format.Fill.Visible=False
    cht.PlotArea.Format.Line.Visible=True
    cht.PlotArea.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((200,200,200))
    #cht.PlotArea.Format.Line.ForeColor.ObjectThemeColor = msoThemeColorText1
    ax1=cht.Axes(1)
    ax2=cht.Axes(2)
    ax1.HasTitle=True
    ax1.AxisTitle.Text='Categories'
    ax1.AxisTitle.Font.Size=10
    ax1.TickLabels.Font.Size=8
    #ax1.TickLabels.NumberFormat='0.00'
    ax1.HasMajorGridlines=True
    ax2.HasTitle=True
    ax2.AxisTitle.Text='Values'
    ax2.AxisTitle.Font.Size=10
    ax2.TickLabels.Font.Size=8
    ax2.HasMajorGridlines=True
    cht.HasTitle=True
    #cht.ChartTitle.Caption='Plot'
    #cht.ChartTitle.Font.Size=12

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

sht.api.Range('A2:C8').Select()  #数据
shp=sht.api.Shapes.AddChart2(-1,xw.constants.ChartType.xlColumnClustered,20,20,350,250,True)
cht=shp.Chart  #添加图表
cht.SeriesCollection(1).ChartType=xw.constants.ChartType.xlLine

set_style(cht)

cht.Export(root+'/cht.jpg')
cht.Export(root+'/cht.svg')
cht.ExportAsFixedFormat(0,root+'/cht.pdf')

#wb.save()
#app.kill()