DEV Community

Allen Yang
Allen Yang

Posted on

Configuring Chart Axes and Gridlines in Excel Using Python

How readable a chart is depends largely on its axes: whether the value range fits the data, whether the tick interval is sensible, and whether the axes explain their units. The defaults draw a chart, but not always a clear one — when the values sit between 3 and 5, a default scale of 0 to 6 flattens the differences.

Axes and gridlines are structural properties of the chart and can be set in the same script that generates it. This article uses Spire.XLS for Python to add axis titles, fix the value axis range and tick interval, control tick marks and label positions, and change or hide the gridlines.

Setting Up the Environment

Install Spire.XLS and import the modules:

pip install Spire.XLS
Enter fullscreen mode Exit fullscreen mode
from spire.xls import *
from spire.xls.common import *
Enter fullscreen mode Exit fullscreen mode

Creating the Base Chart

Start with planned and actual monthly figures and build a clustered column chart; every setting below applies to this chart:

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
workbook.CreateEmptySheets(1)
sheet = workbook.Worksheets[0]

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
planned = [3.3, 2.5, 2.0, 3.7, 4.5, 4.0]
actual = [3.8, 3.2, 1.7, 3.5, 4.5, 4.3]

sheet.Range["A1"].Value = "Month"
sheet.Range["B1"].Value = "Planned"
sheet.Range["C1"].Value = "Actual"
for i in range(len(months)):
    sheet.Range[i + 2, 1].Value = months[i]
    sheet.Range[i + 2, 2].NumberValue = planned[i]
    sheet.Range[i + 2, 3].NumberValue = actual[i]

chart = sheet.Charts.Add(ExcelChartType.ColumnClustered)
chart.DataRange = sheet.Range["B1:C7"]
chart.SeriesDataFromRange = False
chart.ChartTitle = "Monthly Results"
for series in chart.Series:
    series.CategoryLabels = sheet.Range["A2:A7"]

chart.TopRow = 10
chart.BottomRow = 28
chart.LeftColumn = 1
chart.RightColumn = 10

workbook.SaveToFile("BaseChart.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

The two axes are reached through PrimaryCategoryAxis (the category axis) and PrimaryValueAxis (the value axis).

Adding Axis Titles

Axis titles explain what each axis means and in which unit. Set the titles, then reduce the font size on both axes:

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
workbook.LoadFromFile("BaseChart.xlsx")
sheet = workbook.Worksheets[0]
chart = sheet.Charts[0]

# Axis titles and font sizes
chart.PrimaryCategoryAxis.Title = "Month"
chart.PrimaryValueAxis.Title = "Sales (thousand)"
chart.PrimaryCategoryAxis.Font.Size = 11
chart.PrimaryValueAxis.Font.Size = 11

workbook.SaveToFile("AxisTitles.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

The value axis title is rotated 90 degrees along the left side, and the category axis title sits horizontally below the chart; both are saved with the file.

Fixing the Value Axis Range and Tick Interval

By default Excel calculates the value axis range. When the data sits in a narrow band, fixing the range spreads the differences out:

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
workbook.LoadFromFile("BaseChart.xlsx")
sheet = workbook.Worksheets[0]
chart = sheet.Charts[0]

# Fix the value axis range and tick interval
axis = chart.PrimaryValueAxis
axis.MaxValue = 5.0
axis.MinValue = 0.0
axis.MajorUnit = 1.0
axis.MinorUnit = 0.5
axis.NumberFormat = "0.0"
axis.IsSourceLinked = False

workbook.SaveToFile("AxisScale.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

MaxValue and MinValue pin the ends of the axis; MajorUnit and MinorUnit set the major and minor tick intervals. NumberFormat formats the tick labels — 0.0 shows one decimal on every tick — and IsSourceLinked must be switched to False afterwards, or the format keeps following the source cells.

Controlling Tick Marks and Label Positions

Tick marks and tick labels are controlled by two sets of enums:

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
workbook.LoadFromFile("BaseChart.xlsx")
sheet = workbook.Worksheets[0]
chart = sheet.Charts[0]

# Tick marks and label position
axis = chart.PrimaryValueAxis
axis.MajorTickMark = TickMarkType.TickMarkOutside
axis.MinorTickMark = TickMarkType.TickMarkInside
axis.MinorUnit = 0.5
axis.TickLabelPosition = TickLabelPositionType.TickLabelPositionNextToAxis
axis.CrossesAt = 0

workbook.SaveToFile("AxisTicks.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

MajorTickMark and MinorTickMark decide whether the tick lines are drawn outside, inside, or not at all (TickMarkOutside / TickMarkInside / TickMarkNone); minor ticks only appear at an interval once MinorUnit is set. TickLabelPosition chooses which side of the axis the labels sit on, and CrossesAt sets where the category axis crosses the value axis — moving it to the bottom of the chart makes data with negative values easier to read.

Changing and Hiding Gridlines

Gridlines belong to the value axis' MajorGridLines; they can be recoloured or switched off entirely:

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
workbook.LoadFromFile("BaseChart.xlsx")
sheet = workbook.Worksheets[0]
chart = sheet.Charts[0]

# Recolour the major gridlines of the value axis
gridlines = chart.PrimaryValueAxis.MajorGridLines
gridlines.LineProperties.Color = Color.get_Red()

workbook.SaveToFile("GridlinesStyled.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

For a cleaner layout, set HasMajorGridLines to False:

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
workbook.LoadFromFile("BaseChart.xlsx")
sheet = workbook.Worksheets[0]
chart = sheet.Charts[0]

# Hide the major gridlines of the value axis
chart.PrimaryValueAxis.HasMajorGridLines = False

workbook.SaveToFile("GridlinesHidden.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

The category axis usually needs no gridlines; both snippets also work on PrimaryCategoryAxis.MajorGridLines.

Practical Tips

  • Two axes, two objects: PrimaryCategoryAxis is the category axis and PrimaryValueAxis the value axis, each with its own properties.
  • Fix the range before the format: set MaxValue, MinValue, and MajorUnit together so the NumberFormat of the tick labels has a stable width.
  • Unlink before formatting: a custom NumberFormat only takes effect after IsSourceLinked = False.
  • Pair minor ticks with a unit: MinorTickMark appears at intervals only once MinorUnit is set.
  • Gridlines belong to the value axis: the horizontal gridlines are controlled by PrimaryValueAxis.MajorGridLines.
  • Release resources: call Dispose() on each workbook when done.

Conclusion

This article covered configuring chart axes and gridlines in Excel from Python: adding titles and font sizes with Title and Font; fixing the value axis range and ticks with MaxValue, MinValue, MajorUnit, MinorUnit, and NumberFormat; controlling tick marks and labels with MajorTickMark, MinorTickMark, TickLabelPosition, and CrossesAt; and recolouring the gridlines with MajorGridLines.LineProperties.Color or hiding them with HasMajorGridLines = False. These settings are independent of the data, so a report script keeps its charts readable at any data scale.

Top comments (0)