Subscribe Bookmark RSS Feed

Using a For Loop with X by Y plot

scottdprm

Community Trekker

Joined:

Jul 1, 2016

I am trying to recreate the script below using a loop (this script works, but I would like it to run as a loop so that it treats each data set the same way):

 

bivariate(
Y( :name( "1st Thickness PST" ) ),
 X( :site ),
By( :Lot ),
Fit Where( :Slot_id == 1, Fit Each Value( {Line Color( {213, 72, 87} )} ) ),
Fit Where( :Slot_id == 2, Fit Each Value( {Line Color( {57, 177, 67} )} ) ),
Fit Where( :Slot_id == 3, Fit Each Value( {Line Color( {64, 111, 223} )} ) ),
Fit Where( :Slot_id == 4, Fit Each Value( {Line Color( {207, 121, 38} )} ) ),
Fit Where( :Slot_id == 5, Fit Each Value( {Line Color( {33, 189, 145} )} ) ),
Fit Where( :Slot_id == 6, Fit Each Value( {Line Color( {161, 44, 220} )} ) ),
Fit Where( :Slot_id == 7, Fit Each Value( {Line Color( {200, 193, 39} )} ) ),
Fit Where( :Slot_id == 8, Fit Each Value( {Line Color( {31, 182, 182} )} ) ),
Fit Where( :Slot_id == 9, Fit Each Value( {Line Color( {201, 37, 205} )} ) ),
Fit Where( :Slot_id == 10, Fit Each Value( {Line Color( {145, 183, 32} )} ) ),
Fit Where( :Slot_id == 11, Fit Each Value( {Line Color( {35, 157, 195} )} ) ),
Fit Where( :Slot_id == 12, Fit Each Value( {Line Color( {210, 38, 158} )} ) ),
Fit Where( :Slot_id == 13, Fit Each Value( {Line Color( {141, 165, 48} )} ) ),
Fit Where( :Slot_id == 14, Fit Each Value( {Line Color( {106, 191, 140} )} ) ),
Fit Where( :Slot_id == 15, Fit Each Value( {Line Color( {23, 67, 132} )} ) ),
Fit Where( :Slot_id == 16, Fit Each Value( {Line Color( {144, 81, 160} )} ) ),
Fit Where( :Slot_id == 17, Fit Each Value( {Line Color( {220, 133, 105} )} ) ),
Fit Where( :Slot_id == 18, Fit Each Value( {Line Color( {97, 136, 48} )} ) ),
Fit Where( :Slot_id == 19, Fit Each Value( {Line Color( {41, 228, 161} )} ) ),
Fit Where( :Slot_id == 20, Fit Each Value( {Line Color( {155, 162, 199} )} ) ),
Fit Where( :Slot_id == 21, Fit Each Value( {Line Color( {109, 28, 105} )} ) ),
Fit Where( :Slot_id == 22, Fit Each Value( {Line Color( {160, 109, 62} )} ) ),
Fit Where( :Slot_id == 23, Fit Each Value( {Line Color( {127, 228, 79} )} ) ),
Fit Where( :Slot_id == 24, Fit Each Value( {Line Color( {53, 113, 105} )} ) ),
Fit Where( :Slot_id == 25, Fit Each Value( {Line Color( {55, 42, 208} )} ) ),
 SendToReport(
Dispatch( {}, "1", ScaleBox, {Label Row( Show Major Grid( 1 ) )} ),
Dispatch( {}, "2", ScaleBox, {Minor Ticks( 1 ), Label Row( Show Major Grid( 1 ) )} ),
Dispatch( {}, "Bivar Plot", FrameBox, {Frame Size( 655, 344 ), Marker Size( 3 )} )
)
);

 This generates the data that I need but I don't understand why the script below does not. It will generate a plot but without "fit each value" lines and color assignments for each slot_id in the loop

R = [213, 57, 64, 207, 33, 161, 200, 31, 201, 145, 35, 210, 141, 106, 23, 144, 220, 97, 41, 155, 109, 160, 127, 53, 55];
G = [72, 177, 111, 121, 189, 44, 193, 182, 37, 183, 157, 38, 165, 191, 67, 81, 133, 136, 228, 162, 28, 109, 228, 113, 42];
B = [87, 67, 223, 38, 145, 220, 39, 182, 205, 32, 195, 158, 48, 140, 132, 160, 105, 48, 161, 199, 105, 62, 79, 105, 208];
SlotNums = Associative Array( :slot_id );
UniqueSlots = slotnums << get keys;
For( i = 1, i <= N Items( uniqueslots ), i++,
If( i == 1,
lines = Eval(
"Fitwhere(:Slot_id == " || Char( uniqueslots[i] ) || ", " || "Fit Each Value( {Line Color( {" || Char( R[i] ) || ", " || Char( G[i] ) ||
", " || Char( B[i] ) || "} )} )),"
 )
);
If( i < N Items( uniqueslots ),
Lines = Char( lines ) || Eval(
"Fitwhere(:Slot_id == " || Char( uniqueslots[i] ) || ", " || "Fit Each Value( {Line Color( {" || Char( R[i] ) || ", " || Char( G[i] ) ||
", " || Char( B[i] ) || "} )} )),"
 )
);
If( i == N Items( uniqueslots ),
Lines = Char( lines ) || Eval(
"Fitwhere(:Slot_id == " || Char( uniqueslots[i] ) || ", " || "Fit Each Value( {Line Color( {" || Char( R[i] ) || ", " || Char( G[i] ) ||
", " || Char( B[i] ) || "} )} ))"
 )
 );
);
 
