UFJF - Machine Learning Toolkit  0.51.8
gnuplot_i.hpp
1 
31 
32 #ifndef _GNUPLOT_PIPES_H_
33 #define _GNUPLOT_PIPES_H_
34 
35 
36 #include <iostream>
37 #include <string>
38 #include <vector>
39 #include <fstream>
40 #include <sstream> // for std::ostringstream
41 #include <stdexcept>
42 #include <cstdio>
43 #include <cstdlib> // for getenv()
44 #include <list> // for std::list
45 
46 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
47 //defined for 32 and 64-bit environments
48  #include <io.h> // for _access(), _mktemp()
49  #define GP_MAX_TMP_FILES 27 // 27 temporary files it's Microsoft restriction
50 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
51 //all UNIX-like OSs (Linux, *BSD, MacOSX, Solaris, ...)
52 #include <unistd.h> // for access(), mkstemp()
53 #define GP_MAX_TMP_FILES 64
54 #else
55 #error unsupported or unknown operating system
56 #endif
57 
58 //declare classes in global namespace
59 
60 
61 class GnuplotException : public std::runtime_error
62 {
63 public:
64  GnuplotException(const std::string &msg) : std::runtime_error(msg){}
65 };
66 
67 
68 
69 class Gnuplot
70 {
71 private:
72 
73  //----------------------------------------------------------------------------------
74  // member data
76  FILE *gnucmd;
78  bool valid;
80  bool two_dim;
82  int nplots;
84  std::string pstyle;
86  std::string smooth;
88  std::vector<std::string> tmpfile_list;
89 
90  //----------------------------------------------------------------------------------
91  // static data
93  static int tmpfile_num;
95  static std::string m_sGNUPlotFileName;
97  static std::string m_sGNUPlotPath;
99  static std::string terminal_std;
100 
101  //----------------------------------------------------------------------------------
102  // member functions (auxiliary functions)
103  // ---------------------------------------------------
109  // ---------------------------------------------------
110  void init();
111  // ---------------------------------------------------
117  // ---------------------------------------------------
118  std::string create_tmpfile(std::ofstream &tmp);
119 
120  //----------------------------------------------------------------------------------
126  // ---------------------------------------------------------------------------------
127  static bool get_program_path();
128 
129  // ---------------------------------------------------------------------------------
136  // ---------------------------------------------------------------------------------
137  bool file_available(const std::string &filename);
138 
139  // ---------------------------------------------------------------------------------
146  // ---------------------------------------------------------------------------------
147  static bool file_exists(const std::string &filename, int mode=0);
148 
149 public:
150 
151  // ----------------------------------------------------------------------------
158  // ----------------------------------------------------------------------------
159  static bool set_GNUPlotPath(const std::string &path);
160 
161 
162  // ----------------------------------------------------------------------------
169  // ----------------------------------------------------------------------------
170  static void set_terminal_std(const std::string &type);
171 
172  //-----------------------------------------------------------------------------
173  // constructors
174  // ----------------------------------------------------------------------------
175 
176 
178  Gnuplot(const std::string &style = "points");
179 
181  Gnuplot(const std::vector<double> &x,
182  const std::string &title = "",
183  const std::string &style = "points",
184  const std::string &labelx = "x",
185  const std::string &labely = "y");
186 
188  Gnuplot(const std::vector<double> &x,
189  const std::vector<double> &y,
190  const std::string &title = "",
191  const std::string &style = "points",
192  const std::string &labelx = "x",
193  const std::string &labely = "y");
194 
196  Gnuplot(const std::vector<double> &x,
197  const std::vector<double> &y,
198  const std::vector<double> &z,
199  const std::string &title = "",
200  const std::string &style = "points",
201  const std::string &labelx = "x",
202  const std::string &labely = "y",
203  const std::string &labelz = "z");
204 
206  ~Gnuplot();
207 
208 
209  //----------------------------------------------------------------------------------
210 
212  Gnuplot& cmd(const std::string &cmdstr);
213  // ---------------------------------------------------------------------------------
220  // ---------------------------------------------------------------------------------
221  inline Gnuplot& operator<<(const std::string &cmdstr){
222  cmd(cmdstr);
223  return(*this);
224  }
225 
226 
227 
228  //----------------------------------------------------------------------------------
229  // show on screen or write to file
230 
232  Gnuplot& showonscreen(); // window output is set by default (win/x11/aqua)
233 
235  Gnuplot& savetops(const std::string &filename = "gnuplot_output");
236 
237 
238  //----------------------------------------------------------------------------------
239  // set and unset
240 
244  Gnuplot& set_style(const std::string &stylestr = "points");
245 
250  Gnuplot& set_smooth(const std::string &stylestr = "csplines");
251 
252  // ----------------------------------------------------------------------
259  // ----------------------------------------------------------------------
260  inline Gnuplot& unset_smooth(){ smooth = ""; return *this;};
261 
262 
264  Gnuplot& set_pointsize(const double pointsize = 1.0);
265 
267  inline Gnuplot& set_grid() {cmd("set grid");return *this;};
269  inline Gnuplot& unset_grid(){cmd("unset grid");return *this;};
270 
271  // -----------------------------------------------
277  // -----------------------------------------------
278  inline Gnuplot& set_multiplot(){cmd("set multiplot") ;return *this;};
279 
280  // -----------------------------------------------
286  // -----------------------------------------------
287  inline Gnuplot& unset_multiplot(){cmd("unset multiplot");return *this;};
288 
289 
290 
292  Gnuplot& set_samples(const int samples = 100);
294  Gnuplot& set_isosamples(const int isolines = 10);
295 
296  // --------------------------------------------------------------------------
302  // --------------------------------------------------------------------------
303  Gnuplot& set_hidden3d(){cmd("set hidden3d");return *this;};
304 
305  // ---------------------------------------------------------------------------
311  // ---------------------------------------------------------------------------
312  inline Gnuplot& unset_hidden3d(){cmd("unset hidden3d"); return *this;};
313 
316  Gnuplot& set_contour(const std::string &position = "base");
317  // --------------------------------------------------------------------------
323  // ------------------------------------------------------------------
324  inline Gnuplot& unset_contour(){cmd("unset contour");return *this;};
325 
326  // ------------------------------------------------------------
332  // ------------------------------------------------------------------
333  inline Gnuplot& set_surface(){cmd("set surface");return *this;};
334 
335  // ----------------------------------------------------------
342  // ------------------------------------------------------------------
343  inline Gnuplot& unset_surface(){cmd("unset surface"); return *this;}
344 
345 
348  Gnuplot& set_legend(const std::string &position = "default");
349 
350  // ------------------------------------------------------------------
357  // ------------------------------------------------------------------
358  inline Gnuplot& unset_legend(){cmd("unset key"); return *this;}
359 
360  // -----------------------------------------------------------------------
366  // -----------------------------------------------------------------------
367  inline Gnuplot& set_title(const std::string &title = "")
368  {
369  std::string cmdstr;
370  cmdstr = "set title \"";
371  cmdstr+=title;
372  cmdstr+="\"";
373  *this<<cmdstr;
374  return *this;
375  }
376 
377  //----------------------------------------------------------------------------------
384  // ---------------------------------------------------------------------------------
385  inline Gnuplot& unset_title(){this->set_title();return *this;}
386 
387 
389  Gnuplot& set_ylabel(const std::string &label = "x");
391  Gnuplot& set_xlabel(const std::string &label = "y");
393  Gnuplot& set_zlabel(const std::string &label = "z");
394 
396  Gnuplot& set_xrange(const double iFrom,
397  const double iTo);
399  Gnuplot& set_yrange(const double iFrom,
400  const double iTo);
402  Gnuplot& set_zrange(const double iFrom,
403  const double iTo);
409  // -----------------------------------------------
410  inline Gnuplot& set_xautoscale(){cmd("set xrange restore");cmd("set autoscale x");return *this;};
411 
412  // -----------------------------------------------
418  // -----------------------------------------------
419  inline Gnuplot& set_yautoscale(){cmd("set yrange restore");cmd("set autoscale y");return *this;};
420 
421  // -----------------------------------------------
427  // -----------------------------------------------
428  inline Gnuplot& set_zautoscale(){cmd("set zrange restore");cmd("set autoscale z");return *this;};
429 
430 
432  Gnuplot& set_xlogscale(const double base = 10);
434  Gnuplot& set_ylogscale(const double base = 10);
436  Gnuplot& set_zlogscale(const double base = 10);
437 
438  // -----------------------------------------------
444  // -----------------------------------------------
445  inline Gnuplot& unset_xlogscale(){cmd("unset logscale x"); return *this;};
446 
447  // -----------------------------------------------
453  // -----------------------------------------------
454  inline Gnuplot& unset_ylogscale(){cmd("unset logscale y"); return *this;};
455 
456  // -----------------------------------------------
462  // -----------------------------------------------
463  inline Gnuplot& unset_zlogscale(){cmd("unset logscale z"); return *this;};
464 
465 
467  Gnuplot& set_cbrange(const double iFrom, const double iTo);
468 
469 
470  //----------------------------------------------------------------------------------
471  // plot
472 
475  Gnuplot& plotfile_x(const std::string &filename,
476  const unsigned int column = 1,
477  const std::string &title = "");
479  template<typename X>
480  Gnuplot& plot_x(const X& x, const std::string &title = ""){
481  if (x.size() == 0)
482  {
483  throw GnuplotException("std::vector too small");
484  return *this;
485  }
486 
487  std::ofstream tmp;
488  std::string name = create_tmpfile(tmp);
489  if (name == "")
490  return *this;
491 
492  //
493  // write the data to file
494  //
495  for (unsigned int i = 0; i < x.size(); i++)
496  tmp << x[i] << std::endl;
497 
498  tmp.flush();
499  tmp.close();
500 
501 
502  plotfile_x(name, 1, title);
503 
504  return *this;
505  }
506 
507 
510  Gnuplot& plotfile_xy(const std::string &filename,
511  const unsigned int column_x = 1,
512  const unsigned int column_y = 2,
513  const std::string &title = "");
515  template<typename X, typename Y>
516  Gnuplot& plot_xy(const X& x, const Y& y, const std::string &title = "")
517  {
518  if (x.size() == 0 || y.size() == 0)
519  {
520  throw GnuplotException("std::vectors too small");
521  return *this;
522  }
523 
524  if (x.size() != y.size())
525  {
526  throw GnuplotException("Length of the std::vectors differs");
527  return *this;
528  }
529 
530 
531  std::ofstream tmp;
532  std::string name = create_tmpfile(tmp);
533  if (name == "")
534  return *this;
535 
536  //
537  // write the data to file
538  //
539  for (unsigned int i = 0; i < x.size(); i++)
540  tmp << x[i] << " " << y[i] << std::endl;
541 
542  tmp.flush();
543  tmp.close();
544 
545 
546  plotfile_xy(name, 1, 2, title);
547 
548  return *this;
549  }
550 
551 
552 
555  Gnuplot& plotfile_xy_err(const std::string &filename,
556  const unsigned int column_x = 1,
557  const unsigned int column_y = 2,
558  const unsigned int column_dy = 3,
559  const std::string &title = "");
561  template<typename X, typename Y, typename E>
562  Gnuplot& plot_xy_err(const X &x, const Y &y, const E &dy,
563  const std::string &title = ""){
564  if (x.size() == 0 || y.size() == 0 || dy.size() == 0)
565  {
566  throw GnuplotException("std::vectors too small");
567  return *this;
568  }
569 
570  if (x.size() != y.size() || y.size() != dy.size())
571  {
572  throw GnuplotException("Length of the std::vectors differs");
573  return *this;
574  }
575 
576 
577  std::ofstream tmp;
578  std::string name = create_tmpfile(tmp);
579  if (name == "")
580  return *this;
581 
582  //
583  // write the data to file
584  //
585  for (unsigned int i = 0; i < x.size(); i++)
586  tmp << x[i] << " " << y[i] << " " << dy[i] << std::endl;
587 
588  tmp.flush();
589  tmp.close();
590 
591 
592  // Do the actual plot
593  plotfile_xy_err(name, 1, 2, 3, title);
594 
595  return *this;
596  }
597 
598 
601  Gnuplot& plotfile_xyz(const std::string &filename,
602  const unsigned int column_x = 1,
603  const unsigned int column_y = 2,
604  const unsigned int column_z = 3,
605  const std::string &title = "");
607  template<typename X, typename Y, typename Z>
608  Gnuplot& plot_xyz(const X &x,
609  const Y &y,
610  const Z &z,
611  const std::string &title = ""){
612  if (x.size() == 0 || y.size() == 0 || z.size() == 0)
613  {
614  throw GnuplotException("std::vectors too small");
615  return *this;
616  }
617 
618  if (x.size() != y.size() || x.size() != z.size())
619  {
620  throw GnuplotException("Length of the std::vectors differs");
621  return *this;
622  }
623 
624 
625  std::ofstream tmp;
626  std::string name = create_tmpfile(tmp);
627  if (name == "")
628  return *this;
629 
630  //
631  // write the data to file
632  //
633  for (unsigned int i = 0; i < x.size(); i++)
634  tmp << x[i] << " " << y[i] << " " << z[i] <<std::endl;
635 
636  tmp.flush();
637  tmp.close();
638 
639 
640  plotfile_xyz(name, 1, 2, 3, title);
641 
642  return *this;
643  }
644 
645 
647  Gnuplot& plot_slope(const double a,
648  const double b,
649  const std::string &title = "");
650 
651 
662  Gnuplot& plot_equation(const std::string &equation,
663  const std::string &title = "");
664 
667  Gnuplot& plot_equation3d(const std::string &equation,
668  const std::string &title = "");
669 
670 
672  Gnuplot& plot_image(const unsigned char *ucPicBuf,
673  const unsigned int iWidth,
674  const unsigned int iHeight,
675  const std::string &title = "");
676 
677 
678  //----------------------------------------------------------------------------------
686  //----------------------------------------------------------------------------------
687  inline Gnuplot& replot(void){if (nplots > 0) cmd("replot");return *this;};
688 
690  Gnuplot& reset_plot();
691 
693  Gnuplot& reset_all();
694 
696  void remove_tmpfiles();
697 
698  // -------------------------------------------------------------------
705  // -------------------------------------------------------------------
706  inline bool is_valid(){return(valid);};
707 
708 };
709 template <typename Container>
710 void stringtok (Container &container,
711  std::string const &in,
712  const char * const delimiters = " \t\n")
713 {
714  const std::string::size_type len = in.length();
715  std::string::size_type i = 0;
716 
717  while ( i < len )
718  {
719  // eat leading whitespace
720  i = in.find_first_not_of (delimiters, i);
721 
722  if (i == std::string::npos)
723  return; // nothing left but white space
724 
725  // find the end of the token
726  std::string::size_type j = in.find_first_of (delimiters, i);
727 
728  // push token
729  if (j == std::string::npos)
730  {
731  container.push_back (in.substr(i));
732  return;
733  }
734  else
735  container.push_back (in.substr(i, j-i));
736 
737  // set up for next loop
738  i = j + 1;
739  }
740 
741  return;
742 }
743 #endif
A C++ interface to gnuplot.
Definition: gnuplot_i.hpp:62
Definition: gnuplot_i.hpp:70
Gnuplot & savetops(const std::string &filename="gnuplot_output")
saves a gnuplot session to a postscript file, filename without extension
Definition: gnuplot_i.cpp:327
Gnuplot & cmd(const std::string &cmdstr)
send a command to gnuplot
Definition: gnuplot_i.cpp:891
Gnuplot & unset_contour()
Definition: gnuplot_i.hpp:324
Gnuplot & plot_xy(const X &x, const Y &y, const std::string &title="")
from data
Definition: gnuplot_i.hpp:516
Gnuplot & plotfile_xy(const std::string &filename, const unsigned int column_x=1, const unsigned int column_y=2, const std::string &title="")
Definition: gnuplot_i.cpp:707
Gnuplot & set_xautoscale()
Definition: gnuplot_i.hpp:410
Gnuplot(const std::string &style="points")
set a style during construction
Definition: gnuplot_i.cpp:60
Gnuplot & set_zlogscale(const double base=10)
turns on/off log scaling for the specified zaxis (logscale is not set by default)
Definition: gnuplot_i.cpp:384
Gnuplot & set_ylogscale(const double base=10)
turns on/off log scaling for the specified yaxis (logscale is not set by default)
Definition: gnuplot_i.cpp:370
static void set_terminal_std(const std::string &type)
Definition: gnuplot_i.cpp:168
Gnuplot & set_cbrange(const double iFrom, const double iTo)
set palette range (autoscale by default)
Definition: gnuplot_i.cpp:545
void remove_tmpfiles()
deletes temporary files
Definition: gnuplot_i.cpp:1199
bool is_valid()
Is the gnuplot session valid ??
Definition: gnuplot_i.hpp:706
Gnuplot & replot(void)
replot repeats the last plot or splot command. this can be useful for viewing a plot with different s...
Definition: gnuplot_i.hpp:687
Gnuplot & showonscreen()
sets terminal type to terminal_std
Definition: gnuplot_i.cpp:315
Gnuplot & plot_equation(const std::string &equation, const std::string &title="")
Definition: gnuplot_i.cpp:595
Gnuplot & set_yrange(const double iFrom, const double iTo)
set y-axis - ranges
Definition: gnuplot_i.cpp:516
Gnuplot & set_xrange(const double iFrom, const double iTo)
set axis - ranges
Definition: gnuplot_i.cpp:502
Gnuplot & unset_surface()
Definition: gnuplot_i.hpp:343
Gnuplot & set_title(const std::string &title="")
sets and clears the title of a gnuplot session
Definition: gnuplot_i.hpp:367
Gnuplot & plotfile_x(const std::string &filename, const unsigned int column=1, const std::string &title="")
Definition: gnuplot_i.cpp:662
Gnuplot & plot_slope(const double a, const double b, const std::string &title="")
plot an equation of the form: y = ax + b, you supply a and b
Definition: gnuplot_i.cpp:561
Gnuplot & unset_grid()
grid is not set by default
Definition: gnuplot_i.hpp:269
Gnuplot & set_grid()
turns grid on/off
Definition: gnuplot_i.hpp:267
Gnuplot & set_yautoscale()
Definition: gnuplot_i.hpp:419
Gnuplot & set_samples(const int samples=100)
set sampling rate of functions, or for interpolating data
Definition: gnuplot_i.cpp:411
Gnuplot & reset_plot()
resets a gnuplot session (next plot will erase previous ones)
Definition: gnuplot_i.cpp:212
static bool set_GNUPlotPath(const std::string &path)
optional function: set Gnuplot path manual attention: for windows: path with slash '/' not backslash ...
Definition: gnuplot_i.cpp:140
Gnuplot & set_multiplot()
Definition: gnuplot_i.hpp:278
Gnuplot & plot_x(const X &x, const std::string &title="")
from std::vector
Definition: gnuplot_i.hpp:480
Gnuplot & set_zrange(const double iFrom, const double iTo)
set z-axis - ranges
Definition: gnuplot_i.cpp:530
~Gnuplot()
destructor: needed to delete temporary files
Definition: gnuplot_i.cpp:194
Gnuplot & plot_equation3d(const std::string &equation, const std::string &title="")
Definition: gnuplot_i.cpp:628
Gnuplot & unset_xlogscale()
Definition: gnuplot_i.hpp:445
Gnuplot & set_hidden3d()
Definition: gnuplot_i.hpp:303
Gnuplot & unset_ylogscale()
Definition: gnuplot_i.hpp:454
Gnuplot & set_pointsize(const double pointsize=1.0)
scales the size of the points used in plots
Definition: gnuplot_i.cpp:398
Gnuplot & set_surface()
Definition: gnuplot_i.hpp:333
Gnuplot & reset_all()
resets a gnuplot session and sets all variables to default
Definition: gnuplot_i.cpp:226
Gnuplot & plot_xy_err(const X &x, const Y &y, const E &dy, const std::string &title="")
from data
Definition: gnuplot_i.hpp:562
Gnuplot & plotfile_xyz(const std::string &filename, const unsigned int column_x=1, const unsigned int column_y=2, const unsigned int column_z=3, const std::string &title="")
Definition: gnuplot_i.cpp:794
Gnuplot & set_smooth(const std::string &stylestr="csplines")
Definition: gnuplot_i.cpp:291
Gnuplot & set_xlabel(const std::string &label="y")
set y axis label
Definition: gnuplot_i.cpp:461
Gnuplot & unset_multiplot()
Definition: gnuplot_i.hpp:287
Gnuplot & plot_image(const unsigned char *ucPicBuf, const unsigned int iWidth, const unsigned int iHeight, const std::string &title="")
plot image
Definition: gnuplot_i.cpp:836
Gnuplot & set_zlabel(const std::string &label="z")
set z axis label
Definition: gnuplot_i.cpp:487
Gnuplot & set_isosamples(const int isolines=10)
set isoline density (grid) for plotting functions as surfaces (for 3d plots)
Definition: gnuplot_i.cpp:425
Gnuplot & unset_hidden3d()
Definition: gnuplot_i.hpp:312
Gnuplot & unset_title()
Clears the title of a gnuplot session The title is not set by default.
Definition: gnuplot_i.hpp:385
Gnuplot & unset_legend()
Switches legend off attention:legend is set by default.
Definition: gnuplot_i.hpp:358
Gnuplot & set_style(const std::string &stylestr="points")
Definition: gnuplot_i.cpp:245
Gnuplot & plot_xyz(const X &x, const Y &y, const Z &z, const std::string &title="")
from std::vector
Definition: gnuplot_i.hpp:608
Gnuplot & set_legend(const std::string &position="default")
Definition: gnuplot_i.cpp:342
Gnuplot & unset_smooth()
unset smooth attention: smooth is not set by default
Definition: gnuplot_i.hpp:260
Gnuplot & set_zautoscale()
Definition: gnuplot_i.hpp:428
Gnuplot & set_contour(const std::string &position="base")
Definition: gnuplot_i.cpp:440
Gnuplot & unset_zlogscale()
Definition: gnuplot_i.hpp:463
Gnuplot & operator<<(const std::string &cmdstr)
Sends a command to an active gnuplot session, identical to cmd() send a command to gnuplot using the ...
Definition: gnuplot_i.hpp:221
Gnuplot & set_ylabel(const std::string &label="x")
set x axis label
Definition: gnuplot_i.cpp:474
Gnuplot & plotfile_xy_err(const std::string &filename, const unsigned int column_x=1, const unsigned int column_y=2, const unsigned int column_dy=3, const std::string &title="")
Definition: gnuplot_i.cpp:752
Gnuplot & set_xlogscale(const double base=10)
turns on/off log scaling for the specified xaxis (logscale is not set by default)
Definition: gnuplot_i.cpp:356