Difference between revisions of "Gnuplot in cpp"

From AIRWiki
Jump to: navigation, search
 
(5 intermediate revisions by the same user not shown)
Line 14: Line 14:
  
  
# You should download gnuplot-cpp library. I suggest you to use my version LINKHERE because the official one [http://code.google.com/p/gnuplot-cpp/ gnuplot-cpp]has problems, (see below).
+
# You should download gnuplot-cpp library. I suggest you to use my version [[Media: Gnuplot-cpp_modified.zip‎]] because the official one [http://code.google.com/p/gnuplot-cpp/ gnuplot-cpp] has problems, (see below).
 
#To use the plotting library you should only need to include: "gnuplot-cpp/gnuplot_i.hpp"
 
#To use the plotting library you should only need to include: "gnuplot-cpp/gnuplot_i.hpp"
 
# A code snippet is here presented that simply plots a 2D set of points:
 
# A code snippet is here presented that simply plots a 2D set of points:
Line 20: Line 20:
 
     if(ANIMATIONON){
 
     if(ANIMATIONON){
 
         // GNUPLOT
 
         // GNUPLOT
 
 
         // GNUPLOT INITIALIZATION
 
         // GNUPLOT INITIALIZATION
 
         Gnuplot gp("My plot");
 
         Gnuplot gp("My plot");
Line 26: Line 25:
 
         // END GNUPLOT INITIALIZATION
 
         // END GNUPLOT INITIALIZATION
 
         try {
 
         try {
 
 
             gp.remove_tmpfiles(); // it is always better to do it
 
             gp.remove_tmpfiles(); // it is always better to do it
 
             gp.reset_plot(); // delete the plot (like Matlab figure())
 
             gp.reset_plot(); // delete the plot (like Matlab figure())
Line 38: Line 36:
 
         {
 
         {
 
             cout << ge.what() << endl;
 
             cout << ge.what() << endl;
        }
+
        }
 
+
 
       } // END GNUPLOT
 
       } // END GNUPLOT
  
 
</code>
 
</code>
 +
 +
'''HOW IT WORKS'''
 +
 +
For what I understood, the program writes a temp file (/tmp) in GNU/Linux and use it to plot in the correct way using [http://www.gnuplot.info/ Gnuplot].
 +
 +
'''PROBLEM OF THE OFFICIAL VERSION'''
 +
 +
Up to now (read the data of this post), the official package provided [http://code.google.com/p/gnuplot-cpp/ gnuplot-cpp] it has problems that deals with tempFiles. In many cases, for example in the destructor the function ''remove_tmpfiles()'' is commented. In this way many and many files are created in /tmp and after the max files limit is reached no other plots are created. Note that this could be useful if you want to save your points in a file for other processing.
 +
Moreover a major problem is due to the wrong use of the header file. Some functions are not inline and global constats are defined. These cause linking problems when you include official "gnuplot-cpp/gnuplot_i.hpp" in more than one source files.

Latest revision as of 13:11, 4 November 2009

How to plot using gnuplot in your C/C++ programs


This is a little guide that shows you how you can add a plotting function in your C/C++ program. This approach has some advantages and disadvantages:

PROS - You don't need to write on files or similar and plot in a second moment through a Matlab/Octave/Gnuplot script

CONTRAS - You don't have all the potentiality of Matlab/Octave plotting even if you can program your plotting utilities

HOW TO


  1. You should download gnuplot-cpp library. I suggest you to use my version Media: Gnuplot-cpp_modified.zip‎ because the official one gnuplot-cpp has problems, (see below).
  2. To use the plotting library you should only need to include: "gnuplot-cpp/gnuplot_i.hpp"
  3. A code snippet is here presented that simply plots a 2D set of points:

   if(ANIMATIONON){
        // GNUPLOT
        // GNUPLOT INITIALIZATION
        Gnuplot gp("My plot");
        //std::vector<double> x,y; // this are the vector containing (x,y) points
        // END GNUPLOT INITIALIZATION
        try {
           gp.remove_tmpfiles(); // it is always better to do it
           gp.reset_plot(); // delete the plot (like Matlab figure())
           cout << endl << endl << "*** user-defined lists of points (x,y)" << endl;
           gp.set_grid();
           gp.set_style("points").plot_xy(x,y,"user-defined points 2d");
           //sleep(1);  
           //usleep(3e5);
        }
        catch (GnuplotException ge)
        {
           cout << ge.what() << endl;
       }
     } // END GNUPLOT

HOW IT WORKS

For what I understood, the program writes a temp file (/tmp) in GNU/Linux and use it to plot in the correct way using Gnuplot.

PROBLEM OF THE OFFICIAL VERSION

Up to now (read the data of this post), the official package provided gnuplot-cpp it has problems that deals with tempFiles. In many cases, for example in the destructor the function remove_tmpfiles() is commented. In this way many and many files are created in /tmp and after the max files limit is reached no other plots are created. Note that this could be useful if you want to save your points in a file for other processing. Moreover a major problem is due to the wrong use of the header file. Some functions are not inline and global constats are defined. These cause linking problems when you include official "gnuplot-cpp/gnuplot_i.hpp" in more than one source files.