Choose Language Hide Translation Bar

## Creating customized graphs inside a function

I want to write a function which will plot a chart using a specific set of parameters, and then call it multiple times as in the simple example below.  This example doesn't actually work however, presumably because the "X" that I'm referencing inside the graph box is local to the "myplot" function.  What is the easiest way I could modify the following so that it would work as I intend, i.e. the letters "A", "B", "C" and "D" should be written inside each of the four charts respectively?

myplot1 = Function( {X},

gb = Graph Box(

FrameSize( 100, 100 ),

X Scale( 0, 10 ),

Y Scale( 0, 100 ),

Text( center justified, {5, 50}, X )

)

);

nw1 = New Window( "Four Charts",

Lineup Box( N Col( 2 ), myplot1( "A" ), myplot1( "B" ), myplot1( "C" ), myplot1( "D" ) )

);

Many thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Community Trekker

Joined:

Jun 23, 2011

Solution

## Creating customized graphs inside a function

You need to substitute the current value into your Graph Box expression on each call to the function, like this:

myplot1 = Function( {X},

gb = Substitute(Expr(

Graph Box(

FrameSize( 100, 100 ),

X Scale( 0, 10 ),

Y Scale( 0, 100 ),

Text( center justified, {5, 50}, xxx )

)),

Expr(xxx),

X

);

gb // to evaluate the Graph Box expression, alternatively put substitute command inside Eval()

);

nw1 = New Window( "Four Charts",

Lineup Box( N Col( 2 ), myplot1( "A" ), myplot1( "B" ), myplot1( "C" ), myplot1( "D" ) )

);

6 REPLIES 6

Community Trekker

Joined:

Jun 23, 2011

Solution

## Creating customized graphs inside a function

You need to substitute the current value into your Graph Box expression on each call to the function, like this:

myplot1 = Function( {X},

gb = Substitute(Expr(

Graph Box(

FrameSize( 100, 100 ),

X Scale( 0, 10 ),

Y Scale( 0, 100 ),

Text( center justified, {5, 50}, xxx )

)),

Expr(xxx),

X

);

gb // to evaluate the Graph Box expression, alternatively put substitute command inside Eval()

);

nw1 = New Window( "Four Charts",

Lineup Box( N Col( 2 ), myplot1( "A" ), myplot1( "B" ), myplot1( "C" ), myplot1( "D" ) )

);

## Creating customized graphs inside a function

Thanks - that's perfect.  May I ask a related question while I'm thinking about it?  If I wanted to generalize that to incorporating two or more parameters, could I do that essentially the same way, and if so, how would that change the syntax?

Again, many thanks

Community Trekker

Joined:

Jun 23, 2011

## Creating customized graphs inside a function

Can you be more specific?  How do you want to incorporate multiple parameters?  Show me an example of what you would like to do.

## Creating customized graphs inside a function

Sure - the sort of thing I was thinking of is a situation where I'd feed into the function several parameters telling the chart how I want different features of it displayed.  For example in the case of just two parameters, if I wanted a single window showing several different polynomial fits to the same data, with each one having a different title - or to generalise it further, I might want to feed in the lower and upper limits of the axes, the colors of the fitted lines, the frame size etc.

Since I started composing this reply however, I’ve found a neat way to do exactly what I want, just by supplying a list as the argument to the function instead of a single parameter:

myplot1 = Function( {X},

gb = Substitute(

Expr(

Graph Box(FrameSize( 100, 100 ),

X Scale( 0, 10 ),

Y Scale( 0, xxx[1] ),

Text( center justified, {5, 4*xxx[1]/5}, xxx[2] );

)

),

Expr( xxx ), X

);

gb

);

nw1 = New Window( "Four Charts",

Lineup Box( N Col( 2 ),

myplot1( {50, "Linear"} ), myplot1( {70, "Quadratic"} ),

myplot1( {90, "Cubic"} ), myplot1( {200, "Quartic"} ) )

);

So many thanks – that answers everything!

Community Trekker

Joined:

Jun 23, 2011

## Creating customized graphs inside a function

If you wanted to make your function calls more "traditional," you could do the following with the same effect:

myplot1 = Function( {X1, X2},

gb = Substitute(

Expr(

Graph Box(FrameSize( 100, 100 ),

X Scale( 0, 10 ),

Y Scale( 0, xxx ),

Text( center justified, {5, 4*xxx/5}, yyy );

)

),

Expr( xxx ), X1,

Expr( yyy ), X2

);

gb

);

nw1 = New Window( "Four Charts",

Lineup Box( N Col( 2 ),

myplot1( 50, "Linear" ), myplot1( 70, "Quadratic" ),

myplot1( 90, "Cubic" ), myplot1( 200, "Quartic" )

)

);

## Creating customized graphs inside a function

The above syntax was what I was trying to get right when I realised that I could use a list instead - so yes, that's even better, as it enables me to see at a glance which expression corresponds to which input parameter.  Many thanks yet again!