45 template <
typename T1,
typename T2>
void plot_str2(
const T1 &x,
const T2 &y)
47 std::ostringstream tmp_s;
49 tmp_s <<
"$Dxy" << plot_ix <<
" << EOD \n";
50 arma::uword Nelem = x.n_elem;
51 for (arma::uword n = 0; n < Nelem; n++)
53 tmp_s << x(n) <<
" " << y(n);
66 template <
typename T> std::string
get_type(T x)
68 if (
typeid(x) ==
typeid(int8_t))
70 if (
typeid(x) ==
typeid(uint8_t))
72 if (
typeid(x) ==
typeid(int16_t))
74 if (
typeid(x) ==
typeid(uint16_t))
76 if (
typeid(x) ==
typeid(int32_t))
78 if (
typeid(x) ==
typeid(uint32_t))
80 if (
typeid(x) ==
typeid(int64_t))
82 if (
typeid(x) ==
typeid(arma::sword))
84 if (
typeid(x) ==
typeid(uint64_t))
86 if (
typeid(x) ==
typeid(arma::uword))
88 if (
typeid(x) ==
typeid(
float))
90 if (
typeid(x) ==
typeid(
double))
104 #if defined(_MSC_VER) 105 gnucmd = _popen(
"gnuplot -persist 2> NUL",
"wb");
111 gnucmd = popen(
"gnuplot -persist",
"w");
130 #if defined(_MSC_VER) 143 std::string s_in(cmdstr);
144 std::string tmp = s_in +
"\n";
145 std::fputs(tmp.c_str(),
gnucmd);
166 std::ostringstream tmp_s;
167 tmp_s <<
"set term " << term <<
" " << fig;
168 std::string s = tmp_s.str();
182 void window(
const int fig,
const char *name,
const int x,
const int y,
183 const int width,
const int height)
186 std::ostringstream tmp_s;
187 tmp_s <<
"set term " << term <<
" " << fig <<
" title \"" << name
188 <<
"\" position " << x <<
"," << y <<
" size " << width <<
"," 190 std::string s = tmp_s.str();
204 void window(
const char *name,
const int x,
const int y,
const int width,
207 window(0, name, x, y, width, height);
215 std::ostringstream tmp_s;
216 tmp_s <<
"set term " << term <<
" close";
217 std::string s = tmp_s.str();
238 std::ostringstream tmp_s;
239 tmp_s <<
"set xlabel \"" << label <<
"\" ";
240 std::string s = tmp_s.str();
250 std::ostringstream tmp_s;
251 tmp_s <<
"set ylabel \"" << label <<
"\" ";
252 std::string s = tmp_s.str();
262 void label(
const double &x,
const double &y,
const char *
label)
264 std::ostringstream tmp_s;
265 tmp_s <<
"set label \"" << label <<
"\" at " << x <<
"," << y;
266 std::string s = tmp_s.str();
276 std::ostringstream tmp_s;
277 tmp_s <<
"set title \"" << name <<
" \" ";
278 std::string s = tmp_s.str();
287 void xlim(
const double xmin,
const double xmax)
289 std::ostringstream tmp_s;
290 tmp_s <<
"set xrange [" << xmin <<
":" << xmax <<
"]";
291 std::string s = tmp_s.str();
300 void ylim(
const double ymin,
const double ymax)
302 std::ostringstream tmp_s;
303 tmp_s <<
"set yrange [" << ymin <<
":" << ymax <<
"]";
304 std::string s = tmp_s.str();
315 template <
typename T1,
typename T2>
316 void plot_add(
const T1 &x,
const T2 &y,
const std::string lb,
317 const std::string ls =
"lines")
324 plotlist.push_back(pd);
335 template <
typename T1>
337 const std::string ls =
"lines")
339 arma::vec x = arma::regspace(0,
double(y.n_elem - 1));
345 plotlist.push_back(pd);
356 arma::vec x = arma::regspace(0,
double(y.n_cols - 1));
361 for (arma::uword r = 0; r < y.n_rows; r++)
363 plotlist.push_back(pd);
376 arma::vec x = arma::regspace(0,
double(y.n_cols - 1));
380 for (arma::uword r = 0; r < y.n_rows; r++)
382 std::ostringstream tmp_s;
384 std::string s = tmp_s.str();
386 plotlist.push_back(pd);
397 std::ostringstream tmp_s;
399 tmp_s <<
"plot $Dxy0 title \"" << plotlist[0].label <<
"\" with " 400 << plotlist[0].linespec;
401 for (
int r = 1; r <
plot_ix; r++)
403 tmp_s <<
" ,$Dxy" << r <<
" title \"" << plotlist[r].label <<
"\" with " 404 << plotlist[r].linespec;
406 std::string s = tmp_s.str();
425 template <
typename T>
427 const std::string fmt_args =
"with lines")
429 std::string fmt =
get_type(x.at(0));
433 s =
"plot '-' binary format='%" + fmt +
"' array=(" +
434 std::to_string(x.n_elem) +
") " + fmt_args;
436 std::fwrite(x.memptr(),
sizeof(x.at(0)), x.n_elem, gnucmd);
447 template <
typename T>
448 void fast_plot(
const arma::Col<T> &x,
const arma::Col<T> &y,
449 const std::string fmt_args =
"with lines")
451 std::string fmt1 =
get_type(x.at(0));
454 const arma::uword N = x.n_elem;
455 arma::Col<T> v(2 * N);
456 for (arma::uword n = 0; n < N; n++)
458 v.at(2 * n) = x.at(n);
459 v.at(2 * n + 1) = y.at(n);
462 s =
"plot '-' binary format='%" + fmt1 +
"' record=(" +
463 std::to_string(x.n_elem) +
") " + fmt_args;
465 std::fwrite(v.memptr(),
sizeof(x.at(0)), v.n_elem, gnucmd);
473 template <
typename T>
void image(
const arma::Mat<T> &x)
475 xlim(-0.5, x.n_cols - 0.5);
476 ylim(x.n_rows - 0.5, -0.5);
478 std::string fmt =
get_type(x.at(0));
481 s =
"plot '-' binary array=(" + std::to_string(x.n_cols) +
"," +
482 std::to_string(x.n_rows) +
") scan=yx format='%" + fmt +
"' w image";
484 std::fwrite(x.memptr(),
sizeof(x.at(0)), x.n_elem, gnucmd);
492 template <
typename T>
void image(
const arma::Cube<T> &x)
494 xlim(-0.5, x.n_cols - 0.5);
495 ylim(x.n_rows - 0.5, -0.5);
497 std::string fmt =
get_type(x.at(0));
501 arma::Cube<T> gp_im(arma::size(x));
502 T *ptr = gp_im.memptr();
503 for (arma::uword c = 0; c < x.n_cols; c++)
505 for (arma::uword r = 0; r < x.n_rows; r++)
507 *ptr++ = x.at(r, c, 0);
508 *ptr++ = x.at(r, c, 1);
509 *ptr++ = x.at(r, c, 2);
513 s =
"plot '-' binary array=(" + std::to_string(x.n_cols) +
"," +
514 std::to_string(x.n_rows) +
") scan=yx format='%" + fmt +
"' w rgbimage";
516 std::fwrite(gp_im.memptr(),
sizeof(x.at(0)), gp_im.n_elem, gnucmd);
524 template <
typename T>
void mesh(
const arma::Mat<T> &x)
528 std::string fmt =
get_type(x.at(0));
529 std::string s =
"splot '-' binary array=(" + std::to_string(x.n_cols) +
530 "," + std::to_string(x.n_rows) +
") scan=yx format='%" +
533 std::fwrite(x.memptr(),
sizeof(x.at(0)), x.n_elem, gnucmd);
541 template <
typename T>
void surf(
const arma::Mat<T> &x)
553 send2gp(
"set linetype 1 lc rgb '#0072bd' ");
554 send2gp(
"set linetype 2 lc rgb '#d95319' ");
555 send2gp(
"set linetype 3 lc rgb '#edb120' ");
556 send2gp(
"set linetype 4 lc rgb '#7e2f8e' ");
557 send2gp(
"set linetype 5 lc rgb '#77ac30' ");
558 send2gp(
"set linetype 6 lc rgb '#4dbeee' ");
559 send2gp(
"set linetype 7 lc rgb '#a2142f' ");
568 send2gp(
"set linetype 1 lc rgb '#0000ff' ");
569 send2gp(
"set linetype 2 lc rgb '#007f00' ");
570 send2gp(
"set linetype 3 lc rgb '#ff0000' ");
571 send2gp(
"set linetype 4 lc rgb '#00bfbf' ");
572 send2gp(
"set linetype 5 lc rgb '#bf00bf' ");
573 send2gp(
"set linetype 6 lc rgb '#bfbf00' ");
574 send2gp(
"set linetype 7 lc rgb '#3f3f3f' ");
582 send2gp(
"set linetype 1 lc rgb '#E41A1C' ");
583 send2gp(
"set linetype 2 lc rgb '#377EB8' ");
584 send2gp(
"set linetype 3 lc rgb '#4DAF4A' ");
585 send2gp(
"set linetype 4 lc rgb '#984EA3' ");
586 send2gp(
"set linetype 5 lc rgb '#FF7F00' ");
587 send2gp(
"set linetype 6 lc rgb '#FFFF33' ");
588 send2gp(
"set linetype 7 lc rgb '#A65628' ");
589 send2gp(
"set linetype 8 lc rgb '#F781BF' ");
591 send2gp(
"set palette maxcolors 8");
592 char str[] =
"set palette defined ( \ 610 char str[] =
"set palette defined ( \ 629 char str[] =
"set palette defined (\ 648 char str[] =
"set palette defined (\ 666 char str[] =
"set palette defined (\ 697 std::string name_s(name);
698 size_t found = name_s.find_last_of(
".");
700 ext = name_s.substr(found + 1);
701 std::ostringstream tmp_s;
703 if (ext.compare(
"png") == 0)
705 tmp_s <<
"set terminal pngcairo enhanced font 'Verdana,10'";
707 else if (ext.compare(
"ps") == 0)
709 tmp_s <<
"set terminal postscript enhanced color";
711 else if (ext.compare(
"eps") == 0)
713 tmp_s <<
"set terminal postscript eps enhanced color";
715 else if (ext.compare(
"tex") == 0)
717 tmp_s <<
"set terminal cairolatex eps color enhanced";
719 else if (ext.compare(
"pdf") == 0)
721 tmp_s <<
"set terminal pdfcairo color enhanced";
723 else if (ext.compare(
"svg") == 0)
725 tmp_s <<
"set terminal svg enhanced";
727 else if (ext.compare(
"emf") == 0)
729 tmp_s <<
"set terminal emf color enhanced";
731 else if (ext.compare(
"gif") == 0)
733 tmp_s <<
"set terminal gif enhanced";
741 tmp_s <<
"set terminal " <<
term;
743 std::string s = tmp_s.str();
746 tmp_s <<
"set output '" << name_s <<
"'";
761 std::ostringstream tmp_s;
762 tmp_s <<
"set terminal " << ttype;
763 std::string s = tmp_s.str();
void set_set1_line(void)
Set linetype to Matlab 'parula' NB! doesn't work with X11 -terminal Data from https://github.com/Gnuplotting/gnuplot-palettes.
void draw_now(void)
Updates gnuplot instantly. (Flushes the command buffer)
void set_output(const char *name)
Save plot to file.
void reset_term(void)
Reset output terminal.
void set_jet_line(void)
Set linetype to Matlab 'jet' NB! doesn't work with X11 -terminal Data from https://github.com/Gnuplotting/gnuplot-palettes.
std::string get_type(T x)
Get type.
void fast_plot(const arma::Col< T > &x, const arma::Col< T > &y, const std::string fmt_args="with lines")
Plot vector, fast version.
void flush_cmd_buf(void)
Flush command buffer to Gnuplot pipe.
std::vector< plot_data_s > plotlist
void plot_add_mat(const arma::mat &y, const std::string p_lb)
Push multiple plot, each row gives a plot with prefix label.
void surf(const arma::Mat< T > &x)
Plot mat as surf.
void plot_add(const T1 &y, const std::string lb, const std::string ls="lines")
Push plot y vs. x with label and linespec.
void set_blackbody_palette(void)
Set palette to 'black body'.
void grid_off(void)
Set grid.
void fast_plot(const arma::Col< T > &x, const std::string fmt_args="with lines")
Plot vector, fast version.
void xlim(const double xmin, const double xmax)
Set X-axis range.
void set_jet_palette(void)
Set palette to Matlab 'jet' Data from https://github.com/Gnuplotting/gnuplot-palettes.
void plot_show(void)
Show plots.
void plot_add_mat(const arma::mat &y)
Push multiple plot, each row gives a plot without label.
void grid_on(void)
Set grid.
void set_coolwarm_palette(void)
Set palette to 'cool-warm'.
FILE * gnucmd
File handle to pipe.
void plot_str2(const T1 &x, const T2 &y)
Plot y vs. x.
void ylabel(const char *label)
Set label for X-axis.
void set_parula_palette(void)
Set palette to Matlab 'parula' Data from https://github.com/Gnuplotting/gnuplot-palettes.
void figure(const int fig)
Sets the active figure.
void label(const double &x, const double &y, const char *label)
Set label at position x,y.
void set_parula_line(void)
Set linetype to Matlab 'parula' NB! doesn't work with X11 -terminal Data from https://github.com/Gnuplotting/gnuplot-palettes.
void image(const arma::Mat< T > &x)
Plot mat as image.
void plot_clear(void)
Clear plots.
void set_term(const char *ttype)
Set output terminal.
void image(const arma::Cube< T > &x)
Plot cube as image.
void window(const int fig, const char *name, const int x, const int y, const int width, const int height)
Configure the figure used Windows environment.
void xlabel(const char *label)
Set label for X-axis.
void ylim(const double ymin, const double ymax)
Set Y-axis range.
void plot_add(const T1 &x, const T2 &y, const std::string lb, const std::string ls="lines")
Push plot y vs. x with label and linespec.
void mesh(const arma::Mat< T > &x)
Plot mat as mesh.
void window(const char *name, const int x, const int y, const int width, const int height)
Configure the figure/window - used in Linux environment where no figure numbers are needed...
void close_window(void)
Close window.
void title(const char *name)
Set windowtitle.
void send2gp(const char *cmdstr)
Send command to Gnuplot pipe.