Simple line plot and multiple windows demo

Image obtained by using pbm driver and then converted to png.

  1 #pragma package "../"
  2 #include <math.h>
  3 #include "chplplot.h"
  4 
  5 static PLFLT x[101], y[101];
  6 static PLFLT xscale, yscale, xoff, yoff, xs[6], ys[6];
  7 static PLGraphicsIn *gin;
  8 
  9 static int locate_mode;
 10 static int test_xor;
 11 static int fontset = 1;
 12 static char *f_name = NULL;
 13 
 14 
 15 char *notes[] = {"Make sure you get it right!", NULL};
 16 
 17 
 18 void plot1(int);
 19 void plot2(void);
 20 void plot3(void);
 21 
 22 
 23 int
 24 main(int argc, char *argv[])
 25 {
 26     gin = new PLGraphicsIn();
 27     PLINT digmax, cur_strm, new_strm;
 28     char ver[80];
 29 
 30 
 31     plssub(2, 2);
 32 
 33 
 34     plParseOpts(&argc, argv, PL_PARSE_FULL);
 35 
 36 
 37     plgver(ver);
 38     fprintf(stderr, "PLplot library version: %s\n", ver);
 39 
 40 
 41     plinit();
 42 
 43     if (fontset)
 44 	plfontld(1);
 45     else
 46 	plfontld(0);
 47 
 48 
 49     xscale = 6.;
 50     yscale = 1.;
 51     xoff = 0.;
 52     yoff = 0.;
 53 
 54 
 55     plot1(0);
 56 
 57 
 58     xscale = 1.;
 59     yscale = 0.0014;
 60     yoff = 0.0185;
 61 
 62 
 63     digmax = 5;
 64     plsyax(digmax, 0);
 65 
 66     plot1(1);
 67 
 68     plot2();
 69 
 70     plot3();
 71 
 72 
 73     if (f_name) {
 74       printf("The current plot was saved in color Postscript under the name
 75 ++++`%s'.\n", f_name);
 76       plgstrm(&cur_strm);      plmkstrm(&new_strm); 
 77     
 78       plsfnam(f_name);      plsdev("psc");
 79       plcpstrm(cur_strm, 0);      plreplot();	      plend1();
 80       plsstrm(cur_strm);    }
 81 
 82 
 83     if (locate_mode) {
 84 	for (;;) {
 85 	  if (! plGetCursor(gin)) break;
 86 	  if ((int)gin->keysym_get() == (int)PLK_Escape) break;
 87 
 88 	    pltext();
 89 	    if (gin->keysym_get() < 0xFF && isprint(gin->keysym_get())) 
 90 		printf("subwin = %d, wx = %f,  wy = %f, dx = %f,  dy = %f,  c = '%c'\n",
 91 		       gin->subwindow_get(), gin->wX_get(), gin->wY_get(), gin->dX_get(),
 92 ++++gin->dY_get(), gin->keysym_get());
 93 	    else
 94 		printf("subwin = %d, wx = %f,  wy = %f, dx = %f,  dy = %f,  c = 0x%02x\n",
 95 		       gin->subwindow_get(), gin->wX_get(), gin->wY_get(), gin->dX_get(),
 96 ++++gin->dY_get(), gin->keysym_get());
 97 
 98 	    plgra();
 99 	}
100     }
101 
102 
103     plend();
104     exit(0);
105 }
106 
107  
108 void
109 plot1(int do_test)
110 {
111   int i;
112   PLINT st;
113     PLFLT xmin, xmax, ymin, ymax;
114 
115     for (i = 0; i < 60; i++) {
116 	x[i] = xoff + xscale * (i + 1) / 60.0;
117 	y[i] = yoff + yscale * pow(x[i], 2.);
118     }
119 
120     xmin = x[0];
121     xmax = x[59];
122     ymin = y[0];
123     ymax = y[59];
124 
125     for (i = 0; i < 6; i++) {
126 	xs[i] = x[i * 10 + 3];
127 	ys[i] = y[i * 10 + 3];
128     }
129 
130     plcol0(1);
131     plenv(xmin, xmax, ymin, ymax, 0, 0);
132     plcol0(2);
133     pllab("(x)", "(y)", "#frPLplot Example 1 - y=x#u2");
134 
135 
136     plcol0(4);
137     plpoin(6, xs, ys, 9);
138 
139 
140     plcol0(3);
141     plline(60, x, y);
142 
143 
144     if (do_test && test_xor) {
145 #ifdef HAVE_USLEEP
146 	plxormod(1, &st);	if (st) {
147 	  for (i=0; i<60; i++) {
148 	    plpoin(1, x+i, y+i,9);		    usleep(50000);			    plflush();				   
149 ++++plpoin(1, x+i, y+i,9);		  }
150 	  plxormod(0, &st);				}
151 #else
152     printf("The -xor command line option can only be exercised if your "
153            "system\nhas usleep(), which does not seem to happen.\n"); 
154 #endif
155     }
156 }
157  
158 
159 void
160 plot2(void)
161 {
162     int i;
163 
164     plcol0(1);
165     plenv(-2.0, 10.0, -0.4, 1.2, 0, 1);
166     plcol0(2);
167     pllab("(x)", "sin(x)/x", "#frPLplot Example 1 - Sinc Function");
168 
169 
170     for (i = 0; i < 100; i++) {
171 	x[i] = (i - 19.0) / 6.0;
172 	y[i] = 1.0;
173 	if (x[i] != 0.0)
174 	    y[i] = sin(x[i]) / x[i];
175     }
176 
177 
178     plcol0(3);
179     plwid(2);
180     plline(100, x, y);
181     plwid(1);
182 }
183 
184 
185 void
186 plot3(void)
187 {
188     PLINT space0 = 0, mark0 = 0, space1 = 1500, mark1 = 1500;
189     int i;
190 
191     pladv(0);
192 
193     plvsta();
194     plwind(0.0, 360.0, -1.2, 1.2);
195 
196 
197     plcol0(1);
198     plbox("bcnst", 60.0, 2, "bcnstv", 0.2, 2);
199 
200     plstyl(1, &mark1, &space1);
201     plcol0(2);
202     plbox("g", 30.0, 0, "g", 0.2, 0);
203     plstyl(0, &mark0, &space0);
204 
205     plcol0(3);
206     pllab("Angle (degrees)", "sine", "#frPLplot Example 1 - Sine function");
207 
208     for (i = 0; i < 101; i++) {
209 	x[i] = 3.6 * i;
210 	y[i] = sin(x[i] * M_PI / 180.0);
211     }
212 
213     plcol0(4);
214     plline(101, x, y);
215 }

Code description

4
Variables and data arrays used by plot generators
14
Options data structure definition.
17
Function prototypes
22
--------------------------------------------------------------------------*\ * main * * Generates several simple line plots. Demonstrates: * - subwindow capability * - setting up the window, drawing plot, and labelling * - changing the color * - automatic axis rescaling to exponential notation * - placing the axes in the middle of the box * - gridded coordinate axes \*--------------------------------------------------------------------------
30
plplot initialization
30
Divide page into 2x2 plots unless user overrides
33
Parse and process command line arguments
34
plMergeOpts(options, "x01c options", notes);
36
Get version number, just for kicks
40
Initialize plplot
42
Select font set as per input flag
48
Set up the data
48
Original case
54
Do a plot
57
Set up the data
62
Do a plot
72
* Show how to save a plot: * Open a new device, make it current, copy parameters, * and replay the plot buffer
73
command line option '-save filename'
75
get current stream
75
create a new one
77
file name
77
device type
78
copy old stream parameters to new stream
78
do the save by replaying the plot buffer
78
finish the device
79
return to previous stream
81
Let's get some user input
99
Don't forget to call plend() to finish off!
104
===============================================================
127
Set up the viewport and window using PLENV. The range in X is * 0.0 to 6.0, and the range in Y is 0.0 to 30.0. The axes are * scaled separately (just = 0), and we just draw a labelled * box (axis = 0).
132
Plot the data points
136
Draw the line through the data
140
xor mode enable erasing a line/point/text by replotting it again
140
it does not work in double buffering mode, however
143
enter xor mode
145
draw a point
145
wait a little
145
force an update of the tk driver
145
erase point
146
leave xor mode
154
===============================================================
160
Set up the viewport and window using PLENV. The range in X is -2.0 to * 10.0, and the range in Y is -0.4 to 2.0. The axes are scaled separately * (just = 0), and we draw a box with axes (axis = 1).
165
Fill up the arrays
173
Draw the line
180
===============================================================
187
For the final graph we wish to override the default tick intervals, and * so do not use plenv().
189
Use standard viewport, and define X range from 0 to 360 degrees, Y range * from -1.2 to 1.2.
192
Draw a box with ticks spaced 60 degrees apart in X, and 0.2 in Y.
196
Superimpose a dashed line grid, with 1.5 mm marks and spaces. * plstyl expects a pointer!

Copyright 2006 Roman Putanowicz

Email: putanowr at twins.pk.edu.pl

Last Modified: Thu, 13 Jul 2006 15:42:11 CEST

Made with PubTal 3.1.3