bivariate(
Y( :name( "1st Thickness PST" ) ),
 
X( :site ),
By( :Lot ),
Parse( lines ),
 
SendToReport(
Dispatch( {}, "1", ScaleBox, {Label Row( Show Major Grid( 1 ) )} ),
Dispatch( {}, "2", ScaleBox, {Minor Ticks( 1 ), Label Row( Show Major Grid( 1 ) )} ),
Dispatch( {}, "Bivar Plot", FrameBox, {Frame Size( 655, 344 ), Marker Size( 3 )} )
 
)
);

 Can anyone suggest a method for getting this to work?

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson

Super User

Joined:

Jun 22, 2012

Solution

It appears that the parsing of your code is not connecting the "Lines".  The following solution will get what you want

R = [213, 57, 64, 207, 33, 161, 200, 31, 201, 145, 35, 210, 141, 106, 23, 144, 220, 97, 41, 155, 109, 160, 127, 53, 55];
G = [72, 177, 111, 121, 189, 44, 193, 182, 37, 183, 157, 38, 165, 191, 67, 81, 133, 136, 228, 162, 28, 109, 228, 113, 42];
B = [87, 67, 223, 38, 145, 220, 39, 182, 205, 32, 195, 158, 48, 140, 132, 160, 105, 48, 161, 199, 105, 62, 79, 105, 208];
SlotNums = Associative Array( :slot_id );
UniqueSlots = slotnums << get keys;
For( i = 1, i <= N Items( uniqueslots ), i++,
	If( i == 1,
		lines = Eval(
			"Fitwhere(:Slot_id == " || Char( uniqueslots[i] ) || ", " || "Fit Each Value( {Line Color( {" || Char( R[i] ) ||
			", " || Char( G[i] ) || ", " || Char( B[i] ) || "} )} )),"
		)
	);
	If( i < N Items( uniqueslots ),
		Lines = Char( lines ) || Eval(
			"Fitwhere(:Slot_id == " || Char( uniqueslots[i] ) || ", " || "Fit Each Value( {Line Color( {" || Char( R[i] ) ||
			", " || Char( G[i] ) || ", " || Char( B[i] ) || "} )} )),"
		)
	);
	If( i == N Items( uniqueslots ),
		Lines = Char( lines ) || Eval(
			"Fitwhere(:Slot_id == " || Char( uniqueslots[i] ) || ", " || "Fit Each Value( {Line Color( {" || Char( R[i] ) ||
			", " || Char( G[i] ) || ", " || Char( B[i] ) || "} )} ))"
		)
	);
);
 
Eval(
	Parse(
		"bivariate(
	Y( :name( \!"1st Thickness PST\!" ) ), 
 
	X( :site ),
	By( :Lot ),
	" || lines ||
		"), 
 
	SendToReport(
		Dispatch( {}, \!"1\!", ScaleBox, {Label Row( Show Major Grid( 1 ) )} ),
		Dispatch( {}, \!"2\!", ScaleBox, {Minor Ticks( 1 ), Label Row( Show Major Grid( 1 ) )} ),
		Dispatch( {}, \!"Bivar Plot\!", FrameBox, {Frame Size( 655, 344 ), Marker Size( 3 )} )
 
	)
);"
	)
);
Jim
3 REPLIES
txnelson

Super User

Joined:

Jun 22, 2012

Solution

It appears that the parsing of your code is not connecting the "Lines".  The following solution will get what you want

