Edit graph axis in Grapher via automation

To open a graph and alter an existing regular axis:

  1. Click here to download the BAS file: axis properties.BAS.
  2. Click Automation | Scripts | Run, select the BAS file from your downloads directory, and click Open.


  1. Copy the script below.
  2. Open Grapher and turn on the Script Manager by clicking View | Display | Script Manager.
  3. Press Ctrl+A to select all of the existing lines in the Script Manager and then press DELETE.
  4. Press Ctrl+V to paste it into the Script Manager.
  5. Click the Start/Resume icon () in the Script Manager.


Sub Main
'This script shows many of the methods and properties of an axis

'Declare objects
Dim GrapherApp, Plot, Graph, XAxis1, YAxis1, YAxis2, DupYAxis As Object

'Start Grapher
Set GrapherApp = CreateObject("Grapher.Application")

'Make the Grapher window visible
GrapherApp.Visible = True

'Create a new document window
Set Plot = GrapherApp.Documents.Add(grfPlotDoc)

'Create function plot graph named Graph1
Set Graph = Plot1.Shapes.AddFunctionPlotGraph(False,"cos(X)",,0,12)
Set LineScatterPlot1 = Graph.Plots.Item(1)

'Add an additional plot to the graph
Graph.AddLinePlot(GrapherApp.Path + "\samples\tutorial.dat",,3)
Set LineScatterPlot2 = Graph.Plots.Item(2)

'Change the properties of the second plot
LineScatterPlot2.line.foreColor = grfColorBlue

'Add an axis to the graph

'Move the new axis
Graph.Axes.Item(3).PositionAxis(grfPositionRightTop, "X Axis 1")

'Set variable names for the axes
Set XAxis1 = Graph.Axes.Item(1)
Set YAxis1 = Graph.Axes.Item(2)
Set YAxis2 = Graph.Axes.Item(3)

'Change the Length to 8 inches
XAxis1.length = 8

'Move the X Axis to the top of the Y Axis
XAxis1.PositionAxis(grfPositionRightTop, "Y Axis 1")

'Set the Minimum and Maximum X axis values
XAxis1.AutoMin = False
XAxis1.Min = 1
XAxis1.AutoMax = False
XAxis1.Max = 10

'Clip both plots to the x axis maximum
LineScatterPlot2.Clipping.XMaxMode = grfClipAxis
LineScatterPlot2.Clipping.DrawToLimits = True
LineScatterPlot1.Clipping.XMaxMode = grfClipAxis
LineScatterPlot1.Clipping.DrawToLimits = True

'Make the axis descending
XAxis1.Descending = True

'Set the scale
XAxis1.scale = grfAxisLog
XAxis1.AScale = grfAxisLinear

'Add an X Axis title
XAxis1.title.text = "X Axis 1"
XAxis1.title.Side = grfAxisTitleRightTop
XAxis1.title.yOffset = 0.2

'Move the Y Axis 2 to the right and up
YAxis2.xPos = 9
YAxis2.yPos = 4

'Set the grid lines for the X Axis 1
XAxis1.Grid.AtMajorTicks = True
XAxis1.Grid.MajorLine.style = ".1 in. Dash"
XAxis1.Grid.MajorLine.width = 0.03

'Show the tick marks on the right side
XAxis1.Tickmarks.MajorSide = grfTicksTopRight

'Set the TickMarks Spacing to 1
XAxis1.Tickmarks.MajorSpacingAuto = False
XAxis1.Tickmarks.MajorSpacing = 1

'Set the TickMarks MajorLength to 0.1 inches
XAxis1.Tickmarks.MajorLength = 0.1

'Set the TickMarks MajorLine color to blue
XAxis1.Tickmarks.MajorLineAuto = False
XAxis1.Tickmarks.MajorLine.foreColor = grfColorBlue

'Set the minor tick marks to the top and 5 divisions
XAxis1.Tickmarks.MinorSide = grfTicksTopRight
XAxis1.Tickmarks.MinorDivision = 5

'Set the TickLabels to show on the top side
XAxis1.TickLabels.MajorSide = grfTicksTopRight

'Set the TickLabels Frequency to 2 (every other)
XAxis1.TickLabels.MajorFreq = 2

'Set the TickLabels LabelDivisor to 10
XAxis1.TickLabels.LabelDivisor = 10

'Add an axis break with fixed scale
XAxis1.AxisBreak.on = True
XAxis1.AxisBreak.FixedScale = True

'Return the type of axis to the Debug window
Debug.Print "Type of axis " + XAxis1.axisType

'Delete the extra axis

 'Duplicate the Y axis 1 
Set DupYAxis = YAxis1.DuplicateAxis(True,False, False, "Duplicate Y Axis")

 'Set the axis linking properties 
DupYAxis.Link.ToAxis = "Y Axis 1"
DupYAxis.Link.scale = True
DupYAxis.Link.spacing = True
DupYAxis.Link.Limits = True
DupYAxis.Link.LimitsEquation = "X*2"
DupYAxis.Link.length = True
DupYAxis.Link.LengthScale = 0.5
DupYAxis.Link.xPos = True
DupYAxis.Link.XPosOffset = 8
DupYAxis.Link.yPos = True
DupYAxis.Link.YPosOffset = 0.0

 'Set the axis line properties 
XAxis1.line.width = 0.03

 'Move the axis labels 

 'Move Graph 

 'Reset position of manually-moved tick labels 
Set XAxis1 = Graph.Axes.Item(1)

 'Set graph title Offset 
Graph.title.xOffset = 0.05
Graph.title.yOffset = 0.15

End Sub


Updated August 2019

Was this article helpful?
0 out of 2 found this helpful



Please sign in to leave a comment.