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, 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 12\Scripter.
  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.
     

*********

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 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.xPos = YAxis1.xPos
YAxis2.yPos = YAxis1.yPos
YAxis2.length = YAxis1.length
YAxis2.title.worksheet = DataFile$
YAxis2.title.text = "<<@a1>>"


'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.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
		.xPos = XAxis1.xPos
		.yPos = XAxis1.yPos
		.length = Tot_Width
		.AutoMin = False
		.Min = 0
		.AutoMax = False
		.Max = Num_Variables
		.Grid.AtMajorTicks = True
		.Grid.ParallelTo = YAxisMaster.Name
		.Tickmarks.MajorSpacing = 1
End With
'Set Master Y properties
With YAxisMaster
		.Tickmarks.MajorSide = grfTicksOff
		.Tickmarks.MinorSide = grfTicksOff
		.TickLabels.MajorOn = False
		.TickLabels.MinorOn = False
		.xPos = XAxis1.xPos
		.yPos = XAxis1.yPos-Tot_Width
		.length = Tot_Width
		.AutoMin = False
		.Min = 0
		.AutoMax = False
		.Max = Num_Variables
		.Grid.AtMajorTicks = True
		.Grid.ParallelTo = XAxisMaster.Name
		.Tickmarks.MajorSpacing = 1
End With

'Add the scatter plots
'i will increment row = y
'j will increment column = x
For i = 1 To Num_Variables
	For j = 1 To Num_Variables
			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 on the page
Graph1.left = 1
Graph1.top = 9

End Sub

 

Updated October 28, 2016

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.