Follow

How can I write a Grapher script to plot a scatter plot matrix from a user-defined data file?

This article contains a sample script for creating a scatter plot matrix from a data file. Prompts for worksheet, number of variables, first and last rows, and total graph size.

To run this script:

  1. Copy the script below, or click here to download the BAS file: Scatter Plot Matrix.BAS
  2. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\Grapher 13.
  3. Double click on Scripter.exe to launch Scripter.
  4. Press Ctrl+A to select all of the existing lines then press Delete.
  5. If you copied this script, press Ctrl+V to paste it into Scripter. If you downloaded it, click File | Open, select the BAS file from your downloads directory, and click Open.
  6. Click Script | Run to run the script.

OR:

  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

'Create Grapher as an Object
Dim Grapher As Object

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

'Make Grapher visible
Grapher.Visible = True

'Create a new document window
Set Plot1 = Grapher.Documents.Add(grfPlotDoc)

'Set page size
Plot1.PageSetup.pageSize = grfLetter
Plot1.PageSetup.Orientation = grfLandscape
pageheight = 8.5
pagewidth = 11

'Set the page units to inches
Plot1.Windows.Item(1).PageUnits(grfInches)

'Prompts for data file, columns, number of matrix columns, rows, etc.
DataFile$ = GetFilePath$()
Num_Variables = CLng(InputBox("How many variables are there?"))
First_Row = CLng(InputBox("What is the first row of data?"))
Last_Row = CLng(InputBox("What is the last row of data?"))
Tot_Width = CLng(InputBox("How big should the matrix be in inches?"))

'Create initial histogram graph
Set Graph1 = Plot1.Shapes.AddHistogramGraph(DataFile$,1)
Set Histogram1 = Graph1.Plots.Item(1)
'Set histogram properties
Histogram1.AutoLastRow = False
Histogram1.LastRow = Last_Row
Histogram1.AutoFirstRow = False
Histogram1.FirstRow = First_Row

'Set-up the initial axes
Set YAxis1 = Graph1.Axes.Item(2)
Set XAxis1 = Graph1.Axes.Item(1)
YAxis1.Link.ToAxis = "X Axis 1"
YAxis1.Link.xPos = True
XAxis1.Link.ToAxis = "Y Axis 1"
XAxis1.Link.yPos = True
XAxis1.Link.YPosOffset = Tot_Width/Num_Variables
XAxis1.length = Tot_Width/Num_Variables
YAxis1.length = Tot_Width/Num_Variables
XAxis1.Tickmarks.MajorSide = grfTicksTopRight
XAxis1.Tickmarks.MinorSide = grfTicksNone
XAxis1.TickLabels.MajorSide = grfTicksTopRight
XAxis1.TickLabels.MinorOn = False
XAxis1.title.Side = grfAxisTitleRightTop
XAxis1.title.worksheet = DataFile$
XAxis1.title.text = "<<@a1>>"
YAxis1.Visible = False

'Add X axes to the graph
For i = 2 To Num_Variables
	Set XAxis = Graph1.AddAxis(grfXAxis)
	XAxis.length = Tot_Width/Num_Variables
	XAxis.Link.ToAxis = "X Axis 1"
	XAxis.Link.xPos = True
	XAxis.Link.XPosOffset = Tot_Width/Num_Variables*(i-1)
	XAxis.Link.yPos = True
	XAxis.Link.YPosOffset = 0
	XAxis.Link.Limits = False
	XAxis.AutoMin = True
	XAxis.AutoMax = True
	XAxis.Tickmarks.MajorSide = grfTicksTopRight
	XAxis.Tickmarks.MinorSide = grfTicksNone
	XAxis.TickLabels.MajorSide = grfTicksTopRight
	XAxis.TickLabels.MinorOn = False
	XAxis.title.Side = grfAxisTitleRightTop
	XAxis.title.worksheet = DataFile$
	'Set the title from the cell - converting the i to a column letter with Chr(i+64)
	XAxis.title.text = "<<@"+Chr(i+64)+"1>>"
	ConvertToLetter = ""
Next i

'Add first Y axis to the graph
Set YAxis2 = Graph1.AddAxis(grfYAxis)
YAxis2.Link.xPos = True
YAxis2.Link.XPosOffset = 0
YAxis2.Link.ToAxis = "Y Axis 1"
YAxis2.Link.yPos = True
YAxis2.Link.YPosOffset = 0
YAxis2.Link.length = True
YAxis2.length = 1.66667
YAxis2.title.worksheet = DataFile$
YAxis2.title.text = "<<@a1>>"
YAxis2.Grid.AtMajorTicks = False

