# Plot waves from different folders in different graphs with consistent colours and markers

This snippet assumes that:
(1) your data is organized in different folders within root
(2) wave names across different folder are the same
(3) you want to create different graphs, each of them containing data from different folders, with consistent markers and colours
(4) colours and markers are predefined within a wave in root

Here is an example: First, make some data:

NewDataFolder/O/S run1
Make/O/N=20 SiO2,MgO,K2O
SiO2=45+p+enoise(3)
MgO=15-p/2+enoise(2)
K2O=abs(p+enoise(1))
SetDataFolder root:
NewDataFolder/O/S run2
Make/O/N=20 SiO2,MgO,K2O
SiO2=45+p+enoise(3)
MgO=15-p/2+enoise(2)
K2O=abs(p+enoise(1))
SetDataFolder root:
NewDataFolder/O/S run3
Make/O/N=20 SiO2,MgO,K2O
SiO2=45+p+enoise(3)
MgO=15-p/2+enoise(2)
K2O=abs(p+enoise(1))
SetDataFolder root:

Next, you'll need to define a colour and marker wave:
Make/O/N=(3,4) MarkerWave
// set x DimLabels matching the folders from which to plot data
SetDimLabel 0, 0, run1, MarkerWave
SetDimLabel 0, 1, run2, MarkerWave
SetDimLabel 0, 2, run3, MarkerWave
// set y Dimlabels for colour and marker type
SetDimLabel 1, 0, R, MarkerWave
SetDimLabel 1, 1, G, MarkerWave
SetDimLabel 1, 2, B, MarkerWave
SetDimLabel 1, 3, Marker, MarkerWave
// fill in some values
MarkerWave[0][0]= {0,0,65535}
MarkerWave[0][1]= {0,65535,65535}
MarkerWave[0][2]= {65535,0,0}
MarkerWave[0][3]= {16,19,23}
Edit MarkerWave.ld

Next, use the following snippet....:

function PlotXYDataFromFolders(MarkerWave, GraphName, x, y)
wave/Z markerWave
string GraphName
string x, y

// make sure we are in Root and check if MarkerWave exists
SetDataFolder root:
if(!WaveExists(MarkerWave))
DoAlert 0, "No MakerWave!"
return 0
endif

// define some variables and strings
variable nFolders = DimSize(MarkerWave, 0)
variable i
string folder
string Traces, ThisTrace
string legendStr =""

// Check if graph exists, if yes kill it and make a new one
DoWindow/F \$GraphName
if(V_flag)
KillWindow \$GraphName
endif
Display/N=\$GraphName

// run through all folder listed in MarkerWave
for(i=0; i<nFolders; i+=1)

// get folder name from DimensionLabel of MarkerWave
folder = GetDimLabel(MarkerWave, 0, i)

// check if this folder exists, if not give error
if(!DataFolderExists(folder))
DoAlert 0, "No such folder"
printf "No folder %s\r", folder
return -1
endif

// folder exists: set the data folder
SetDatafolder \$"root:"+ folder

// check if waves x and y exist
wave/z xx = \$x
wave/z yy = \$y
if( !WaveExists(xx) || !WaveExists(yy) )
DoAlert 0, "WaveExists Error"
SetDataFolder root:
return -2
endif

// x and y exist: now append the waves
AppendToGraph yy vs xx

// get the name of the last trace added to graph
traces = TraceNameList("", ";", 1)
ThisTrace = StringFromList(i, traces)

// set mode to "Marker"
ModifyGraph mode(\$ThisTrace) = 3

// change the marker
ModifyGraph marker(\$ThisTrace) = MarkerWave[i][%Marker]

// change the colour
ModifyGraph rgb(\$ThisTrace) = (MarkerWave[i][%R], MarkerWave[i][%G], MarkerWave[i][%B])

// add sqequence to legend string
legendstr += "\\s("+ThisTrace+") " + folder +"\r"

SetDataFolder root:
endfor

// add axis labels
Label Bottom, x
Label Left, y

Legend/C/N=text0 legendStr

// add more formatting....
ModifyGraph useMrkStrokeRGB=1
ModifyGraph mirror = 2

return 1
end

....and execute:
PlotXYDataFromFolders(MarkerWave,"MgOvsSiO2", "SiO2", "MgO")
PlotXYDataFromFolders(MarkerWave,"K2OvsSiO2", "SiO2", "K2O")

Forum

Support

Gallery