How do I create a line/scatter plot in Grapher via automation?

This article contains a sample script that shows how to create a line/scatter plot in Grapher via automation.


To run this script:

  1. Click here to download the BAS file: LinePlot.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

'Declares Objects
Dim GrapherApp, Plot, Graph, LinePlot, SplineFit, LinePlot2 As Object

'Create an instance of Grapher named "GrapherApp"
Set GrapherApp = CreateObject("Grapher.Application")

'Make Grapher visible
GrapherApp.Visible = True

'Create a new plot document named "Plot"
Set Plot = GrapherApp.Documents.Add(grfPlotDoc)

'Create a new graph with a single line plot
Set Graph = Plot.Shapes.AddLinePlotGraph(GrapherApp.Path + "\Samples\bar chart orientations.dat")

'Set the line plot to the variable named "LinePlot"
Set LinePlot = Graph.Plots.Item(1)

'Set the X and Y columns
LinePlot.xCol = 1
LinePlot.yCol = 2

'Adjust properties of the X axis
Set XAxis1 = Graph.Axes.Item(1)
XAxis1.title.text = ""
XAxis1.AutoMin = False
XAxis1.Min = 1
XAxis1.Tickmarks.MajorSpacingAuto = False
XAxis1.Tickmarks.MajorSpacing = 5
XAxis1.Tickmarks.MinorSide = grfTicksBottomLeft
XAxis1.Tickmarks.MinorDivision = 5

'Adjust properties of the Y axis
Set YAxis1 = Graph.Axes.Item(2)
YAxis1.title.text = ""
YAxis1.Tickmarks.MinorDivision = 4
YAxis1.Grid.AtMinorTicks = True = "Dots"
YAxis1.Grid.MinorLine.foreColor = grfColorBlack30

'Add a spline fit
Set SplineFit = LinePlot.AddFit(grfSplineFit)
SplineFit.line.width = 0.02 = ".1 in. Dash"

'Add a second line plot to graph and name it "LinePlot2"
Set LinePlot2 = Graph.AddLinePlot(GrapherApp.Path + "\Samples\bar chart orientations.dat",1,3)

'Changes the line color
LinePlot2.line.foreColor = grfColorDustyPlum

'Changes some clipping properties
LinePlot.Clipping.XMinMode = grfClipCustom
LinePlot.Clipping.xMin = 6
LinePlot.Clipping.XMaxMode = grfClipAxis

'Reset the clipping to None
LinePlot.Clipping.XMinMode = grfClipNone
LinePlot.Clipping.XMaxMode = grfClipNone

'Display vertical error bars from a data file
LinePlot.ErrorBars.VertBarType = grfReadFromData
LinePlot.ErrorBars.VertBarCol = 3
LinePlot.ErrorBars.line.width = 0
LinePlot.ErrorBars.line.foreColor = grfColorBlack70

'Turn on the display of the labels at the vertical center of the symbol
LinePlot.Labels.ShowLabels = True
LinePlot.Labels.VAlign = grfAlignVCenter

'Display a symbol at every data point on the plot
LinePlot2.symbolFreq = 1

'Use a column to determine the symbol number
LinePlot2.SymbolCol = 3

'Rotate the symbols
LinePlot2.SymbolAngle = 45

'Turn off the symbol column
LinePlot2.SymbolCol = -1

'Change some symbol properties
LinePlot2.symbol.Index = 6
LinePlot2.symbol.size = 0.15
LinePlot2.symbol.Fill.foreColor = grfColorDustyPlum
LinePlot2.symbol.line.foreColor = grfColorWhite

'Get the symbol from a symbol table with repeating symbols
LinePlot2.UseSymbolTable = True
LinePlot2.LoadSymbolTable(GrapherApp.Path + "\Samples\SymbolTable.gst")
LinePlot2.RepeatSymbols = True

'Turn off the symbol table
LinePlot2.UseSymbolTable = False

'Change the line color
LinePlot.line.foreColor = grfColorNavyBlue

'Set the Fill pattern and color
LinePlot.Fill.foreColor = grfColorNavyBlue
LinePlot.Fill.ForeOpacity = 10

'Set the fill direction to fill above the line plot
LinePlot.FillDirection = grfFillUp

'Set a custom Fill Cutoff Value
LinePlot.FillCutoffAuto = False
LinePlot.FillCutoffValue = 20

'Changes the fill direction
LinePlot.FillDirection = grfFillDown

'Calculate area using baseline 20
Debug.Print "Area above baseline (20) = " + LinePlot2.Area(20)
Debug.Print "Area below baseline (20) = " + LinePlot2.NegArea(20)

'Change the line plot to a step plot

End Sub


Updated December 14, 2017

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request


Please sign in to leave a comment.