47 template <
typename T1,
typename T2>
void plot_str2(
const T1& x,
const T2& y )
49 std::ostringstream tmp_s;
51 tmp_s <<
"$Dxy" <<
plot_ix <<
" << EOD \n";
52 arma::uword Nelem = x.n_elem;
53 for( arma::uword n = 0; n < Nelem; n++ )
55 tmp_s << x( n ) <<
" " << y( n );
68 template <
typename T> std::string
get_type( T x )
70 if(
typeid( x ) ==
typeid( int8_t ) )
72 if(
typeid( x ) ==
typeid( uint8_t ) )
74 if(
typeid( x ) ==
typeid( int16_t ) )
76 if(
typeid( x ) ==
typeid( uint16_t ) )
78 if(
typeid( x ) ==
typeid( int32_t ) )
80 if(
typeid( x ) ==
typeid( uint32_t ) )
82 if(
typeid( x ) ==
typeid( int64_t ) )
84 if(
typeid( x ) ==
typeid( arma::sword ) )
86 if(
typeid( x ) ==
typeid( uint64_t ) )
88 if(
typeid( x ) ==
typeid( arma::uword ) )
90 if(
typeid( x ) ==
typeid(
float ) )
92 if(
typeid( x ) ==
typeid(
double ) )
106#if defined( _MSC_VER )
107 gnucmd = _popen(
"gnuplot -persist 2> NUL",
"wb" );
113 gnucmd = popen(
"gnuplot -persist",
"w" );
132#if defined( _MSC_VER )
145 std::string s_in( cmdstr );
146 std::string tmp = s_in +
"\n";
147 std::fputs( tmp.c_str(),
gnucmd );
174 std::ostringstream tmp_s;
175 tmp_s <<
"set term " <<
term <<
" " << fig;
176 std::string s = tmp_s.str();
190 void window(
const int fig,
const char* name,
const int x,
const int y,
const int width,
const int height )
193 std::ostringstream tmp_s;
194 tmp_s <<
"set term " <<
term <<
" " << fig <<
" title \"" << name <<
"\" position " << x <<
"," << y <<
" size " << width <<
"," << height;
195 std::string s = tmp_s.str();
209 void window(
const char* name,
const int x,
const int y,
const int width,
const int height )
211 window( 0, name, x, y, width, height );
219 std::ostringstream tmp_s;
220 tmp_s <<
"set term " <<
term <<
" close";
221 std::string s = tmp_s.str();
248 std::ostringstream tmp_s;
249 tmp_s <<
"set xlabel \"" <<
label <<
"\" ";
250 std::string s = tmp_s.str();
260 std::ostringstream tmp_s;
261 tmp_s <<
"set ylabel \"" <<
label <<
"\" ";
262 std::string s = tmp_s.str();
272 void label(
const double& x,
const double& y,
const char*
label )
274 std::ostringstream tmp_s;
275 tmp_s <<
"set label \"" <<
label <<
"\" at " << x <<
"," << y;
276 std::string s = tmp_s.str();
286 std::ostringstream tmp_s;
287 tmp_s <<
"set title \"" << name <<
" \" ";
288 std::string s = tmp_s.str();
297 void xlim(
const double xmin,
const double xmax )
299 std::ostringstream tmp_s;
300 tmp_s <<
"set xrange [" << xmin <<
":" << xmax <<
"]";
301 std::string s = tmp_s.str();
310 void ylim(
const double ymin,
const double ymax )
312 std::ostringstream tmp_s;
313 tmp_s <<
"set yrange [" << ymin <<
":" << ymax <<
"]";
314 std::string s = tmp_s.str();
325 template <
typename T1,
typename T2>
void plot_add(
const T1& x,
const T2& y,
const std::string lb,
const std::string ls =
"lines" )
343 template <
typename T1>
void plot_add(
const T1& y,
const std::string lb,
const std::string ls =
"lines" )
345 arma::vec x = arma::regspace( 0,
double( y.n_elem - 1 ) );
362 arma::vec x = arma::regspace( 0,
double( y.n_cols - 1 ) );
367 for( arma::uword r = 0; r < y.n_rows; r++ )
382 arma::vec x = arma::regspace( 0,
double( y.n_cols - 1 ) );
386 for( arma::uword r = 0; r < y.n_rows; r++ )
388 std::ostringstream tmp_s;
390 std::string s = tmp_s.str();
403 std::ostringstream tmp_s;
405 tmp_s <<
"plot $Dxy0 title \"" <<
plotlist[0].label <<
"\" with " <<
plotlist[0].linespec;
406 for(
int r = 1; r <
plot_ix; r++ )
408 tmp_s <<
" ,$Dxy" << r <<
" title \"" <<
plotlist[r].label <<
"\" with " <<
plotlist[r].linespec;
410 std::string s = tmp_s.str();
429 template <
typename T>
void fast_plot(
const arma::Col<T>& x,
const std::string fmt_args =
"with lines" )
431 std::string fmt =
get_type( x.at( 0 ) );
435 s =
"plot '-' binary format='%" + fmt +
"' array=(" + std::to_string( x.n_elem ) +
") " + fmt_args;
437 std::fwrite( x.memptr(),
sizeof( x.at( 0 ) ), x.n_elem,
gnucmd );
448 template <
typename T>
void fast_plot(
const arma::Col<T>& x,
const arma::Col<T>& y,
const std::string fmt_args =
"with lines" )
450 std::string fmt1 =
get_type( x.at( 0 ) );
453 const arma::uword N = x.n_elem;
454 arma::Col<T> v( 2 * N );
455 for( arma::uword n = 0; n < N; n++ )
457 v.at( 2 * n ) = x.at( n );
458 v.at( 2 * n + 1 ) = y.at( n );
461 s =
"plot '-' binary format='%" + fmt1 +
"' record=(" + std::to_string( x.n_elem ) +
") " + fmt_args;
463 std::fwrite( v.memptr(),
sizeof( x.at( 0 ) ), v.n_elem,
gnucmd );
471 template <
typename T>
void image(
const arma::Mat<T>& x )
473 xlim( -0.5, x.n_cols - 0.5 );
474 ylim( x.n_rows - 0.5, -0.5 );
476 std::string fmt =
get_type( x.at( 0 ) );
479 s =
"plot '-' binary array=(" + std::to_string( x.n_cols ) +
"," + std::to_string( x.n_rows ) +
") scan=yx format='%" + fmt +
"' w image";
481 std::fwrite( x.memptr(),
sizeof( x.at( 0 ) ), x.n_elem,
gnucmd );
489 template <
typename T>
void image(
const arma::Cube<T>& x )
491 xlim( -0.5, x.n_cols - 0.5 );
492 ylim( x.n_rows - 0.5, -0.5 );
494 std::string fmt =
get_type( x.at( 0 ) );
498 arma::Cube<T> gp_im( arma::size( x ) );
499 T* ptr = gp_im.memptr();
500 for( arma::uword c = 0; c < x.n_cols; c++ )
502 for( arma::uword r = 0; r < x.n_rows; r++ )
504 *ptr++ = x.at( r, c, 0 );
505 *ptr++ = x.at( r, c, 1 );
506 *ptr++ = x.at( r, c, 2 );
510 s =
"plot '-' binary array=(" + std::to_string( x.n_cols ) +
"," + std::to_string( x.n_rows ) +
") scan=yx format='%" + fmt +
"' w rgbimage";
512 std::fwrite( gp_im.memptr(),
sizeof( x.at( 0 ) ), gp_im.n_elem,
gnucmd );
520 template <
typename T>
void mesh(
const arma::Mat<T>& x )
524 std::string fmt =
get_type( x.at( 0 ) );
525 std::string s =
"splot '-' binary array=(" + std::to_string( x.n_cols ) +
"," + std::to_string( x.n_rows ) +
") scan=yx format='%" + fmt +
"' w lines";
527 std::fwrite( x.memptr(),
sizeof( x.at( 0 ) ), x.n_elem,
gnucmd );
535 template <
typename T>
void surf(
const arma::Mat<T>& x )
547 send2gp(
"set linetype 1 lc rgb '#0072bd' " );
548 send2gp(
"set linetype 2 lc rgb '#d95319' " );
549 send2gp(
"set linetype 3 lc rgb '#edb120' " );
550 send2gp(
"set linetype 4 lc rgb '#7e2f8e' " );
551 send2gp(
"set linetype 5 lc rgb '#77ac30' " );
552 send2gp(
"set linetype 6 lc rgb '#4dbeee' " );
553 send2gp(
"set linetype 7 lc rgb '#a2142f' " );
562 send2gp(
"set linetype 1 lc rgb '#0000ff' " );
563 send2gp(
"set linetype 2 lc rgb '#007f00' " );
564 send2gp(
"set linetype 3 lc rgb '#ff0000' " );
565 send2gp(
"set linetype 4 lc rgb '#00bfbf' " );
566 send2gp(
"set linetype 5 lc rgb '#bf00bf' " );
567 send2gp(
"set linetype 6 lc rgb '#bfbf00' " );
568 send2gp(
"set linetype 7 lc rgb '#3f3f3f' " );
577 send2gp(
"set linetype 1 lc rgb '#E41A1C' " );
578 send2gp(
"set linetype 2 lc rgb '#377EB8' " );
579 send2gp(
"set linetype 3 lc rgb '#4DAF4A' " );
580 send2gp(
"set linetype 4 lc rgb '#984EA3' " );
581 send2gp(
"set linetype 5 lc rgb '#FF7F00' " );
582 send2gp(
"set linetype 6 lc rgb '#FFFF33' " );
583 send2gp(
"set linetype 7 lc rgb '#A65628' " );
584 send2gp(
"set linetype 8 lc rgb '#F781BF' " );
586 send2gp(
"set palette maxcolors 8" );
587 char str[] =
"set palette defined ( \
605 char str[] =
"set palette defined ( \
624 char str[] =
"set palette defined (\
643 char str[] =
"set palette defined (\
661 char str[] =
"set palette defined (\
692 std::string name_s( name );
693 size_t found = name_s.find_last_of(
"." );
695 ext = name_s.substr( found + 1 );
696 std::ostringstream tmp_s;
698 if( ext.compare(
"png" ) == 0 )
700 tmp_s <<
"set terminal pngcairo enhanced font 'Verdana,10'";
702 else if( ext.compare(
"ps" ) == 0 )
704 tmp_s <<
"set terminal postscript enhanced color";
706 else if( ext.compare(
"eps" ) == 0 )
708 tmp_s <<
"set terminal postscript eps enhanced color";
710 else if( ext.compare(
"tex" ) == 0 )
712 tmp_s <<
"set terminal cairolatex eps color";
714 else if( ext.compare(
"pdf" ) == 0 )
716 tmp_s <<
"set terminal pdfcairo color enhanced";
718 else if( ext.compare(
"svg" ) == 0 )
720 tmp_s <<
"set terminal svg enhanced";
722 else if( ext.compare(
"emf" ) == 0 )
724 tmp_s <<
"set terminal emf color enhanced";
726 else if( ext.compare(
"gif" ) == 0 )
728 tmp_s <<
"set terminal gif enhanced";
736 tmp_s <<
"set terminal " <<
term;
738 std::string s = tmp_s.str();
741 tmp_s <<
"set output '" << name_s <<
"'";
759 std::ostringstream tmp_s;
760 tmp_s <<
"set terminal " << ttype;
761 std::string s = tmp_s.str();
void plot_show(void)
Show plots.
void plot_str2(const T1 &x, const T2 &y)
Plot y vs. x.
void plot_clear(void)
Clear plots.
void grid_on(void)
Set grid.
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 title(const char *name)
Set windowtitle.
void set_jet_palette(void)
Set palette to Matlab 'jet' Data from https://github.com/Gnuplotting/gnuplot-palettes.
void set_jet_line(void)
Set linetype to Matlab 'jet' NB! doesn't work with X11 -terminal Data from https://github....
void set_output(const char *name)
Save plot to file.
void set_blackbody_palette(void)
Set palette to 'black body'.
void fast_plot(const arma::Col< T > &x, const std::string fmt_args="with lines")
Plot vector, fast version.
void close_window(void)
Close window.
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 flush_cmd_buf(void)
Flush command buffer to Gnuplot pipe.
void set_coolwarm_palette(void)
Set palette to 'cool-warm'.
void xlabel(const char *label)
Set label for X-axis.
void ylabel(const char *label)
Set label for X-axis.
void figure(const int fig)
Sets the active figure.
void mesh(const arma::Mat< T > &x)
Plot mat as mesh.
void reset_term(void)
Reset output terminal.
void surf(const arma::Mat< T > &x)
Plot mat as surf.
void send2gp(const char *cmdstr)
Send command to Gnuplot pipe.
FILE * gnucmd
File handle to pipe.
void ylim(const double ymin, const double ymax)
Set Y-axis range.
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_set1_line(void)
Set linetype to Matlab 'parula' NB! doesn't work with X11 -terminal Data from https://github....
void image(const arma::Mat< T > &x)
Plot mat as image.
void set_term(const char *ttype)
Set output terminal.
void image(const arma::Cube< T > &x)
Plot cube as image.
void xlim(const double xmin, const double xmax)
Set X-axis range.
void draw_now(void)
Updates gnuplot instantly. (Flushes the command buffer)
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 fast_plot(const arma::Col< T > &x, const arma::Col< T > &y, const std::string fmt_args="with lines")
Plot vector, fast version.
void plot_add_mat(const arma::mat &y)
Push multiple plot, each row gives a plot without label.
void grid_off(void)
Set grid.
void label(const double &x, const double &y, const char *label)
Set label at position x,y.
std::string get_type(T x)
Get type.
std::vector< plot_data_s > plotlist
void set_parula_palette(void)
Set palette to Matlab 'parula' Data from https://github.com/Gnuplotting/gnuplot-palettes.
void set_parula_line(void)
Set linetype to Matlab 'parula' NB! doesn't work with X11 -terminal Data from https://github....
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.