27 # define PATH_MAXNAMESZ 4096 36 # define pclose _pclose 38 # define access _access 41 # define MKTEMP_AND_CHECK_FAILED(name) (_mktemp(name) == NULL) 43 # define ACCESS_OK X_OK 45 # define MKTEMP_AND_CHECK_FAILED(name) (mkstemp(name) == -1) 68 template <
typename Container>
70 stringtok (Container &container,
string const &in,
71 const char *
const delimiters =
" \t\n")
73 const string::size_type len = in.length();
75 string::size_type i = 0;
80 i = in.find_first_not_of (delimiters, i);
81 if (i == string::npos)
85 string::size_type j = in.find_first_of (delimiters, i);
88 if (j == string::npos)
90 container.push_back (in.substr(i));
93 container.push_back (in.substr(i, j-i));
103 std::string Gnuplot::gnuplot_executable_ =
"pgnuplot.exe";
105 std::string Gnuplot::gnuplot_executable_ =
"gnuplot";
128 const string &labelx,
const string &labely,
129 vector<double> x, vector<double> y)
133 if (x.empty() || y.empty() )
137 this->set_style(
"lines");
139 this->set_style(style);
142 this->set_xlabel(
"X");
144 this->set_xlabel(labelx);
146 this->set_ylabel(
"Y");
148 this->set_ylabel(labely);
150 this->plot_xy(x,y,title);
152 cout <<
"Press enter to continue" << endl;
153 while (getchar() !=
'\n'){}
159 const string &labelx,
const string &labely,
170 this->set_style(
"lines");
172 this->set_style(style);
175 this->set_xlabel(
"X");
177 this->set_xlabel(labelx);
179 this->set_ylabel(
"Y");
181 this->set_ylabel(labely);
183 this->plot_x(x,title);
185 cout <<
"Press enter to continue" << endl;
186 while (getchar() !=
'\n'){}
193 if ( !((this->to_delete).empty()) )
195 for (
size_t i = 0; i < this->to_delete.size(); i++)
196 remove(this->to_delete[i].c_str());
199 if (pclose(this->gnucmd) == -1)
200 cerr <<
"Problem closing communication to gnuplot" << endl;
207 bool Gnuplot::get_program_path(
const string& pname)
212 path = getenv(
"PATH");
216 stringtok(ls, path, PATH_SEP);
218 for (list<string>::const_iterator i = ls.begin(); i != ls.end(); ++i)
220 string tmp = (*i) +
"/" + pname;
221 if ( access(tmp.c_str(), ACCESS_OK) == 0 )
232 if ( !(this->to_delete.empty()) )
234 for (
size_t i = 0; i < this->to_delete.size(); i++)
235 remove(this->to_delete[i].c_str());
245 if (stylestr !=
"lines" &&
246 stylestr !=
"points" &&
247 stylestr !=
"linespoints" &&
248 stylestr !=
"impulses" &&
249 stylestr !=
"dots" &&
250 stylestr !=
"steps" &&
251 stylestr !=
"errorbars" &&
252 stylestr !=
"boxes" &&
253 stylestr !=
"boxerrorbars")
254 this->pstyle = string(
"points");
256 this->pstyle = stylestr;
264 char local_cmd[GP_CMD_SIZE];
267 vsprintf(local_cmd, _cmd, ap);
270 strcat(local_cmd,
"\n");
271 fputs(local_cmd,this->gnucmd);
272 fflush(this->gnucmd);
280 ostringstream cmdstr;
282 cmdstr <<
"set xlabel \"" << label <<
"\"";
283 this->cmd(cmdstr.str().c_str());
292 ostringstream cmdstr;
294 cmdstr <<
"set xlabel \"" << label <<
"\"";
295 this->cmd(cmdstr.str().c_str());
307 ostringstream stitle;
308 ostringstream cmdstr;
311 stitle <<
"no title";
315 if (this->nplots > 0)
316 cmdstr <<
"replot " << a <<
" * x + " << b <<
" title \"" << stitle.str() <<
"\" with " << pstyle;
318 cmdstr <<
"plot " << a <<
" * x + " << b <<
" title \"" << stitle.str() <<
"\" with " << pstyle;
319 this->cmd(cmdstr.str().c_str());
330 string titlestr, plotstr;
331 ostringstream cmdstr;
334 titlestr =
"no title";
338 if (this->nplots > 0)
343 cmdstr << plotstr <<
" " << equation <<
" " <<
"title \"" << titlestr <<
"\" with " << this->pstyle;
344 this->cmd(cmdstr.str().c_str());
355 ostringstream cmdstr;
357 char name[] =
"gnuplotiXXXXXX";
359 char name[] =
"/tmp/gnuplotiXXXXXX";
362 if (this->to_delete.size() == GP_MAX_TMP_FILES - 1)
364 cerr <<
"Maximum number of temporary files reached (" << GP_MAX_TMP_FILES <<
"): cannot open more files" << endl;
371 if ( _mktemp(name) == NULL)
373 if ( mkstemp(name) == -1 )
376 cerr <<
"Cannot create temporary file: exiting plot" << endl;
382 cerr <<
"Cannot create temorary file: exiting plot" << endl;
389 this->to_delete.push_back(name);
394 for (
size_t i = 0; i < d.size(); i++)
402 cmdstr << ( (this->nplots > 0) ?
"replot " :
"plot ");
405 cmdstr <<
"\"" << name <<
"\" with " << this->pstyle;
407 cmdstr <<
"\"" << name <<
"\" title \"" << title <<
"\" with " 413 this->cmd(cmdstr.str().c_str());
424 ostringstream cmdstr;
426 char name[] =
"gnuplotiXXXXXX";
428 char name[] =
"/tmp/gnuplotiXXXXXX";
432 if (x.size() != y.size())
435 if ((this->to_delete).size() == GP_MAX_TMP_FILES - 1)
438 s <<
"Maximum number of temporary files reached (" 439 << GP_MAX_TMP_FILES <<
"): cannot open more files" << endl;
445 if (MKTEMP_AND_CHECK_FAILED(name))
454 this->to_delete.push_back(name);
458 size_t N = std::min(x.size(), y.size());
459 for (
size_t i = 0; i < N; i++)
460 tmp << x[i] <<
" " << y[i] << endl;
467 if (this->nplots > 0)
469 else cmdstr <<
"plot ";
471 cmdstr <<
"\"" << name <<
"\" with " << this->pstyle;
473 cmdstr <<
"\"" << name <<
"\" title \"" << title <<
"\" with " << this->pstyle;
478 this->cmd(cmdstr.str().c_str());
488 if (!this->get_program_path(gnuplot_executable_))
494 this->gnucmd = popen(gnuplot_executable_.c_str(),
"w");
Gnuplot()
Default constructor.
void plot_slope(double _a, double _b, const string &_title)
void reset_plot(void)
If multiple plots are present it will clear the plot area.
void plot_x(vector< double > _x, const string &_title)
Plot a single vector.
void plot_equation(const string &_equation, const string &_title)
Plot an equation supplied as a string.
void cmd(const char *_cmd,...)
Send a command to gnuplot (low-level function use by all plot functions.)
void set_ylabel(const string &_ylabel)
set x axis label
void set_xlabel(const string &_xlabel)
set x axis label
Exception thrown by class Gnuplot.
void plot_xy(vector< double > _x, vector< double > _y, const string &_title)
Plot x,y pairs.
void set_style(const string &_style)
set line style