Sales figures and margin percentages are a classic "same table, different scale" pairing: one is an absolute value in the thousands, the other a percentage around 0.2. Drop both into one column chart and the margin columns hug the baseline; drop both into one line chart and the sales line gets flattened. Excel's standard answer to this is the combo chart — columns for sales, a line for margin, with the line on its own secondary axis so each scale gets a side of the chart.
This article uses Spire.XLS for Python to start from a plain column chart and layer by layer turn it into a column-line combo with a percentage-formatted secondary axis. All code runs as-is; the sample documents are at the end.
The only requirement is one install:
pip install Spire.XLS
First, a Plain Column Chart
Prepare half-year data and create a clustered column chart the usual way:
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
sheet = workbook.Worksheets[0]
sheet.Range["A1"].Value = "Month"
sheet.Range["B1"].Value = "Sales"
sheet.Range["C1"].Value = "Margin %"
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [1200, 980, 1350, 1100, 1500, 1620]
margin = [0.15, 0.12, 0.18, 0.13, 0.19, 0.21]
for i in range(6):
sheet.Range[2 + i, 1].Value = months[i]
sheet.Range[2 + i, 2].NumberValue = sales[i]
sheet.Range[2 + i, 3].NumberValue = margin[i]
chart = sheet.Charts.Add(ExcelChartType.ColumnClustered)
chart.DataRange = sheet.Range["A1:C7"]
chart.SeriesDataFromRange = False
chart.LeftColumn = 1
chart.TopRow = 10
chart.RightColumn = 10
chart.BottomRow = 26
workbook.SaveToFile("ComboColumn.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
The chart now has two blue column series: Sales and Margin %. The problem is already visible in the data — the margin columns sit around 0.15, and on the same axis as sales in the thousands they end up visually flat against the zero line.
Turn the Second Series into a Line
In Spire, the chart type is not an exclusive chart-level property — it can be set per series. Series[i].SerieType changes only that one series; the others stay as they are:
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("ComboColumn.xlsx")
sheet = workbook.Worksheets[0]
chart = sheet.Charts[0]
# Give each series its own chart type
chart.Series[0].SerieType = ExcelChartType.ColumnClustered
chart.Series[1].SerieType = ExcelChartType.Line
chart.Series[1].Format.LineProperties.Color = Color.get_Red()
chart.ChartTitle = "Sales and Margin"
workbook.SaveToFile("ComboChart.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
The saved XML now contains two plot groups — <c:barChart> and <c:lineChart> — which is exactly what makes Excel treat the file as a combo chart. But the line still rides the primary value axis (the 0–1800 scale on the left), where 0.15 against 1800 flies nap-of-the-earth; the red line is even less readable than before.
Give the Line Its Own Axis
The real key is UsePrimaryAxis. Set it to False and the series moves to the secondary value axis, where Excel automatically generates a second scale on the right side of the chart:
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("ComboChart.xlsx")
sheet = workbook.Worksheets[0]
chart = sheet.Charts[0]
# Move the line series to the secondary value axis
line = chart.Series[1]
line.UsePrimaryAxis = False
# Format the secondary axis as percentage
sva = chart.SecondaryValueAxis
sva.NumberFormat = "0%"
sva.IsSourceLinked = False
sva.Title = "Margin"
workbook.CalculateAllValue()
workbook.SaveToFile("ComboSecondary.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
SecondaryValueAxis is a ready-made property on the chart object (there is a matching SecondaryCategoryAxis, though the category axis rarely needs touching). Two settings are worth noting: NumberFormat = "0%" turns the right-side scale from decimals like 0.05, 0.10 into percentages like 5%, 10%; and IsSourceLinked = False detaches the axis from the source cells' number format, otherwise that link would pull the format straight back from the source data.
sva.Title = "Margin" adds an axis title on the right. Finish with CalculateAllValue() so no unevaluated formulas remain in the workbook.
At this point the chart is the standard "columns + line + dual axis" shape: the left scale serves the sales columns, the percentage scale on the right serves the red line, and each series reads clearly on its own terms.
Read Back and Verify
After saving, the combo chart reads back property by property:
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("ComboSecondary.xlsx")
sheet = workbook.Worksheets[0]
chart = sheet.Charts[0]
for s in chart.Series:
print(s.Name, "| type:", s.SerieType, "| primary axis:", s.UsePrimaryAxis)
workbook.Dispose()
Output:
Sales | type: ExcelChartType.ColumnClustered | primary axis: True
Margin % | type: ExcelChartType.Line | primary axis: False
The read-back value of UsePrimaryAxis is the authoritative answer to which axis each series is on. For verification independent of Spire, unzip the xlsx and open xl/charts/chart1.xml: two <c:valAx> nodes correspond to the left and right value axes, the 0% formatCode appears on the secondary axis, and <c:barChart> and <c:lineChart> sit side by side — with those three in place, the combo chart is complete.
Wrapping Up
The "columns for quantity, line for rate" pattern is not just for sales: production volume with yield rate, ad spend with click-through rate, inventory with turnover days — wherever absolute values and ratios share a table, the same shape applies. The three supports in Spire are worth remembering: per-series SerieType, per-series UsePrimaryAxis, and the independent number format on SecondaryValueAxis. Each handles one layer, and all three are needed.


Top comments (0)