Gnupltxy.gms

Top  Previous  Next

Uwe Schneider and I developed a modified version of Rutherford's Gnuplot.gms trying to achieve simpler syntax (containing more default values than Rutherford) and simpler construction of so called (in spreadsheets) XY graphs where the X and Y data are not common across lines in the graph.  The package is distributed through http://www.uni-hamburg.de/Wiss/FB/15/Sustainability/schneider/gnuplot/.

To graph data in a GAMS program I need to do three basic things to use Gnupltxy.

Download the Gnupltxy software getting both the gms and windows gnuplot executable (wgnuplot.exe).
Fill a three-dimensional array.  In the example Simplegr.gms I fill an array named graphdata (you may use any other name) describing the data for the two items to graph where the first dimension is the name of the line, the second gives the set element names of the points to use, and third the data for the point giving the x and y coordinates.  Such statements appear below.

 

LINES      Lines in graph /A,B/

POINTS     Points on line /1*10/

ORDINATES  ORDINATES      /X-AXIS,Y-AXIS/  ;

TABLE GRAPHDATA(LINES,POINTS,ORDINATES)

       X-AXIS   Y-AXIS 0

A.1       1       1

A.2       2       4

A.3       3       9

A.4       5      25

A.5      10     100

B.1       1       2

B.2       3       6

B.3       7      15

B.4      12      36;

 

Then given the data call gnupltxy through a libinclude statement

 

 $LIBInclude Gnupltxy GRAPHDATA Y-AXIS X-AXIS

 

 where the first argument after gnupltxy gives the array name, the second name of a set element in the third array position which contains the data coordinates for the y axis and the third the name of a set element in the third array position which contains the data coordinates for the x axis.

 In turn when I run I get two new windows that automatically open in front of the IDE.

 

_img75

 

The window labeled gnuplot graph is the graph of the data and the window labeled gnuplot is a result of the execution of the wgnuplot executable.
I can plot more than one line by including the libinclude command more than once.
I can manipulate labels and set a number of options using setglobal commands as discussed in the Gnupltxy http://www.uni-hamburg.de/Wiss/FB/15/Sustainability/schneider/gnuplot/  and as illustrated in the Conditional Compilation chapter.  In particular, I draw two graphs and manipulate labeling in a manner as illustrated below (simplegr2.gms)

 

$setglobal gp_title  "First Graph of data "

$setglobal gp_xlabel "Label for X Axis"

$setglobal gp_ylabel "Label for Y Axis"

$LIBInclude Gnupltxy GRAPHDATA Y-AXIS X-AXIS

GRAPHDATA("B",POINTS,"Y-axis")

         $GRAPHDATA("B",POINTS,"Y-axis")=

 100-GRAPHDATA("B",POINTS,"Y-axis");

$setglobal gp_title  "Second Graph of data with modified line B "

$LIBInclude Gnupltxy GRAPHDATA Y-AXIS X-AXIS

 

where the setglobal commands enter labels for axes and graph title

This yields both the graph above and the graph below generated in 2 windows with 4 total windows generated.

 

_img3

 

_img76

 

Many more options are possible as listed in the documentation.  Many are embedded in the file plotopts.gms
Calculated data from a model solution can be graphed as illustrated in the example evportfo.gms

 

LOOP (RAPS,RAP=RISKAVER(RAPS);

 SOLVE EVPORTFOL USING NLP MAXIMIZING OBJ ;

 VAR = SUM(STOCK, SUM(STOCKS,

INVEST.L(STOCK)*COVAR(STOCK,STOCKS)*INVEST.L(STOCKS))) ;

 OUTPUT("RAP",RAPS)=RAP;

 OUTPUT(STOCKS,RAPS)=INVEST.L(STOCKS);

 OUTPUT("OBJ",RAPS)=OBJ.L;

 OUTPUT("MEAN",RAPS)=SUM(STOCKS, MEAN(STOCKS) *

INVEST.L(STOCKS));

 OUTPUT("VAR",RAPS) = VAR;

 OUTPUT("STD",RAPS)=SQRT(VAR);

 OUTPUT("SHADPRICE",RAPS)=INVESTAV.M;

 OUTPUT("IDLE",RAPS)=FUNDS-INVESTAV.L   );

parameter graphit (*,raps,*);

graphit("Frontier",raps,"Mean")=OUTPUT("MEAN",RAPS);

graphit("frontier",raps,"Var")=OUTPUT("std",RAPS)**2;

*$include gnu_opt.gms

* titles

$setglobal gp_title  "E-V Frontier "

$setglobal gp_xlabel "Variance of Income"

$setglobal gp_ylabel "Mean Income"

$libinclude gnupltxy graphit mean var

 

 Here a model is repeatedly solved and a three dimensional array called graphit is built which contains the name of the line to be graphed (frontier) and the mean and variances.  These means and variances were calculated using report writing statements into the array called output as discussed in the chapters on Improving Output via Report Writing and Doing a Comparative Analysis with GAMS.   The resultant graph is

 

_img4

 

Once a graph is in the window, a left click on it makes it available for cut and paste.
When using gnupltxy several things need to be present
The gnuplot executable needs to be in the GAMS system directory (nominally c:\program files\gams22.7\).  It is downloadable through my website and is called wgnuplot.exe
The gnupltxy.gms file must be in the inclib directory under the GAMS system directory =        (nominally c:\program files\gams22.7\inclib\).
Sometimes with Windows 2000 when you graph multiple graphs you have to close the first one before the second one becomes visible.