R = [213, 57, 64, 207, 33, 161, 200, 31, 201, 145, 35, 210, 141, 106, 23, 144, 220, 97, 41, 155, 109, 160, 127, 53, 55];
G = [72, 177, 111, 121, 189, 44, 193, 182, 37, 183, 157, 38, 165, 191, 67, 81, 133, 136, 228, 162, 28, 109, 228, 113, 42];
B = [87, 67, 223, 38, 145, 220, 39, 182, 205, 32, 195, 158, 48, 140, 132, 160, 105, 48, 161, 199, 105, 62, 79, 105, 208];
SlotNums = Associative Array( :slot_id );
UniqueSlots = slotnums << get keys;
For( i = 1, i <= N Items( uniqueslots ), i++,
	If( i == 1,
		lines = Eval(
			"Fitwhere(:Slot_id == " || Char( uniqueslots[i] ) || ", " || "Fit Each Value( {Line Color( {" || Char( R[i] ) ||
			", " || Char( G[i] ) || ", " || Char( B[i] ) || "} )} )),"
		)
	);
	If( i < N Items( uniqueslots ),
		Lines = Char( lines ) || Eval(
			"Fitwhere(:Slot_id == " || Char( uniqueslots[i] ) || ", " || "Fit Each Value( {Line Color( {" || Char( R[i] ) ||
			", " || Char( G[i] ) || ", " || Char( B[i] ) || "} )} )),"
		)
	);
	If( i == N Items( uniqueslots ),
		Lines = Char( lines ) || Eval(
			"Fitwhere(:Slot_id == " || Char( uniqueslots[i] ) || ", " || "Fit Each Value( {Line Color( {" || Char( R[i] ) ||
			", " || Char( G[i] ) || ", " || Char( B[i] ) || "} )} ))"
		)
	);
);
 
Eval(
	Parse(
		"bivariate(
	Y( :name( \!"1st Thickness PST\!" ) ), 
 
	X( :site ),
	By( :Lot ),
	" || lines ||
		"), 
 
	SendToReport(
		Dispatch( {}, \!"1\!", ScaleBox, {Label Row( Show Major Grid( 1 ) )} ),
		Dispatch( {}, \!"2\!", ScaleBox, {Minor Ticks( 1 ), Label Row( Show Major Grid( 1 ) )} ),
		Dispatch( {}, \!"Bivar Plot\!", FrameBox, {Frame Size( 655, 344 ), Marker Size( 3 )} )
 
	)
);"
	)
);
Jim
scottdprm

Community Trekker

Joined:

Jul 1, 2016

This works.  Thank you!

ih

Community Trekker

Joined:

Sep 30, 2016

I am late to the party but here is another way that uses Eval( Parse () ) on smaller strings.  This might be easier to debug.  If you only want to see the visualization another option might be to use graph builder (bottom of this script).

names default to here( 1 ) ;

//Sample data, add some categorical colums
dt = open( "$SAMPLE_DATA/football.jmp" );

dt << New Column( "Height Binned",
	Numeric, "Ordinal", Format( "Best", 12 ),
	Formula( Round( :Height * 2, -1 ) ) );

dt << New Column( "Weight Binned",
	Numeric, "Ordinal", Format( "Best", 12 ),
	Formula( Round( :Weight, -1 ) ) );

//colors
ColorList = {
	[213, 72, 87], [57, 177, 67], 
	[64, 111, 223], [33, 121, 38]
};

Color = function( {n}, ColorList[ mod(n, n items( ColorList ) ) ] );

//base plot
bv = dt << Bivariate(
	Y( :Speed ),
	X( :Weight Binned ),
	By( :Position2 ),
	SendToReport(
		Dispatch( {}, "1", ScaleBox, {Label Row( Show Major Grid( 1 ) )} ),
		Dispatch( {}, "2", ScaleBox, {Minor Ticks( 1 ), Label Row( Show Major Grid( 1 ) )} ),
		Dispatch( {}, "Bivar Plot", FrameBox, {Frame Size( 655, 344 ), Marker Size( 3 )} )
	)
);

//get list of bins to fit, remove missing values
bincol = "Height Binned";
bins = (Associative Array( column(dt, bincol) ) << get keys);

//add each bin to the chart
if( ( N Items( bins ) > 0 ) ,
	for( i = 1, i <= N Items( bins ), i++,
		s = "bv << Fit Where( " ||
			if( is missing( bins[i] ), 
				"is missing( :Name( \!"" || bincol || "\!" ) )",
				":Name( \!"" || bincol || "\!" ) == " || 
					char( bins[i] )
			) || 
			", Fit Each Value( Color( " || char( i ) || " ) ) )";
		show(s);
		Eval( Parse( s ) );
	)
);

//Graph builder option
dt << Graph Builder(
	Size( 425, 580 ),
	Show Control Panel( 0 ),
	Variables(
		X( :Weight Binned ),
		Y( :Speed ),
		Group Y( :Position2 ),
		Overlay( :Height Binned )
	),
	Elements( Line( X, Y, Legend( 17 ) ), Points( X, Y, Legend( 18 ) ) )
);