'Add each additional Y axis to the graph
For i = 2 To Num_Variables
	Set YAxis = Graph1.AddAxis(grfYAxis)
	YAxis.length = Tot_Width/Num_Variables
	YAxis.Link.ToAxis = "Y Axis 2"
	YAxis.Link.xPos = True
	YAxis.Link.XPosOffset = 0
	YAxis.Link.yPos = True
	YAxis.Link.YPosOffset = -1*Tot_Width/Num_Variables*(i-1)
	YAxis.Link.Limits = False
	YAxis.AutoMin = True
	YAxis.AutoMax = True
	YAxis.Grid.AtMajorTicks = False
	YAxis.title.worksheet = DataFile$
	'Set the title from the cell - converting the i to a column letter with Chr(i+64)
	YAxis.title.text = "<<@"+Chr(i+64)+"1>>"
	ConvertToLetter = ""
Next i

'Add the master axes
Set XAxisMaster = Graph1.AddAxis(grfXAxis)
Set YAxisMaster = Graph1.AddAxis(grfYAxis)
'Set Master X properties
With XAxisMaster
		.Tickmarks.MajorSide = grfTicksOff
		.Tickmarks.MinorSide = grfTicksOff
		.TickLabels.MajorOn = False
		.TickLabels.MinorOn = False
		.length = Tot_Width
		.AutoMin = False
		.Min = 0
		.AutoMax = False
		.Max = Num_Variables
		.Grid.AtMajorTicks = True
		.Grid.ParallelTo = YAxisMaster.Name
		.Tickmarks.MajorSpacing = 1
		.Link.ToAxis = "X Axis 1"
		.Link.yPos = True
		.Link.YPosOffset = Tot_Width*-1
		.Link.xPos = True
		.Link.XPosOffset = 0
End With
Set YAxis5 = Graph1.Axes.Item(8)
'Set Master Y properties
With YAxisMaster
		.Tickmarks.MajorSide = grfTicksOff
		.Tickmarks.MinorSide = grfTicksOff
		.TickLabels.MajorOn = False
		.TickLabels.MinorOn = False
		.length = Tot_Width
		.AutoMin = False
		.Min = 0
		.AutoMax = False
		.Max = Num_Variables
		.Grid.AtMajorTicks = True
		.Grid.ParallelTo = XAxisMaster.Name
		.Tickmarks.MajorSpacing = 1
		controllingaxis = "Y Axis"+Str(Num_Variables+1)
		.Link.ToAxis = Str(controllingaxis)
		.Link.yPos = True
		.Link.YPosOffset = 0
		.Link.xPos = True
		.Link.XPosOffset = 0
End With

'Add the scatter plots
For i = 1 To Num_Variables 'i increments row = y
	For j = 1 To Num_Variables 'j increments column = x
			If i <> j Then
				XAxisName = "X Axis" + Str$(j)
				YAxisName = "Y Axis" + Str$(i+1)
				Set LineScatterPlot1 = Graph1.AddLinePlot(DataFile$,j,i,XAxisName,YAxisName)
				LineScatterPlot1.symbolFreq = 1
				LineScatterPlot1.line.style = "Invisible"
				LineScatterPlot1.AutoLastRow = False
				LineScatterPlot1.LastRow = Last_Row
				LineScatterPlot1.AutoFirstRow = False
				LineScatterPlot1.FirstRow = First_Row
			End If
	Next j
Next i

'Add the other histograms
For i = 2 To Num_Variables
	Set YAxisHist = Graph1.AddAxis(grfYAxis)
	YAxisHist.length = Tot_Width/Num_Variables
	YAxisHist.Link.ToAxis = "Y Axis 2"
	YAxisHist.Link.xPos = True
	YAxisHist.Link.XPosOffset = 0
	YAxisHist.Link.yPos = True
	YAxisHist.Link.YPosOffset = -1*Tot_Width/Num_Variables*(i-1)
	YAxisHist.Visible = False
	XAxisName = "X Axis"+ Str$(i)
	Set Histogram2 = Graph1.AddHistogram(DataFile$,i,XAxisName,YAxisHist.Name)
	Histogram2.AutoLastRow = False
	Histogram2.LastRow = Last_Row
	Histogram2.AutoFirstRow = False
	Histogram2.FirstRow = First_Row
	Histogram2.ResetToDefault
Next

'Position the graph and turn off the title
Graph1.left = pagewidth/2-(Graph1.width/2)
Graph1.top = pageheight/2+(Graph1.width/2)
Graph1.title.text = ""

End Sub

 

Updated September 24, 2018

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

0 Comments

Please sign in to leave a comment.