Guest User

example.cc

a guest
Nov 22nd, 2013
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.19 KB | None | 0 0
  1. // Example for C++ Interface to Gnuplot
  2.  
  3. // requirements:
  4. // * gnuplot has to be installed (http://www.gnuplot.info/download.html)
  5. // * for Windows: set Path-Variable for Gnuplot path (e.g. C:/program files/gnuplot/bin)
  6. //             or set Gnuplot path with: Gnuplot::set_GNUPlotPath(const std::string &path);
  7.  
  8.  
  9. #include <iostream>
  10. #include "gnuplot_i.hpp" //Gnuplot class handles POSIX-Pipe-communication with Gnuplot
  11.  
  12. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
  13.  #include <conio.h>   //for getch(), needed in wait_for_key()
  14.  #include <windows.h> //for Sleep()
  15.  void sleep(int i) { Sleep(i*1000); }
  16. #endif
  17.  
  18.  
  19. #define SLEEP_LGTH 2  // sleep time in seconds
  20. #define NPOINTS    50 // length of array
  21.  
  22. void wait_for_key(); // Programm halts until keypress
  23.  
  24. using std::cout;
  25. using std::endl;
  26.  
  27. int main(int argc, char* argv[])
  28. {
  29.     // if path-variable for gnuplot is not set, do it with:
  30.     // Gnuplot::set_GNUPlotPath("C:/program files/gnuplot/bin/");
  31.     //Gnuplot::set_GNUPlotPath("\"C:/Program/ Files\ (x86)/gnuplot/bin\"");
  32.     Gnuplot::set_GNUPlotPath("");
  33.  
  34.     // set a special standard terminal for showonscreen (normally not needed),
  35.     //   e.g. Mac users who want to use x11 instead of aqua terminal:
  36.     // Gnuplot::set_terminal_std("x11");
  37.  
  38.     cout << "*** example of gnuplot control through C++ ***" << endl << endl;
  39.  
  40.     //
  41.     // Using the GnuplotException class
  42.     //
  43.     try
  44.     {
  45.         Gnuplot g1("lines");
  46.  
  47.         //
  48.         // Slopes
  49.         //
  50.         cout << "*** plotting slopes" << endl;
  51.         g1.set_title("Slopes\\nNew Line");
  52.  
  53.         cout << "y = x" << endl;
  54.         g1.plot_slope(1.0,0.0,"y=x");
  55.  
  56.         cout << "y = 2*x" << endl;
  57.         g1.plot_slope(2.0,0.0,"y=2x");
  58.  
  59.         cout << "y = -x" << endl;
  60.         g1.plot_slope(-1.0,0.0,"y=-x");
  61.         g1.unset_title();
  62.  
  63.         //
  64.         // Equations
  65.         //
  66.         g1.reset_plot();
  67.         cout << endl << endl << "*** various equations" << endl;
  68.  
  69.         cout << "y = sin(x)" << endl;
  70.         g1.plot_equation("sin(x)","sine");
  71.  
  72.         cout << "y = log(x)" << endl;
  73.         g1.plot_equation("log(x)","logarithm");
  74.  
  75.         cout << "y = sin(x) * cos(2*x)" << endl;
  76.         g1.plot_equation("sin(x)*cos(2*x)","sine product");
  77.  
  78.         //
  79.         // Styles
  80.         //
  81.         g1.reset_plot();
  82.         cout << endl << endl << "*** showing styles" << endl;
  83.  
  84.         cout << "sine in points" << endl;
  85.         g1.set_pointsize(0.8).set_style("points");
  86.         g1.plot_equation("sin(x)","points");
  87.  
  88.         cout << "sine in impulses" << endl;
  89.         g1.set_style("impulses");
  90.         g1.plot_equation("sin(x)","impulses");
  91.  
  92.         cout << "sine in steps" << endl;
  93.         g1.set_style("steps");
  94.         g1.plot_equation("sin(x)","steps");
  95.  
  96.         //
  97.         // Save to ps
  98.         //
  99.         g1.reset_all();
  100.         cout << endl << endl << "*** save to ps " << endl;
  101.  
  102.         cout << "y = sin(x) saved to test_output.ps in working directory" << endl;
  103.         g1.savetops("test_output");
  104.         g1.set_style("lines").set_samples(300).set_xrange(0,5);
  105.         g1.plot_equation("sin(12*x)*exp(-x)").plot_equation("exp(-x)");
  106.  
  107.         g1.showonscreen(); // window output
  108.  
  109.  
  110.         //
  111.         // User defined 1d, 2d and 3d point sets
  112.         //
  113.         std::vector<double> x, y, y2, dy, z;
  114.  
  115.         for (int i = 0; i < NPOINTS; i++)  // fill double arrays x, y, z
  116.         {
  117.             x.push_back((double)i);             // x[i] = i
  118.             y.push_back((double)i * (double)i); // y[i] = i^2
  119.             z.push_back( x[i]*y[i] );           // z[i] = x[i]*y[i] = i^3
  120.             dy.push_back((double)i * (double)i / (double) 10); // dy[i] = i^2 / 10
  121.         }
  122.         y2.push_back(0.00); y2.push_back(0.78); y2.push_back(0.97); y2.push_back(0.43);
  123.         y2.push_back(-0.44); y2.push_back(-0.98); y2.push_back(-0.77); y2.push_back(0.02);
  124.  
  125.  
  126.         g1.reset_all();
  127.         cout << endl << endl << "*** user-defined lists of doubles" << endl;
  128.         g1.set_style("impulses").plot_x(y,"user-defined doubles");
  129.  
  130.         g1.reset_plot();
  131.         cout << endl << endl << "*** user-defined lists of points (x,y)" << endl;
  132.         g1.set_grid();
  133.         g1.set_style("points").plot_xy(x,y,"user-defined points 2d");
  134.  
  135.         g1.reset_plot();
  136.         cout << endl << endl << "*** user-defined lists of points (x,y,z)" << endl;
  137.         g1.unset_grid();
  138.         g1.plot_xyz(x,y,z,"user-defined points 3d");
  139.  
  140.         g1.reset_plot();
  141.         cout << endl << endl << "*** user-defined lists of points (x,y,dy)" << endl;
  142.         g1.plot_xy_err(x,y,dy,"user-defined points 2d with errorbars");
  143.  
  144.  
  145.         //
  146.         // Multiple output screens
  147.         //
  148.         cout << endl << endl;
  149.         cout << "*** multiple output windows" << endl;
  150.  
  151.         g1.reset_plot();
  152.         g1.set_style("lines");
  153.         cout << "window 1: sin(x)" << endl;
  154.         g1.set_grid().set_samples(600).set_xrange(0,300);
  155.         g1.plot_equation("sin(x)+sin(x*1.1)");
  156.  
  157.         g1.set_xautoscale().replot();
  158.  
  159.         Gnuplot g2;
  160.         cout << "window 2: user defined points" << endl;
  161.         g2.plot_x(y2,"points");
  162.         g2.set_smooth().plot_x(y2,"cspline");
  163.         g2.set_smooth("bezier").plot_x(y2,"bezier");
  164.         g2.unset_smooth();
  165.  
  166.         Gnuplot g3("lines");
  167.         cout << "window 3: log(x)/x" << endl;
  168.         g3.set_grid();
  169.         g3.plot_equation("log(x)/x","log(x)/x");
  170.  
  171.         Gnuplot g4("lines");
  172.         cout << "window 4: splot x*x+y*y" << endl;
  173.         g4.set_zrange(0,100);
  174.         g4.set_xlabel("x-axis").set_ylabel("y-axis").set_zlabel("z-axis");
  175.         g4.plot_equation3d("x*x+y*y");
  176.  
  177.         Gnuplot g5("lines");
  178.         cout << "window 5: splot with hidden3d" << endl;
  179.         g5.set_isosamples(25).set_hidden3d();
  180.         g5.plot_equation3d("x*y*y");
  181.  
  182.         Gnuplot g6("lines");
  183.         cout << "window 6: splot with contour" << endl;
  184.         g6.set_isosamples(60).set_contour();
  185.         g6.unset_surface().plot_equation3d("sin(x)*sin(y)+4");
  186.  
  187.         g6.set_surface().replot();
  188.  
  189.         Gnuplot g7("lines");
  190.         cout << "window 7: set_samples" << endl;
  191.         g7.set_xrange(-30,20).set_samples(40);
  192.         g7.plot_equation("besj0(x)*0.12e1").plot_equation("(x**besj0(x))-2.5");
  193.  
  194.         g7.set_samples(400).replot();
  195.  
  196.         Gnuplot g8("filledcurves");
  197.         cout << "window 8: filledcurves" << endl;
  198.         g8.set_legend("outside right top").set_xrange(-5,5);
  199.         g8.plot_equation("x*x").plot_equation("-x*x+4");
  200.  
  201.         //
  202.         // Plot an image
  203.         //
  204.         Gnuplot g9;
  205.         cout << "window 9: plot_image" << endl;
  206.         const int iWidth  = 255;
  207.         const int iHeight = 255;
  208.         g9.set_xrange(0,iWidth).set_yrange(0,iHeight).set_cbrange(0,255);
  209.         g9.cmd("set palette gray");
  210.         unsigned char ucPicBuf[iWidth*iHeight];
  211.         // generate a greyscale image
  212.         for(int iIndex = 0; iIndex < iHeight*iWidth; iIndex++)
  213.         {
  214.             ucPicBuf[iIndex] = iIndex%255;
  215.         }
  216.         g9.plot_image(ucPicBuf,iWidth,iHeight,"greyscale");
  217.  
  218.         g9.set_pointsize(0.6).unset_legend().plot_slope(0.8,20);
  219.  
  220.         //
  221.         // manual control
  222.         //
  223.         Gnuplot g10;
  224.         cout << "window 10: manual control" << endl;
  225.         g10.cmd("set samples 400").cmd("plot abs(x)/2"); // either with cmd()
  226.         g10 << "replot sqrt(x)" << "replot sqrt(-x)";    // or with <<
  227.  
  228.         wait_for_key();
  229.  
  230.     }
  231.     catch (GnuplotException ge)
  232.     {
  233.         cout << ge.what() << endl;
  234.     }
  235.  
  236.  
  237.     cout << endl << "*** end of gnuplot example" << endl;
  238.  
  239.     getchar();
  240.  
  241.     return 0;
  242. }
  243.  
  244.  
  245.  
  246. void wait_for_key ()
  247. {
  248. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)  // every keypress registered, also arrow keys
  249.     cout << endl << "Press any key to continue..." << endl;
  250.  
  251.     FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
  252.     _getch();
  253. #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
  254.     cout << endl << "Press ENTER to continue..." << endl;
  255.  
  256.     std::cin.clear();
  257.     std::cin.ignore(std::cin.rdbuf()->in_avail());
  258.     std::cin.get();
  259. #endif
  260.  
  261.     getchar();
  262.  
  263.     return;
  264. }
Advertisement
Add Comment
Please, Sign In to add comment