pmm  1.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
gnuplot_i.c
Go to the documentation of this file.
1 #ifndef _GNUPLOT_PIPES_C_
2 #define _GNUPLOT_PIPES_C_
3 /*-------------------------------------------------------------------------*/
4 /**
5  @file gnuplot_i.c
6  @author N. Devillard (parts R. Bradley, R.Higgins)
7  @date Sep 1998, Oct 2004, Sept 2005, Nov 2005, Apr 2006, Dec 2009
8  @version $Revision: 2.10.4 $
9  @brief C interface to gnuplot.
10 
11  gnuplot is a freely available, command-driven graphical display tool for
12  Unix. It compiles and works quite well on a number of Unix flavours as
13  well as other operating systems. The following module enables sending
14  display requests to gnuplot through simple C calls.
15 
16  3D and colour printing extensions added October 2004 by Robert Bradley
17  (robert.bradley@merton.ox.ac.uk), based on existing code:
18 
19  gnuplot_splot (plot 3D graph), gnuplot_set_zlabel (set the z axis label for 3D plots),
20  gnuplot_hardcopy_colour (for printing in colour)
21 
22  OS X and limited Windows support added 11-13th September 2005:
23 
24  On Windows, pgnuplot and wgnuplot.exe must be in the current directory.
25  On OS X, if DISPLAY environment variable is not set, or USE_AQUA=1, aqua is
26  used instead of x11.
27  gnuplot_setterm also added to allow the terminal type to be set.
28 
29  gnuplot_plot_obj_xy added 15th September 2005 - plotting data from arbitrary
30  structures using callback functions
31  gnuplot_contour_plot added 23rd November 2005 (plots contours, use
32  gnuplot_setstyle to set the contour style)
33 
34  gnuplot_splot_grid added 2nd April 2006 - plots 2D grids of data [x,y]
35 
36  gnuplot_setaxes added December 2009 (R. Higgins)
37 */
38 /*--------------------------------------------------------------------------*/
39 
40 /*
41  $Id: gnuplot_i.c,v 2.10 2003/01/27 08:58:04 ndevilla Exp $
42  $Author: ndevilla $
43  $Date: 2003/01/27 08:58:04 $
44  $Revision: 2.10.2 $
45  */
46 
47 /*---------------------------------------------------------------------------
48  Includes
49  ---------------------------------------------------------------------------*/
50 
51 #include "gnuplot_i.h"
52 
53 /*---------------------------------------------------------------------------
54  Defines
55  ---------------------------------------------------------------------------*/
56 
57 /** Maximal size of a gnuplot command */
58 #define GP_CMD_SIZE 2048
59 /** Maximal size of a plot title */
60 #define GP_TITLE_SIZE 80
61 /** Maximal size for an equation */
62 #define GP_EQ_SIZE 512
63 /** Maximal size of a name in the PATH */
64 #define PATH_MAXNAMESZ 4096
65 
66 #ifdef _WIN32
67 #undef P_tmpdir
68 #endif
69 /** Define P_tmpdir if not defined (this is normally a POSIX symbol) */
70 #ifndef P_tmpdir
71 #define P_tmpdir "."
72 #endif
73 
74 #define GNUPLOT_TEMPFILE "%s/gnuplot-i-XXXXXX"
75 #ifndef _WIN32
76 #define GNUPLOT_EXEC "gnuplot"
77 #else
78 #define GNUPLOT_EXEC "pgnuplot.exe"
79 #endif
80 
81 /*---------------------------------------------------------------------------
82  Function codes
83  ---------------------------------------------------------------------------*/
84 
85 #ifdef _WIN32
86 #include <windows.h>
87 #include <fcntl.h>
88 
89 int mkstemp(char* templ) {
90  srand(time(NULL));
91  int i;
92  char *start=strstr(templ, "XXXXXX");
93  for (i=0;i<6;i++) {
94  start[i]=(char) (48+((int) rand()*10/32768.0));
95  }
96  //while ((start=strchr(templ, '/'))!=NULL) {start[0]='-';}
97  //printf("Opening %s...\n",templ);
98  i=open(templ,O_RDWR | O_CREAT);
99  if (i!=-1) {
100  DWORD dwFileAttr=GetFileAttributes(templ);
101  SetFileAttributes(templ,dwFileAttr & !FILE_ATTRIBUTE_READONLY);
102  }
103  return i;
104 }
105 #endif
106 
107 /*-------------------------------------------------------------------------*/
108 /**
109  @brief Find out where a command lives in your PATH.
110  @param pname Name of the program to look for.
111  @return pointer to statically allocated character string.
112 
113  This is the C equivalent to the 'which' command in Unix. It parses
114  out your PATH environment variable to find out where a command
115  lives. The returned character string is statically allocated within
116  this function, i.e. there is no need to free it. Beware that the
117  contents of this string will change from one call to the next,
118  though (as all static variables in a function).
119 
120  The input character string must be the name of a command without
121  prefixing path of any kind, i.e. only the command name. The returned
122  string is the path in which a command matching the same name was
123  found.
124 
125  Examples (assuming there is a prog named 'hello' in the cwd):
126 
127  @verbatim
128  gnuplot_get_program_path("hello") returns "."
129  gnuplot_get_program_path("ls") returns "/bin"
130  gnuplot_get_program_path("csh") returns "/usr/bin"
131  gnuplot_get_program_path("/bin/ls") returns NULL
132  @endverbatim
133 
134  */
135 /*-------------------------------------------------------------------------*/
136 
137 char * gnuplot_get_program_path(char * pname)
138 {
139  int i, j, lg;
140  char * path;
141  static char buf[PATH_MAXNAMESZ];
142 
143  /* Trivial case: try in CWD */
144  sprintf(buf, "./%s", pname) ;
145  if (access(buf, X_OK)==0) {
146  sprintf(buf, ".");
147  return buf ;
148  }
149  /* Try out in all paths given in the PATH variable */
150  buf[0] = 0;
151  path = getenv("PATH") ;
152  if (path!=NULL) {
153  for (i=0; path[i]; ) {
154  for (j=i ; (path[j]) && (path[j]!=':') ; j++);
155  lg = j - i;
156  strncpy(buf, path + i, lg);
157  if (lg == 0) buf[lg++] = '.';
158  buf[lg++] = '/';
159  strcpy(buf + lg, pname);
160  if (access(buf, X_OK) == 0) {
161  /* Found it! */
162  break ;
163  }
164  buf[0] = 0;
165  i = j;
166  if (path[i] == ':') i++ ;
167  }
168  } else {
169  fprintf(stderr, "PATH variable not set\n");
170  }
171  /* If the buffer is still empty, the command was not found */
172  if (buf[0] == 0) return NULL ;
173  /* Otherwise truncate the command name to yield path only */
174  lg = strlen(buf) - 1 ;
175  while (buf[lg]!='/') {
176  buf[lg]=0 ;
177  lg -- ;
178  }
179  buf[lg] = 0;
180  return buf ;
181 }
182 
183 /*-------------------------------------------------------------------------*/
184 /**
185  @brief Opens up a gnuplot session, ready to receive commands.
186  @return Newly allocated gnuplot control structure.
187 
188  This opens up a new gnuplot session, ready for input. The struct
189  controlling a gnuplot session should remain opaque and only be
190  accessed through the provided functions.
191 
192  The session must be closed using gnuplot_close().
193  */
194 /*--------------------------------------------------------------------------*/
195 
197 {
198  gnuplot_ctrl * handle ;
199 #ifndef _WIN32
200 #ifndef __APPLE__
201  if (getenv("DISPLAY") == NULL) {
202  fprintf(stderr, "cannot find DISPLAY variable: is it set?\n") ;
203  }
204 #endif
205 #endif
206 #ifndef _WIN32
207  if (gnuplot_get_program_path("gnuplot")==NULL) {
208  fprintf(stderr, "cannot find gnuplot in your PATH");
209  return NULL ;
210  }
211 #endif
213  fprintf(stderr, "cannot find gnuplot in your PATH");
214  return NULL ;
215  }
216 
217  /*
218  * Structure initialization:
219  */
220  handle = (gnuplot_ctrl*)malloc(sizeof(gnuplot_ctrl)) ;
221  handle->nplots = 0 ;
222  gnuplot_setstyle(handle, "points") ;
223  gnuplot_setaxes(handle, "x1y1") ;
224  handle->ntmp = 0 ;
225  handle->gnucmd = popen(GNUPLOT_EXEC " -geometry \"640x480+0+0\"", "w") ;
226  if (handle->gnucmd == NULL) {
227  fprintf(stderr, "error starting gnuplot\n") ;
228 
229  free(handle) ;
230  handle = NULL;
231 
232  return NULL ;
233  }
234 #ifdef _WIN32
235  gnuplot_setterm(handle,"windows");
236 #elif __APPLE__
237  // Try to determine whether we should use aqua or x11 as our default
238  if (getenv("DISPLAY") == NULL || (getenv("USE_AQUA")!=NULL && strcmp(getenv("USE_AQUA"),"1")>=0))
239  gnuplot_setterm(handle,"aqua");
240  else
241  gnuplot_setterm(handle,"x11");
242 #else
243  gnuplot_setterm(handle,"x11");
244 #endif
245  return handle;
246 }
247 
248 /*-------------------------------------------------------------------------*/
249 /**
250  @brief Closes a gnuplot session previously opened by gnuplot_init()
251  @param handle Gnuplot session control handle.
252  @return void
253 
254  Kills the child PID and deletes all opened temporary files.
255  It is mandatory to call this function to close the handle, otherwise
256  temporary files are not cleaned and child process might survive.
257 
258  */
259 /*--------------------------------------------------------------------------*/
260 
262 {
263  int i ;
264 
265  if (pclose(handle->gnucmd) == -1) {
266  fprintf(stderr, "problem closing communication to gnuplot\n") ;
267  return ;
268  }
269  if (handle->ntmp) {
270  for (i=0 ; i<handle->ntmp ; i++) {
271 #ifdef _WIN32
272  int x=remove(handle->to_delete[i]) ;
273  if (x)
274  printf("Failure to delate %s: error number %d\n",handle->to_delete[i],errno);
275 #else
276  remove(handle->to_delete[i]);
277 #endif
278  }
279  }
280 
281  free(handle) ;
282  handle = NULL ;
283 
284  return ;
285 }
286 
287 
288 /*-------------------------------------------------------------------------*/
289 /**
290  @brief Sends a command to an active gnuplot session.
291  @param handle Gnuplot session control handle
292  @param cmd Command to send, same as a printf statement.
293 
294  This sends a string to an active gnuplot session, to be executed.
295  There is strictly no way to know if the command has been
296  successfully executed or not.
297  The command syntax is the same as printf.
298 
299  Examples:
300 
301  @code
302  gnuplot_cmd(g, "plot %d*x", 23.0);
303  gnuplot_cmd(g, "plot %g * cos(%g * x)", 32.0, -3.0);
304  @endcode
305 
306  Since the communication to the gnuplot process is run through
307  a standard Unix pipe, it is only unidirectional. This means that
308  it is not possible for this interface to query an error status
309  back from gnuplot.
310  */
311 /*--------------------------------------------------------------------------*/
312 
313 void gnuplot_cmd(gnuplot_ctrl * handle, char * cmd, ...)
314 {
315  va_list ap ;
316  char local_cmd[GP_CMD_SIZE];
317 
318  va_start(ap, cmd);
319  vsprintf(local_cmd, cmd, ap);
320  va_end(ap);
321 
322  strcat(local_cmd, "\n");
323 
324  fputs(local_cmd, handle->gnucmd) ;
325  fflush(handle->gnucmd);
326  return ;
327 }
328 
329 /*-------------------------------------------------------------------------*/
330 /**
331  @brief Read commands from standard in and send them to gnuplot handle
332  @param handle Gnuplot session control handle
333 
334  This reads commands from a simple interactive prompt and sends them to
335  be executed via the gnuplot handle. There is no indicatio nof whether a
336  command is executed successfully or not and no checking of input command
337  syntax.
338 
339  Ctrl+D ends the interactive session.
340  */
341 /*--------------------------------------------------------------------------*/
342 void gnuplot_prompt(gnuplot_ctrl * handle) {
343 
344  char cmd[GP_CMD_SIZE];
345 
346  printf("Entering interactive gnuplot mode (experimental) ...\n");
347 
348  printf("> ");
349  while(fgets(cmd, GP_CMD_SIZE, stdin) != NULL) {
350  gnuplot_cmd(handle, cmd);
351 
352  printf("> ");
353  }
354 
355  printf("Leaving interactive gnuplot mode ... \n");
356 
357  return;
358 }
359 
360 
361 
362 
363 /*-------------------------------------------------------------------------*/
364 /**
365  @brief Change the plotting style of a gnuplot session.
366  @param h Gnuplot session control handle
367  @param plot_style Plotting-style to use (character string)
368  @return void
369 
370  The provided plotting style is a character string. It must be one of
371  the following:
372 
373  - lines
374  - points
375  - linespoints
376  - impulses
377  - dots
378  - steps
379  - errorbars
380  - boxes
381  - boxeserrorbars
382  */
383 /*--------------------------------------------------------------------------*/
384 
385 void gnuplot_setstyle(gnuplot_ctrl * h, char * plot_style)
386 {
387  if (strcmp(plot_style, "lines") &&
388  strcmp(plot_style, "points") &&
389  strcmp(plot_style, "linespoints") &&
390  strcmp(plot_style, "impulses") &&
391  strcmp(plot_style, "dots") &&
392  strcmp(plot_style, "steps") &&
393  strcmp(plot_style, "errorbars") &&
394  strcmp(plot_style, "boxes") &&
395  strcmp(plot_style, "boxerrorbars") &&
396  strcmp(plot_style, "points palette")) {
397  fprintf(stderr, "warning: unknown requested style: using points\n") ;
398  strcpy(h->pstyle, "points") ;
399  } else {
400  strcpy(h->pstyle, plot_style) ;
401  }
402  return ;
403 }
404 
405 /*-------------------------------------------------------------------------*/
406 /**
407  @brief Change the plotting axes of a gnuplot session (2d only).
408  @param h Gnuplot session control handle
409  @param plot_axes Plotting-axes to use (character string)
410  @return void
411 
412  The provided plotting axes is a character string. It must be one of
413  the following:
414 
415  - x1y1
416  - x1y2
417  - x2y1
418  - x2y2
419  */
420 /*--------------------------------------------------------------------------*/
421 
422 void gnuplot_setaxes(gnuplot_ctrl * h, char * plot_axes)
423 {
424  if (strcmp(plot_axes, "x1y1") &&
425  strcmp(plot_axes, "x1y2") &&
426  strcmp(plot_axes, "x2y1") &&
427  strcmp(plot_axes, "x2y2")) {
428  fprintf(stderr, "warning: unknown requested axes: using default\n") ;
429  sprintf(h->axes, " ") ;
430  } else {
431  sprintf(h->axes, "axes %s", plot_axes) ;
432  }
433  return ;
434 }
435 
436 
437 /*-------------------------------------------------------------------------*/
438 /**
439  @brief Change the terminal of a gnuplot session.
440  @param h Gnuplot session control handle
441  @param terminal Terminal name (character string)
442  @return void
443 
444  No attempt is made to check the validity of the terminal name. This function
445  simply makes a note of it and calls gnuplot_cmd to change the name
446  */
447 /*--------------------------------------------------------------------------*/
448 
449 void gnuplot_setterm(gnuplot_ctrl * h, char * terminal)
450 {
451  char cmd[64];
452  strncpy(h->term, terminal,32);
453  h->term[31]=0;
454  sprintf(cmd,"set terminal %s",h->term);
455  gnuplot_cmd(h,cmd);
456 }
457 
458 
459 /*-------------------------------------------------------------------------*/
460 /**
461  @brief Sets the x label of a gnuplot session.
462  @param h Gnuplot session control handle.
463  @param label Character string to use for X label.
464  @return void
465 
466  Sets the x label for a gnuplot session.
467  */
468 /*--------------------------------------------------------------------------*/
469 
470 void gnuplot_set_xlabel(gnuplot_ctrl * h, char * label)
471 {
472  char cmd[GP_CMD_SIZE] ;
473 
474  sprintf(cmd, "set xlabel \"%s\"", label) ;
475  gnuplot_cmd(h, cmd) ;
476  return ;
477 }
478 
479 
480 /*-------------------------------------------------------------------------*/
481 /**
482  @brief Sets the y label of a gnuplot session.
483  @param h Gnuplot session control handle.
484  @param label Character string to use for Y label.
485  @return void
486 
487  Sets the y label for a gnuplot session.
488  */
489 /*--------------------------------------------------------------------------*/
490 
491 void gnuplot_set_ylabel(gnuplot_ctrl * h, char * label)
492 {
493  char cmd[GP_CMD_SIZE] ;
494 
495  sprintf(cmd, "set ylabel \"%s\"", label) ;
496  gnuplot_cmd(h, cmd) ;
497  return ;
498 }
499 
500 /*-------------------------------------------------------------------------*/
501 /**
502  @brief Sets the z label of a gnuplot session.
503  @param h Gnuplot session control handle.
504  @param label Character string to use for Z label.
505  @return void
506 
507  Sets the z label for a gnuplot session. (Added by Robert Bradley 12 October 2004)
508  */
509 /*--------------------------------------------------------------------------*/
510 
511 void gnuplot_set_zlabel(gnuplot_ctrl * h, char * label)
512 {
513  char cmd[GP_CMD_SIZE] ;
514 
515  sprintf(cmd, "set zlabel \"%s\"", label) ;
516  gnuplot_cmd(h, cmd) ;
517  return ;
518 }
519 
520 /*-------------------------------------------------------------------------*/
521 /**
522  @brief Resets a gnuplot session (next plot will erase previous ones).
523  @param h Gnuplot session control handle.
524  @return void
525 
526  Resets a gnuplot session, i.e. the next plot will erase all previous
527  ones.
528  */
529 /*--------------------------------------------------------------------------*/
530 
532 {
533  int i ;
534  if (h->ntmp) {
535  for (i=0 ; i<h->ntmp ; i++) {
536  remove(h->to_delete[i]) ;
537  }
538  }
539  h->ntmp = 0 ;
540  h->nplots = 0 ;
541  return ;
542 }
543 
544 
545 
546 /*-------------------------------------------------------------------------*/
547 /**
548  @brief Plots a 2d graph from a list of doubles.
549  @param handle Gnuplot session control handle.
550  @param d Array of doubles.
551  @param n Number of values in the passed array.
552  @param title Title of the plot.
553  @return void
554 
555  Plots out a 2d graph from a list of doubles. The x-coordinate is the
556  index of the double in the list, the y coordinate is the double in
557  the list.
558 
559  Example:
560 
561  @code
562  gnuplot_ctrl *h ;
563  double d[50] ;
564  int i ;
565 
566  h = gnuplot_init() ;
567  for (i=0 ; i<50 ; i++) {
568  d[i] = (double)(i*i) ;
569  }
570  gnuplot_plot_x(h, d, 50, "parabola") ;
571  sleep(2) ;
572  gnuplot_close(h) ;
573  @endcode
574  */
575 /*--------------------------------------------------------------------------*/
576 
578  gnuplot_ctrl * handle,
579  double * d,
580  int n,
581  char * title
582 )
583 {
584  int i ;
585  int tmpfd ;
586  char name[128] ;
587  char cmd[GP_CMD_SIZE] ;
588  char line[GP_CMD_SIZE] ;
589  int ret ;
590 
591 
592  if (handle==NULL || d==NULL || (n<1)) return ;
593 
594  /* Open one more temporary file? */
595  if (handle->ntmp == GP_MAX_TMP_FILES - 1) {
596  fprintf(stderr,
597  "maximum # of temporary files reached (%d): cannot open more",
599  return ;
600  }
601 
602  /* Open temporary file for output */
603  sprintf(name, GNUPLOT_TEMPFILE, P_tmpdir);
604  if ((tmpfd=mkstemp(name))==-1) {
605  fprintf(stderr,"cannot create temporary file: exiting plot") ;
606  return ;
607  }
608 
609  /* Store file name in array for future deletion */
610  strcpy(handle->to_delete[handle->ntmp], name) ;
611  handle->ntmp ++ ;
612  /* Write data to this file */
613  for (i=0 ; i<n ; i++) {
614  sprintf(line, "%g\n", d[i]);
615  ret = write(tmpfd, line, strlen(line));
616  if(ret != (int)strlen(line)) {
617  if(ret < 0) {
618  fprintf(stderr,"error writing to temp file\n");
619  }
620  else {
621  fprintf(stderr,"could not complete write %d of %d bytes written\n", ret, (int)strlen(line));
622  }
623  close(tmpfd);
624  return;
625  }
626  }
627  close(tmpfd) ;
628 
629  /* Command to be sent to gnuplot */
630  if (handle->nplots > 0) {
631  strcpy(cmd, "replot") ;
632  } else {
633  strcpy(cmd, "plot") ;
634  }
635 
636  if (title == NULL) {
637  sprintf(line, "%s \"%s\" %s with %s", cmd, name, handle->axes,
638  handle->pstyle) ;
639  } else {
640  sprintf(line, "%s \"%s\" %s title \"%s\" with %s", cmd, name,
641  handle->axes, title, handle->pstyle) ;
642  }
643 
644  /* send command to gnuplot */
645  gnuplot_cmd(handle, line) ;
646  handle->nplots++ ;
647  return ;
648 }
649 
650 
651 
652 /*-------------------------------------------------------------------------*/
653 /**
654  @brief Plot a 2d graph from a list of points.
655  @param handle Gnuplot session control handle.
656  @param x Pointer to a list of x coordinates.
657  @param y Pointer to a list of y coordinates.
658  @param n Number of doubles in x (assumed the same as in y).
659  @param title Title of the plot.
660  @return void
661 
662  Plots out a 2d graph from a list of points. Provide points through a list
663  of x and a list of y coordinates. Both provided arrays are assumed to
664  contain the same number of values.
665 
666  @code
667  gnuplot_ctrl *h ;
668  double x[50] ;
669  double y[50] ;
670  int i ;
671 
672  h = gnuplot_init() ;
673  for (i=0 ; i<50 ; i++) {
674  x[i] = (double)(i)/10.0 ;
675  y[i] = x[i] * x[i] ;
676  }
677  gnuplot_plot_xy(h, x, y, 50, "parabola") ;
678  sleep(2) ;
679  gnuplot_close(h) ;
680  @endcode
681  */
682 /*--------------------------------------------------------------------------*/
683 
685  gnuplot_ctrl * handle,
686  double * x,
687  double * y,
688  int n,
689  char * title
690 )
691 {
692  int i ;
693  int tmpfd ;
694  char name[128] ;
695  char cmd[GP_CMD_SIZE] ;
696  char line[GP_CMD_SIZE] ;
697  int ret ;
698 
699  if (handle==NULL || x==NULL || y==NULL || (n<1)) return ;
700 
701  /* Open one more temporary file? */
702  if (handle->ntmp == GP_MAX_TMP_FILES - 1) {
703  fprintf(stderr,
704  "maximum # of temporary files reached (%d): cannot open more",
706  return ;
707  }
708 
709  /* Open temporary file for output */
710  sprintf(name, GNUPLOT_TEMPFILE, P_tmpdir);
711  if ((tmpfd=mkstemp(name))==-1) {
712  fprintf(stderr,"cannot create temporary file: exiting plot") ;
713  return ;
714  }
715  /* Store file name in array for future deletion */
716  strcpy(handle->to_delete[handle->ntmp], name) ;
717  handle->ntmp ++ ;
718 
719  /* Write data to this file */
720  for (i=0 ; i<n; i++) {
721  sprintf(line, "%g %g\n", x[i], y[i]) ;
722  ret = write(tmpfd, line, strlen(line));
723  if(ret != (int)strlen(line)) {
724  if(ret < 0) {
725  fprintf(stderr,"error writing to temp file:\n");
726  }
727  else {
728  fprintf(stderr,"could not complete write %d of %d bytes written\n", ret, (int)strlen(line));
729  }
730  close(tmpfd);
731  return;
732  }
733  }
734  close(tmpfd) ;
735 
736  /* Command to be sent to gnuplot */
737  if (handle->nplots > 0) {
738  strcpy(cmd, "replot") ;
739  } else {
740  strcpy(cmd, "plot") ;
741  }
742 
743  if (title == NULL) {
744  sprintf(line, "%s \"%s\" %s with %s", cmd, name,
745  handle->axes, handle->pstyle) ;
746  } else {
747  sprintf(line, "%s \"%s\" %s title \"%s\" with %s", cmd, name,
748  handle->axes, title, handle->pstyle) ;
749  }
750 
751  /* send command to gnuplot */
752  gnuplot_cmd(handle, line) ;
753  handle->nplots++ ;
754  return ;
755 }
756 
757 /*-------------------------------------------------------------------------*/
758 /**
759  @brief Plot a 3d graph from a list of points.
760  @param handle Gnuplot session control handle.
761  @param x Pointer to a list of x coordinates.
762  @param y Pointer to a list of y coordinates.
763  @param z Pointer to a list of z coordinates.
764  @param n Number of doubles in x (y and z must be the the same).
765  @param title Title of the plot.
766  @return void
767 
768  gnuplot_splot(handle,x,y,z,n,title);
769  Based on gnuplot_plot_xy, modifications by Robert Bradley 12/10/2004
770 
771  Plots a 3d graph from a list of points, passed in the form of three arrays x, y and z. All arrays are assumed to
772  contain the same number of values.
773 
774  @code
775  gnuplot_ctrl *h ;
776  double x[50] ; y[50] ; z[50];
777  int i ;
778 
779  h = gnuplot_init() ;
780  for (i=0 ; i<50 ; i++) {
781  x[i] = (double)(i)/10.0 ;
782  y[i] = x[i] * x[i] ;
783  z[i] = x[i] * x[i]/2.0;
784  }
785  gnuplot_splot(h, x, y, z, 50, "parabola") ;
786  sleep(2) ;
787  gnuplot_close(h) ;
788  @endcode
789  */
790 /*--------------------------------------------------------------------------*/
791 
792 int gnuplot_splot(gnuplot_ctrl *handle, double *x, double *y, double *z, int n, char *title) {
793  int i;
794  int tmpfd ;
795  char name[128] ;
796  char cmd[GP_CMD_SIZE] ;
797  char line[GP_CMD_SIZE] ;
798  int ret ;
799 
800  if (handle==NULL || x==NULL || y==NULL || (n<1)) return 1;
801  if (handle->nplots > 0)
802  return 1;
803  /* Open one more temporary file? */
804  if (handle->ntmp == GP_MAX_TMP_FILES - 1) {
805  fprintf(stderr,
806  "maximum # of temporary files reached (%d): cannot open more",
808  return 1;
809  }
810 
811  /* Open temporary file for output */
812  sprintf(name, GNUPLOT_TEMPFILE, P_tmpdir);
813  if ((tmpfd=mkstemp(name))==-1) {
814  fprintf(stderr,"cannot create temporary file: exiting plot") ;
815  return 1;
816  }
817  /* Store file name in array for future deletion */
818  strcpy(handle->to_delete[handle->ntmp], name) ;
819  handle->ntmp ++ ;
820 
821  /* Write data to this file */
822  for (i=0 ; i<n; i++) {
823  sprintf(line, "%g %g %g\n", x[i], y[i], z[i]) ;
824  ret = write(tmpfd, line, strlen(line));
825  if(ret != (int)strlen(line)) {
826  if(ret < 0) {
827  fprintf(stderr,"error writing to temp file\n");
828  }
829  else {
830  fprintf(stderr,"could not complete write %d of %d bytes written\n", ret, (int)strlen(line));
831  }
832  close(tmpfd);
833  return 1;
834  }
835  }
836  close(tmpfd) ;
837 
838  /* Command to be sent to gnuplot */
839  strcpy(cmd, "splot") ;
840 
841  if (title == NULL) {
842  sprintf(line, "%s \"%s\" with %s", cmd, name, handle->pstyle) ;
843  } else {
844  sprintf(line, "%s \"%s\" title \"%s\" with %s", cmd, name,
845  title, handle->pstyle) ;
846  }
847 
848  /* send command to gnuplot */
849  gnuplot_cmd(handle, line) ;
850  handle->nplots++ ;
851  return 0;
852 }
853 
854 /*-------------------------------------------------------------------------*/
855 /**
856  @brief Plot a 3d graph from a grid of points.
857  @param handle Gnuplot session control handle.
858  @param points Pointer to a grid of points (rows,cols).
859  @param rows Number of rows (y points).
860  @param cols Number of columns (x points).
861  @param title Title of the plot.
862  @return void
863 
864  gnuplot_splot_grid(handle,(double*) points,rows,cols,title);
865  Based on gnuplot_splot, modifications by Robert Bradley 2/4/2006
866 
867  Plots a 3d graph from a grid of points, passed in the form of a 2D array [x,y].
868  */
869 /*--------------------------------------------------------------------------*/
870 
871 int gnuplot_splot_grid(gnuplot_ctrl *handle, double *points, int rows, int cols, char *title) {
872  int i;
873  int j;
874  int tmpfd ;
875  char name[128] ;
876  char cmd[GP_CMD_SIZE] ;
877  char line[GP_CMD_SIZE] ;
878  int ret;
879 
880  if (handle==NULL || points==NULL || (rows<1) || (cols<1)) return 1;
881  if (handle->nplots > 0)
882  return 1;
883  /* Open one more temporary file? */
884  if (handle->ntmp == GP_MAX_TMP_FILES - 1) {
885  fprintf(stderr,
886  "maximum # of temporary files reached (%d): cannot open more",
888  return 1;
889  }
890 
891  /* Open temporary file for output */
892  sprintf(name, GNUPLOT_TEMPFILE, P_tmpdir);
893  if ((tmpfd=mkstemp(name))==-1) {
894  fprintf(stderr,"cannot create temporary file: exiting plot") ;
895  return 1;
896  }
897  /* Store file name in array for future deletion */
898  strcpy(handle->to_delete[handle->ntmp], name) ;
899  handle->ntmp ++ ;
900 
901  /* Write data to this file */
902  for (i=0 ; i<rows; i++) {
903  for (j=0;j<cols;j++) {
904  sprintf(line, "%d %d %g\n", i,j,points[i*cols+j]) ;
905  ret = write(tmpfd, line, strlen(line));
906  if(ret != (int)strlen(line)) {
907  if(ret < 0) {
908  fprintf(stderr,"error writing to file temp file\n");
909  }
910  else {
911  fprintf(stderr,"could not complete write %d of %d bytes written\n", ret, (int)strlen(line));
912  }
913  close(tmpfd);
914  return 1;
915  }
916  }
917  strcpy(line,"\n");
918  ret = write(tmpfd, line, strlen(line));
919  if(ret != (int)strlen(line)) {
920  if(ret < 0) {
921  fprintf(stderr,"error writing to temp file\n");
922  }
923  else {
924  fprintf(stderr,"could not complete write %d of %d bytes written\n", ret, (int)strlen(line));
925  }
926  close(tmpfd);
927  return 1;
928  }
929  }
930  close(tmpfd) ;
931 
932  /* Command to be sent to gnuplot */
933  strcpy(cmd, "splot") ;
934 
935  if (title == NULL) {
936  sprintf(line, "%s \"%s\" with %s", cmd, name, handle->pstyle) ;
937  } else {
938  sprintf(line, "%s \"%s\" title \"%s\" with %s", cmd, name,
939  title, handle->pstyle) ;
940  }
941 
942  /* send command to gnuplot */
943  gnuplot_cmd(handle, line) ;
944  handle->nplots++ ;
945  return 0;
946 }
947 
948 /*-------------------------------------------------------------------------*/
949 /**
950  @brief Plot contours from a list of points.
951  @param handle Gnuplot session control handle.
952  @param x Pointer to a list of x coordinates. (length=nx*ny)
953  @param y Pointer to a list of y coordinates. (length=nx*ny)
954  @param z Pointer to a list of z coordinates. (length=nx*ny)
955  @param nx Number of doubles in x-direction
956  @param ny Number of doubles in y-direction
957  @param title Title of the plot.
958  @return void
959 
960  gnuplot_contour_plot(handle,x,y,z,n,title);
961  Based on gnuplot_splot, modifications by Robert Bradley 23/11/2005
962 
963  Plots a contour plot from a list of points, passed in the form of three arrays x, y and z.
964 
965  @code
966  gnuplot_ctrl *h ;
967  double x[50] ; y[50] ; z[50];
968  int i ;
969 
970  h = gnuplot_init() ;
971  int count=100;
972  double x[count*count],y[count*count],z[count*count];
973  int i,j;
974  for (i=0;i<count;i++) {
975  for (j=0;j<count;j++) {
976  x[count*i+j]=i;
977  y[count*i+j]=j;
978  z[count*i+j]=1000*sqrt(square(i-count/2)+square(j-count/2));
979  }
980  }
981  gnuplot_setstyle(h,"lines");
982  gnuplot_contour_plot(h,x,y,z,count,count,"Points");
983  sleep(2) ;
984  gnuplot_close(h) ;
985  @endcode
986  */
987 /*--------------------------------------------------------------------------*/
988 
989 int gnuplot_contour_plot(gnuplot_ctrl *handle, double *x, double *y, double *z, int nx, int ny, char *title) {
990  int i,j;
991  int tmpfd ;
992  char name[128] ;
993  char cmd[GP_CMD_SIZE] ;
994  char line[GP_CMD_SIZE] ;
995  int ret ;
996 
997  if (handle==NULL || x==NULL || y==NULL || (nx<1) || (ny<1)) return 1;
998  if (handle->nplots > 0)
999  return 1;
1000  /* Open one more temporary file? */
1001  if (handle->ntmp == GP_MAX_TMP_FILES - 1) {
1002  fprintf(stderr,
1003  "maximum # of temporary files reached (%d): cannot open more",
1004  GP_MAX_TMP_FILES) ;
1005  return 1;
1006  }
1007 
1008  /* Open temporary file for output */
1009  sprintf(name, GNUPLOT_TEMPFILE, P_tmpdir);
1010  if ((tmpfd=mkstemp(name))==-1) {
1011  fprintf(stderr,"cannot create temporary file: exiting plot") ;
1012  return 1;
1013  }
1014  /* Store file name in array for future deletion */
1015  strcpy(handle->to_delete[handle->ntmp], name) ;
1016  handle->ntmp ++ ;
1017 
1018  /* Write data to this file */
1019 
1020  for (i=0 ; i<nx; i++) {
1021  for (j=0 ; j<ny; j++) {
1022  sprintf(line, "%g %g %g\n", x[nx*i+j], y[nx*i+j], z[nx*i+j]) ;
1023  ret = write(tmpfd, line, strlen(line));
1024  if(ret != (int)strlen(line)) {
1025  if(ret < 0) {
1026  fprintf(stderr,"error writing to temp file\n");
1027  }
1028  else {
1029  fprintf(stderr,"could not complete write %d of %d bytes written\n", ret, (int)strlen(line));
1030  }
1031  close(tmpfd);
1032  return 1;
1033  }
1034  }
1035  sprintf(line, "\n") ;
1036  ret = write(tmpfd, line, strlen(line));
1037  if(ret != (int)strlen(line)) {
1038  if(ret < 0) {
1039  fprintf(stderr,"error writing to temp file\n");
1040  }
1041  else {
1042  fprintf(stderr,"could not complete write %d of %d bytes written\n", ret, (int)strlen(line));
1043  }
1044  close(tmpfd);
1045  return 1;
1046  }
1047  }
1048  close(tmpfd) ;
1049 
1050  /* Command to be sent to gnuplot */
1051  gnuplot_cmd(handle,"unset surface");
1052  gnuplot_cmd(handle,"set contour base");
1053  gnuplot_cmd(handle,"set view map");
1054  gnuplot_cmd(handle,"set view 0,0");
1055  strcpy(cmd, "splot") ;
1056 
1057  if (title == NULL) {
1058  sprintf(line, "%s \"%s\" with %s", cmd, name, handle->pstyle) ;
1059  } else {
1060  sprintf(line, "%s \"%s\" title \"%s\" with %s", cmd, name,
1061  title, handle->pstyle) ;
1062  }
1063 
1064  /* send command to gnuplot */
1065  gnuplot_cmd(handle, line) ;
1066  handle->nplots++ ;
1067  return 0;
1068 }
1069 
1070 /*-------------------------------------------------------------------------*/
1071 /**
1072  @brief Plot a 3d graph using callback functions to return the points
1073  @param handle Gnuplot session control handle.
1074  @param obj Pointer to an arbitrary object.
1075  @param getPoint Pointer to a callback function.
1076  @param n Number of doubles in x (y and z must be the the same).
1077  @param title Title of the plot.
1078  @return void
1079 
1080 Calback:
1081 
1082 void getPoint(void *object,gnuplot_point *point,int index,int pointCount);
1083  @param obj Pointer to an arbitrary object
1084  @param point Pointer to the returned point struct (double x,y,z)
1085  @param i Index of the current point (0 to n-1)
1086  @param n Number of points
1087  @return void
1088  */
1089 /*--------------------------------------------------------------------------*/
1090 
1092  void *obj,
1093  void (*getPoint)(void*,gnuplot_point*,int,int),
1094  int n,
1095  char *title) {
1096  int i;
1097  int tmpfd ;
1098  char name[128] ;
1099  char cmd[GP_CMD_SIZE] ;
1100  char line[GP_CMD_SIZE] ;
1101  int ret ;
1102 
1103  if (handle==NULL || getPoint==NULL || (n<1)) return 1;
1104  if (handle->nplots > 0)
1105  return 1;
1106  /* Open one more temporary file? */
1107  if (handle->ntmp == GP_MAX_TMP_FILES - 1) {
1108  fprintf(stderr,
1109  "maximum # of temporary files reached (%d): cannot open more",
1110  GP_MAX_TMP_FILES) ;
1111  return 1;
1112  }
1113 
1114  /* Open temporary file for output */
1115  sprintf(name, GNUPLOT_TEMPFILE, P_tmpdir);
1116  if ((tmpfd=mkstemp(name))==-1) {
1117  fprintf(stderr,"cannot create temporary file: exiting plot") ;
1118  return 1;
1119  }
1120  /* Store file name in array for future deletion */
1121  strcpy(handle->to_delete[handle->ntmp], name) ;
1122  handle->ntmp ++ ;
1123 
1124  /* Write data to this file */
1125  gnuplot_point point;
1126  for (i=0;i<n;i++) {
1127  getPoint(obj,&point,i,n);
1128  sprintf(line, "%g %g %g\n", point.x, point.y, point.z) ;
1129  ret = write(tmpfd, line, strlen(line));
1130  if(ret != (int)strlen(line)) {
1131  if(ret < 0) {
1132  fprintf(stderr,"error writing to temp file\n");
1133  }
1134  else {
1135  fprintf(stderr,"could not complete write %d of %d bytes written\n", ret, (int)strlen(line));
1136  }
1137  close(tmpfd);
1138  return 1;
1139  }
1140  }
1141  close(tmpfd) ;
1142 
1143  /* Command to be sent to gnuplot */
1144  strcpy(cmd, "splot") ;
1145 
1146  if (title == NULL) {
1147  sprintf(line, "%s \"%s\" with %s", cmd, name, handle->pstyle) ;
1148  } else {
1149  sprintf(line, "%s \"%s\" title \"%s\" with %s", cmd, name,
1150  title, handle->pstyle) ;
1151  }
1152 
1153  /* send command to gnuplot */
1154  gnuplot_cmd(handle, line) ;
1155  handle->nplots++ ;
1156  return 0;
1157 }
1158 
1159 /*
1160  @brief Plot a 2d graph using a callback function to return points.
1161  @param handle Gnuplot session control handle.
1162  @param obj Pointer to an arbitrary object.
1163  @param getPoint Pointer to a callback function.
1164  @param n Number of points.
1165  @param title Title of the plot.
1166  @return void
1167 
1168 The callback function is of the following form, and is called once for each
1169 point plotted:
1170 
1171 void getPoint(void *object,gnuplot_point *point,int index,int pointCount);
1172  @param obj Pointer to an arbitrary object
1173  @param point Pointer to the returned point struct (double x,y,z)
1174  @param i Index of the current point (0 to n-1)
1175  @param n Number of points
1176  @return void
1177 
1178 For example (where points is an array of points in this case):
1179 
1180 void PlotPoint(void *obj,gnuplot_point *point,int i, int n) {
1181  Point *p=(Point*) obj;
1182  point->x=p[i].x;
1183  point->y=p[i].y;
1184 }
1185 
1186 int main(int argc, char *argv[]) {
1187 ...
1188 gnuplot_plot_obj_xy(gp,points,PlotPoint,pCount,"Points");
1189 ...
1190 }
1191 
1192 Alternately, PlotPoint could return values based on a complex formula and many
1193 sources of information. For example, it could be used to perform a Discrete
1194 Fourier Transform on an array of complex numbers, calculating one transformed
1195 point per call.
1196 
1197 Note: you should always explicitly set all the relevant parts of the struct.
1198 However, you may ignore the z component for 2D plots.
1199 */
1200 
1202  gnuplot_ctrl *handle,
1203  void *obj,
1204  void (*getPoint)(void*,gnuplot_point*,int,int),
1205  int n,
1206  char *title
1207 )
1208 {
1209  int i ;
1210  int tmpfd ;
1211  char name[128] ;
1212  char cmd[GP_CMD_SIZE] ;
1213  char line[GP_CMD_SIZE] ;
1214  int ret ;
1215 
1216  if (handle==NULL || getPoint==NULL || (n<1)) return 1;
1217 
1218  /* Open one more temporary file? */
1219  if (handle->ntmp == GP_MAX_TMP_FILES - 1) {
1220  fprintf(stderr,
1221  "maximum # of temporary files reached (%d): cannot open more",
1222  GP_MAX_TMP_FILES) ;
1223  return 1;
1224  }
1225 
1226  /* Open temporary file for output */
1227  sprintf(name, GNUPLOT_TEMPFILE, P_tmpdir);
1228  if ((tmpfd=mkstemp(name))==-1) {
1229  fprintf(stderr,"cannot create temporary file: exiting plot") ;
1230  return 1;
1231  }
1232  /* Store file name in array for future deletion */
1233  strcpy(handle->to_delete[handle->ntmp], name) ;
1234  handle->ntmp ++ ;
1235 
1236  /* Write data to this file */
1237  gnuplot_point point;
1238  for (i=0 ; i<n; i++) {
1239  getPoint(obj,&point,i,n);
1240  sprintf(line, "%g %g\n", point.x, point.y) ;
1241  ret = write(tmpfd, line, strlen(line));
1242  if(ret != (int)strlen(line)) {
1243  if(ret < 0) {
1244  fprintf(stderr,"error writing to temp file\n");
1245  }
1246  else {
1247  fprintf(stderr,"could not complete write %d of %d bytes written\n", ret, (int)strlen(line));
1248  }
1249  close(tmpfd);
1250  return 1;
1251  }
1252  }
1253  close(tmpfd) ;
1254 
1255  /* Command to be sent to gnuplot */
1256  if (handle->nplots > 0) {
1257  strcpy(cmd, "replot") ;
1258  } else {
1259  strcpy(cmd, "plot") ;
1260  }
1261 
1262  if (title == NULL) {
1263  sprintf(line, "%s \"%s\" %s with %s", cmd, name,
1264  handle->axes, handle->pstyle) ;
1265  } else {
1266  sprintf(line, "%s \"%s\" %s title \"%s\" with %s", cmd, name,
1267  handle->axes, title, handle->pstyle) ;
1268  }
1269 
1270  /* send command to gnuplot */
1271  gnuplot_cmd(handle, line) ;
1272  handle->nplots++ ;
1273  return 0;
1274 }
1275 
1276 /*-------------------------------------------------------------------------*/
1277 /**
1278  @brief Open a new session, plot a signal, close the session.
1279  @param title Plot title
1280  @param style Plot style
1281  @param label_x Label for X
1282  @param label_y Label for Y
1283  @param x Array of X coordinates
1284  @param y Array of Y coordinates (can be NULL)
1285  @param n Number of values in x and y.
1286  @return
1287 
1288  This function opens a new gnuplot session, plots the provided
1289  signal as an X or XY signal depending on a provided y, waits for
1290  a carriage return on stdin and closes the session.
1291 
1292  It is Ok to provide an empty title, empty style, or empty labels for
1293  X and Y. Defaults are provided in this case.
1294  */
1295 /*--------------------------------------------------------------------------*/
1296 
1298  char * title,
1299  char * style,
1300  char * label_x,
1301  char * label_y,
1302  double * x,
1303  double * y,
1304  int n
1305 )
1306 {
1307  gnuplot_ctrl * handle ;
1308 
1309  if (x==NULL || n<1) return ;
1310 
1311  if ((handle = gnuplot_init()) == NULL) return ;
1312  if (style!=NULL) {
1313  gnuplot_setstyle(handle, style);
1314  } else {
1315  gnuplot_setstyle(handle, "lines");
1316  }
1317  if (label_x!=NULL) {
1318  gnuplot_set_xlabel(handle, label_x);
1319  } else {
1320  gnuplot_set_xlabel(handle, "X");
1321  }
1322  if (label_y!=NULL) {
1323  gnuplot_set_ylabel(handle, label_y);
1324  } else {
1325  gnuplot_set_ylabel(handle, "Y");
1326  }
1327  if (y==NULL) {
1328  gnuplot_plot_x(handle, x, n, title);
1329  } else {
1330  gnuplot_plot_xy(handle, x, y, n, title);
1331  }
1332  printf("press ENTER to continue\n");
1333  while (getchar()!='\n') {}
1334  gnuplot_close(handle);
1335  return ;
1336 }
1337 
1338 
1339 
1340 
1341 /*-------------------------------------------------------------------------*/
1342 /**
1343  @brief Plot a slope on a gnuplot session.
1344  @param handle Gnuplot session control handle.
1345  @param a Slope.
1346  @param b Intercept.
1347  @param title Title of the plot.
1348  @return void
1349 
1350  Plot a slope on a gnuplot session. The provided slope has an
1351  equation of the form y=ax+b
1352 
1353  Example:
1354 
1355  @code
1356  gnuplot_ctrl * h ;
1357  double a, b ;
1358 
1359  h = gnuplot_init() ;
1360  gnuplot_plot_slope(h, 1.0, 0.0, "unity slope") ;
1361  sleep(2) ;
1362  gnuplot_close(h) ;
1363  @endcode
1364  */
1365 /*--------------------------------------------------------------------------*/
1366 
1367 
1369  gnuplot_ctrl * handle,
1370  double a,
1371  double b,
1372  char * title
1373 )
1374 {
1375  char stitle[GP_TITLE_SIZE] ;
1376  char cmd[GP_CMD_SIZE] ;
1377 
1378  if (title == NULL) {
1379  strcpy(stitle, "no title") ;
1380  } else {
1381  strcpy(stitle, title) ;
1382  }
1383 
1384  if (handle->nplots > 0) {
1385  sprintf(cmd, "replot %g * x + %g title \"%s\" with %s",
1386  a, b, title, handle->pstyle) ;
1387  } else {
1388  sprintf(cmd, "plot %g * x + %g title \"%s\" with %s",
1389  a, b, title, handle->pstyle) ;
1390  }
1391  gnuplot_cmd(handle, cmd) ;
1392  handle->nplots++ ;
1393  return ;
1394 }
1395 
1396 
1397 /*-------------------------------------------------------------------------*/
1398 /**
1399  @brief Plot a curve of given equation y=f(x).
1400  @param h Gnuplot session control handle.
1401  @param equation Equation to plot.
1402  @param title Title of the plot.
1403  @return void
1404 
1405  Plots out a curve of given equation. The general form of the
1406  equation is y=f(x), you only provide the f(x) side of the equation.
1407 
1408  Example:
1409 
1410  @code
1411  gnuplot_ctrl *h ;
1412  char eq[80] ;
1413 
1414  h = gnuplot_init() ;
1415  strcpy(eq, "sin(x) * cos(2*x)") ;
1416  gnuplot_plot_equation(h, eq, "sine wave", normal) ;
1417  gnuplot_close(h) ;
1418  @endcode
1419  */
1420 /*--------------------------------------------------------------------------*/
1421 
1423  gnuplot_ctrl * h,
1424  char * equation,
1425  char * title
1426 )
1427 {
1428  char cmd[GP_CMD_SIZE];
1429  char plot_str[GP_EQ_SIZE] ;
1430  char title_str[GP_TITLE_SIZE] ;
1431 
1432  if (title == NULL) {
1433  strcpy(title_str, "no title") ;
1434  } else {
1435  strcpy(title_str, title) ;
1436  }
1437  if (h->nplots > 0) {
1438  strcpy(plot_str, "replot") ;
1439  } else {
1440  strcpy(plot_str, "plot") ;
1441  }
1442 
1443  sprintf(cmd, "%s %s title \"%s\" with %s",
1444  plot_str, equation, title_str, h->pstyle) ;
1445  gnuplot_cmd(h, cmd) ;
1446  h->nplots++ ;
1447  return ;
1448 }
1449 
1450 /*-------------------------------------------------------------------------*/
1451 /**
1452  @brief Save a graph as a postscript file on the hard disk
1453  @param h Gnuplot session control handle.
1454  @param filename Filename to save as
1455  @return void
1456 
1457  A wrapper for the gnuplot_cmd function that sets the terminal
1458  the terminal to postscript, replots the graph and then resets
1459  the terminal to x11.
1460 
1461  Example:
1462 
1463  @code
1464  gnuplot_ctrl *h ;
1465  char eq[80] ;
1466 
1467  h = gnuplot_init() ;
1468  strcpy(eq, "sin(x) * cos(2*x)") ;
1469  gnuplot_plot_equation(h, eq, "sine wave", normal) ;
1470  gnuplot_hardcopy(h, "sinewave.ps");
1471  gnuplot_close(h) ;
1472  @endcode
1473  */
1474 /*--------------------------------------------------------------------------*/
1476  gnuplot_ctrl * h,
1477  char * filename
1478 )
1479 {
1480  gnuplot_cmd(h, "set terminal postscript");
1481 
1482  gnuplot_cmd(h, "set output \"%s\"", filename);
1483  gnuplot_cmd(h, "replot");
1484 
1485  gnuplot_cmd(h, "set terminal %s",h->term);
1486 }
1487 
1488 /*-------------------------------------------------------------------------*/
1489 /**
1490  @brief Save a graph in colour as a postscript file on the hard disk
1491  @param h Gnuplot session control handle.
1492  @param filename Filename to save as
1493  @return void
1494 
1495  Added by Robert Bradley on 12th october 2004
1496 
1497  This function is identical in syntax (and implementation) to gnuplot_hardcopy, but the resulting
1498  PostScript file retains colour information.
1499 
1500  */
1501 /*--------------------------------------------------------------------------*/
1502 
1504  gnuplot_ctrl * h,
1505  char * filename
1506 )
1507 {
1508  gnuplot_cmd(h, "set terminal postscript enhanced color");
1509 
1510  gnuplot_cmd(h, "set output \"%s\"", filename);
1511  gnuplot_cmd(h, "replot");
1512 
1513  gnuplot_cmd(h, "set terminal %s",h->term);
1514 }
1515 
1516 #endif