cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-240433%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EVertical%20Normal%20Distribution%20Polygon%20Shape%20(Graphics%20Scripting)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-240433%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CDIV%20class%3D%22lia-message-template-problem-zone%22%3EProblem%3CP%3EI%20wanted%20to%20create%20this%20figure%20for%20a%20publication%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture1.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Picture1.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Picture1.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F20874iD2B331866F86C182%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Picture1.png%22%20alt%3D%22Picture1.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3ESolution%3CP%3ETo%20create%20the%20vertically%20oriented%20shaded%2Ffilled%20and%20truncated%20normal%20curves%2C%20I%20utilized%20the%20Polygon()%20graphics%20function.%26nbsp%3B%20The%20challenge%20was%20to%20easily%20and%20repeated%20be%20able%20to%20create%20that%20polygon%20that%20was%20scaled%20and%20located%20appropriately%20on%20a%20graph.%26nbsp%3B%20%26nbsp%3B%20I%20did%20this%20using%20this%20function%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Efunc_VNormalShape%20%3D%20Function(%20%2F%2F%20create%20a%20polygon%20that%20looks%20like%20a%20vertically%20oriented%20normal%20distribution%0A%20%7B_xloc%2C%20_yloc%2C%20_xscale%20%3D%201%2C%20_yscale%20%3D%201%7D%2C%0A%20%7B_VNormShapeX%2C%20_VNormShapeY%7D%2C%20%0A%20%20%0A%20%20%2F%2F%20create%20a%20vector%20of%20y%20values%20to%20use%20for%20plotting%20the%20shape%0A%20_VNormShapeY%20%3D%20((_yloc%20-%203%20*%20_yscale)%20%3A%3A%20(_yloc%20%2B%203%20*%20_yscale)%20%3A%3A%20(_yscale%20%2F%2010))%60%3B%0A%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20create%20the%20filled%20polygon%20boundary%20points%0A%20_VNormShapeX%20%3D%20V%20Concat(%0A%20%20J(%2061%2C%201%2C%20_xloc%20)%2C%0A%20%20_xloc%20%2B%20Sqrt(%202%20*%20Pi()%20)%20*%20_yscale%20*%20_xscale%20*%20Normal%20Density(%0A%20%20%20Sort%20Descending(%20_VNormShapeY%20)%2C%0A%20%20%20_yloc%2C%0A%20%20%20_yscale%0A%20%20)%0A%0A%20)%3B%0A%20_VNormShapeY%20%3D%20V%20Concat(%0A%20%20_VNormShapeY%2C%0A%20%20Sort%20Descending(%20_VNormShapeY%20)%0A%0A%20)%3B%0A%20%20%0A%20%20%2F%2F%20substitute%20into%20the%20Polygon(function)%20the%20two%20vectors%20with%20the%20boundary%20point%20X%20and%20Y%20values%0A%20%2F%2F%20and%20evaluate%20the%20function%0A%20Eval(%0A%20%20Substitute(%0A%20%20%20%20Expr(%0A%20%20%20%20%20Polygon(%20Expr(%20shape_x%20)%2C%20Expr(%20shape_y%20)%20)%0A%20%20%20%20)%2C%0A%20%20%20Expr(%20shape_x%20)%2C%20_VNormShapeX%2C%0A%20%20%20Expr(%20shape_y%20)%2C%20_VNormShapeY%0A%20%20)%0A%20)%3B%0A)%3B%20%3C%2FCODE%3E%3C%2FPRE%3Ethe%20first%20two%20arguments%20of%20the%20function%20specify%20the%20%22origin%22%20or%20location%20of%20the%20distribution%20to%20be%20drawn%20(i.e.%20the%20(x%2Cy)%20position%20for%20the%20center%20base%20of%20the%20distribution).%26nbsp%3B%20The%20second%20two%20optional%20arguments%20are%20the%20scaling%20of%20the%20size%20of%20the%20distributions.%26nbsp%3B%20The%20default%20size%20is%20based%20on%20a%20standard%20normal%20distribution%2C%20and%20the%20scaling%20is%20relative%20the%20x%20direction%20and%20y%20direction.%26nbsp%3B%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3EThe%20result%20of%20the%20function%20is%20an%20evaluated%20expression%20that%20a%20graphics%20script%20used%20to%20draw%20the%20distribution%20as%20a%20polygon.%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3Eexamples%3A%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3Efunc_VNormalShape(1%2C3)%26nbsp%3B%20creates%20the%20polygon%20statement%20for%20a%20vertically%20oriented%20standard%20normal%20distribution%20centered%20at%20x%3D1%2C%20y%3D3.%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3Efunc_VNormalShape(2%2C4%2C%200.5%2C%201.5)%20creates%20the%20distribution%20centered%20at%20x%3D2%2C%20y%3D5%2C%20but%20the%20horizontal%20width%20is%20scaled%20down%20by%20a%20factor%20of%200.5%20and%20the%20vertical%20width%20is%20scaled%20up%20by%20a%20factor%20of%201.5.%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-solution-zone%22%3EDiscussion%3CP%3EHere%20is%20the%20full%20script%20that%20creates%20part%20of%20the%20of%20overall%20graphic.%26nbsp%3B%20I%20saved%20the%20output%20from%20this%20script%20as%20an%20image%20file%20and%20then%20imported%20into%20powerpoint%20to%20add%20some%20annotations%20and%20arrived%20at%20the%20graphic%20shown%20at%20the%20top%20of%20the%20post%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Enames%20default%20to%20here(1)%3B%0A%0Afunc_VNormalShape%20%3D%20Function(%20%2F%2F%20create%20a%20polygon%20that%20looks%20like%20a%20vertically%20oriented%20normal%20distribution%0A%20%7B_xloc%2C%20_yloc%2C%20_xscale%20%3D%201%2C%20_yscale%20%3D%201%7D%2C%0A%20%7B_VNormShapeX%2C%20_VNormShapeY%7D%2C%20%0A%20%20%0A%20%20%2F%2F%20create%20a%20vector%20of%20y%20values%20to%20use%20for%20plotting%20the%20shape%0A%20_VNormShapeY%20%3D%20((_yloc%20-%203%20*%20_yscale)%20%3A%3A%20(_yloc%20%2B%203%20*%20_yscale)%20%3A%3A%20(_yscale%20%2F%2010))%60%3B%0A%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20create%20the%20filled%20polygon%20boundary%20points%0A%20_VNormShapeX%20%3D%20V%20Concat(%0A%20%20J(%2061%2C%201%2C%20_xloc%20)%2C%0A%20%20_xloc%20%2B%20Sqrt(%202%20*%20Pi()%20)%20*%20_yscale%20*%20_xscale%20*%20Normal%20Density(%0A%20%20%20Sort%20Descending(%20_VNormShapeY%20)%2C%0A%20%20%20_yloc%2C%0A%20%20%20_yscale%0A%20%20)%0A%0A%20)%3B%0A%20_VNormShapeY%20%3D%20V%20Concat(%0A%20%20_VNormShapeY%2C%0A%20%20Sort%20Descending(%20_VNormShapeY%20)%0A%0A%20)%3B%0A%20%20%0A%20%20%2F%2F%20substitute%20into%20the%20Polygon(function)%20the%20two%20vectors%20with%20the%20boundary%20point%20X%20and%20Y%20values%0A%20%2F%2F%20and%20evaluate%20the%20function%0A%20Eval(%0A%20%20Substitute(%0A%20%20%20%20Expr(%0A%20%20%20%20%20Polygon(%20Expr(%20shape_x%20)%2C%20Expr(%20shape_y%20)%20)%0A%20%20%20%20)%2C%0A%20%20%20Expr(%20shape_x%20)%2C%20_VNormShapeX%2C%0A%20%20%20Expr(%20shape_y%20)%2C%20_VNormShapeY%0A%20%20)%0A%20)%3B%0A)%3B%20%0A%0A_nw%3Dnew%20window(%22Figure%22%2C%0A%20_ob%3DOutline%20Box(%22Figure%22%2C%0A%20%20_gb%3DGraph%20Box(%0A%20%20%20suppressaxes%2C%0A%20%20%20x%20scale(0%2C5)%2C%0A%20%20%20y%20scale(-1%2C11)%2C%0A%20%20%20Xname(%22%22)%2C%0A%20%20%20Yname(%22%22)%2C%0A%20%20%20fill%20color(%22gray%22)%3B%0A%20%20%20func_VNormalShape(1%2C%208%2C%20.75%2C%20.75)%3B%0A%20%20%20line(%7B1%2C8%7D%2C%20%7B1.75%2C%208%7D)%3B%0A%20%20%20func_VNormalShape(2%2C%206%2C%20.75%2C%20.75)%3B%0A%20%20%20line(%7B2%2C6%7D%2C%20%7B2.75%2C%206%7D)%3B%0A%20%20%20func_VNormalShape(3%2C%204%2C%20.75%2C%20.75)%3B%0A%20%20%20line(%7B3%2C4%7D%2C%20%7B3.75%2C%204%7D)%3B%0A%20%20%20func_VNormalShape(4%2C%202%2C%20.75%2C%20.75)%3B%0A%20%20%20line(%7B4%2C2%7D%2C%20%7B4.75%2C%202%7D)%3B%0A%20%20%20linestyle(1)%3B%0A%20%20%20Pen%20Size(2)%3B%0A%20%20%20line(%7B0.5%2C%205%7D%2C%20%7B4.5%2C%205%7D)%3B%0A%20%20%20%0A%20%20)%0A%20%20%0A%20)%0A)%3B%0A%0A_gb%5BFrameBox(1)%5D%26lt%3B%26lt%3B%7Btop(0)%2C%20bottom(0)%2C%20left(0)%2C%20right(0)%7D%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Eresulting%20output%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%221-7-2020%2011-06-14%20PM.png%22%20style%3D%22width%3A%20462px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%221-7-2020%2011-06-14%20PM.png%22%20style%3D%22width%3A%20462px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%221-7-2020%2011-06-14%20PM.png%22%20style%3D%22width%3A%20462px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F20875i03850EAC6C6DBAD5%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%221-7-2020%2011-06-14%20PM.png%22%20alt%3D%221-7-2020%2011-06-14%20PM.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CBR%20%2F%3ESee%20Also%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-solution-zone%22%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-solution-zone%22%3EI%20had%20also%20used%20this%20function%20in%20this%20demonstration%20%2F%20teaching%20script%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FJMP-Scripts%2FAnimated-Sampling-Distribution%2Fta-p%2F79456%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fcommunity.jmp.com%2Ft5%2FJMP-Scripts%2FAnimated-Sampling-Distribution%2Fta-p%2F79456%3C%2FA%3E%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-solution-zone%22%3EI%20wanted%20to%20share%20it%20again%20by%20itself%2C%20as%20it%20was%20really%20handy%20today%20when%20I%20pulled%20it%20out%20to%20make%20my%20figure.%26nbsp%3B%26nbsp%3B%3C%2FDIV%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
Vertical Normal Distribution Polygon Shape (Graphics Scripting)
Problem

