# Stacked plots / Multiple plots graph

This snippet takes a list of y-Waves, x-Waves and plots them in a selectable number of columns in one graph. If the number of plots exceeds the number of columns, plots will be placed into additional rows.

Not extensively tested, but it might be useful for one or the other. To test it:

make/D/O/N=1000 aa, bb, cc, dd
setscale/i x, 0, 3*pi, "", aa,bb, cc, dd
aa=sin(x)
bb=cos(x)
cc= tan(x)
dd=abs(sin(x))

Then use:

function MakeStackedGraph(yWaveList, xWaveList, nCols, spacing, GraphName, mirror)
string yWaveList    // semicolon separated list containing wave names to be plotted
string xWaveList    // semicolon separated list containg corresonding x data; if list items are empty, y-waves will be plotted against x-scaling
variable nCols      // number of columns within the stacked graph
variable spacing    // spacing between plots in terms of fraction of total plot area
string GraphName    // name of the stacked graph
variable mirror     // mirror axis on = 1, or off = 0

variable nGraphs
variable nRows
variable nGaps
variable yLength
variable xLength
variable y0, x0, y1, x1
variable i, j, n = 0

string yWave, xWave
string yAxisName, xAxisName, yMirrorName, xMirrorName

// how many graphs are there and how many rows are required
nGraphs = ItemsInList(yWaveList)
nRows = ceil(nGraphs / nCols)

// calculate length of axis from given spacing
nGaps = nCols - 1
xLength = (1 - spacing * nGaps) / nCols

nGaps = nRows - 1
yLength = (1 - spacing * nGaps) / nRows

// Display empty window; kill if it exists
DoWindow/F \$GraphName
if(V_flag)
KillWindow \$GraphName
endif
Display/K=1 /N= \$Graphname

// append traces
for(i=0; i<nRows; i+=1)

// reset vertical axis position
y0 = 0
x1 = 0

for (j=0; j<    nCols; j+=1)

// get wave names from lists
yWave = StringFromList(n, yWaveList)
yAxisName = "yAxis" + num2str(n)
xWave = StringFromList(n, xWaveList)
xAxisName = "xAxis" + num2str(n)

if(strlen(xWave) == 0)
// if x-string is empty; plot against wave scaling
AppendToGraph/L=\$yAxisName /B=\$xAxisName \$yWave
else
AppendToGraph/L=\$yAxisName /B=\$xAxisName \$yWave vs \$xWave
endif

// set lower left position of y and x axis
ModifyGraph freePos(\$yAxisName)={y0,kwFraction}
ModifyGraph freePos(\$xAxisName)={x0,kwFraction}

// set length of the axis
ModifyGraph axisEnab(\$yAxisName)={y1,(y1+yLength)}
ModifyGraph axisEnab(\$xAxisName)={x1,(x1+xLength)}

// do some formatting
ModifyGraph tick(\$yAxisName) = 2, tick(\$xAxisName) = 2
ModifyGraph btlen(\$yAxisName) = 4, btlen(\$xAxisName) = 4

// append mirror axis
if(mirror)
yMirrorName = "yMirror" + num2str(n)
NewFreeAxis/L \$yMirrorName
ModifyFreeAxis \$yMirrorName master = \$yAxisName
ModifyGraph freePos(\$yMirrorName) ={(y0+xLength), kwFraction}
ModifyGraph axisEnab(\$yMirrorName) ={y1, (y1+ylength)}
ModifyGraph noLabel(\$yMirrorName)=2
ModifyGraph tick(\$yMirrorName) = 0, btlen(\$yMirrorName) = 4

xMirrorName = "xMirror" + num2str(n)
NewFreeAxis/B \$xMirrorName
ModifyFreeAxis \$xMirrorName master = \$xAxisName
ModifyGraph freePos(\$xMirrorName) ={(x0+yLength), kwFraction}
ModifyGraph axisEnab(\$xMirrorName) ={x1, (x1+xlength)}
ModifyGraph noLabel(\$xMirrorName)=2
ModifyGraph tick(\$xMirrorName) = 0, btlen(\$xMirrorName) = 4
endif

// shift next starting positions to the right
y0 += xLength + spacing
x1 += xlength + spacing

// increase plot counter
n += 1
endfor

// shift next starting positions up
x0 += yLength + spacing
y1 += yLength + spacing
endfor

return 1
end

and execute e.g.:

MakeStackedGraph("aa;bb;cc;dd;", ";;;;", 2, 0.1, "Multi", 1)

This uses the wave scaling as values for the x-axis. Alternatively use e.g. the following to create X-Y plots:

MakeStackedGraph("aa;bb;cc;dd;", "aa;aa;aa;aa;", 1, 0.05, "Multi", 0) Forum Support Gallery