I wanted to create this figure for a publication

 

Picture1.png

Solution

To create the vertically oriented shaded/filled and truncated normal curves, I utilized the Polygon() graphics function.  The challenge was to easily and repeated be able to create that polygon that was scaled and located appropriately on a graph.    I did this using this function

 

func_VNormalShape = Function( // create a polygon that looks like a vertically oriented normal distribution
	{_xloc, _yloc, _xscale = 1, _yscale = 1},
	{_VNormShapeX, _VNormShapeY}, 
		
		// create a vector of y values to use for plotting the shape
	_VNormShapeY = ((_yloc - 3 * _yscale) :: (_yloc + 3 * _yscale) :: (_yscale / 10))`;

        
        // create the filled polygon boundary points
	_VNormShapeX = V Concat(
		J( 61, 1, _xloc ),
		_xloc + Sqrt( 2 * Pi() ) * _yscale * _xscale * Normal Density(
			Sort Descending( _VNormShapeY ),
			_yloc,
			_yscale
		)

	);
	_VNormShapeY = V Concat(
		_VNormShapeY,
		Sort Descending( _VNormShapeY )

	);
		
		// substitute into the Polygon(function) the two vectors with the boundary point X and Y values
	// and evaluate the function
	Eval(
		Substitute(
				Expr(
					Polygon( Expr( shape_x ), Expr( shape_y ) )
				),
			Expr( shape_x ), _VNormShapeX,
			Expr( shape_y ), _VNormShapeY
		)
	);
); 
the first two arguments of the function specify the "origin" or location of the distribution to be drawn (i.e. the (x,y) position for the center base of the distribution).  The second two optional arguments are the scaling of the size of the distributions.  The default size is based on a standard normal distribution, and the scaling is relative the x direction and y direction.  
 
The result of the function is an evaluated expression that a graphics script used to draw the distribution as a polygon.
examples:
func_VNormalShape(1,3)  creates the polygon statement for a vertically oriented standard normal distribution centered at x=1, y=3.
 
func_VNormalShape(2,4, 0.5, 1.5) creates the distribution centered at x=2, y=5, but the horizontal width is scaled down by a factor of 0.5 and the vertical width is scaled up by a factor of 1.5.
Discussion

Here is the full script that creates part of the of overall graphic.  I saved the output from this script as an image file and then imported into powerpoint to add some annotations and arrived at the graphic shown at the top of the post

 

names default to here(1);

func_VNormalShape = Function( // create a polygon that looks like a vertically oriented normal distribution
	{_xloc, _yloc, _xscale = 1, _yscale = 1},
	{_VNormShapeX, _VNormShapeY}, 
		
		// create a vector of y values to use for plotting the shape
	_VNormShapeY = ((_yloc - 3 * _yscale) :: (_yloc + 3 * _yscale) :: (_yscale / 10))`;

        
        // create the filled polygon boundary points
	_VNormShapeX = V Concat(
		J( 61, 1, _xloc ),
		_xloc + Sqrt( 2 * Pi() ) * _yscale * _xscale * Normal Density(
			Sort Descending( _VNormShapeY ),
			_yloc,
			_yscale
		)

	);
	_VNormShapeY = V Concat(
		_VNormShapeY,
		Sort Descending( _VNormShapeY )

	);
		
		// substitute into the Polygon(function) the two vectors with the boundary point X and Y values
	// and evaluate the function
	Eval(
		Substitute(
				Expr(
					Polygon( Expr( shape_x ), Expr( shape_y ) )
				),
			Expr( shape_x ), _VNormShapeX,
			Expr( shape_y ), _VNormShapeY
		)
	);
); 

_nw=new window("Figure",
	_ob=Outline Box("Figure",
		_gb=Graph Box(
			suppressaxes,
			x scale(0,5),
			y scale(-1,11),
			Xname(""),
			Yname(""),
			fill color("gray");
			func_VNormalShape(1, 8, .75, .75);
			line({1,8}, {1.75, 8});
			func_VNormalShape(2, 6, .75, .75);
			line({2,6}, {2.75, 6});
			func_VNormalShape(3, 4, .75, .75);
			line({3,4}, {3.75, 4});
			func_VNormalShape(4, 2, .75, .75);
			line({4,2}, {4.75, 2});
			linestyle(1);
			Pen Size(2);
			line({0.5, 5}, {4.5, 5});
			
		)
		
	)
);

_gb[FrameBox(1)]<<{top(0), bottom(0), left(0), right(0)};

resulting output:

1-7-2020 11-06-14 PM.png


See Also
 
I had also used this function in this demonstration / teaching script https://community.jmp.com/t5/JMP-Scripts/Animated-Sampling-Distribution/ta-p/79456
I wanted to share it again by itself, as it was really handy today when I pulled it out to make my figure.  
JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.