7

I have the following, and no matter what i try a command window is opened and closed again. No plots are shown, no files are written. Anyone who have a solution to use gnuplot from c++. I have both 4.4 and 4.6rc1 available.

#ifdef WIN32
  gp = _popen("C:\Program Files (x86)\gnuplot\bin\pgnuplot.exe", "w");
#else
  gp = popen("gnuplot -persist", "w");
#endif


 if (gp == NULL)
       return -1;

  /* fprintf(gp, "unset border\n");
  fprintf(gp, "set clip\n");
  fprintf(gp, "set polar\n");
  fprintf(gp, "set xtics axis nomirror\n");
  fprintf(gp, "set ytics axis nomirror\n");
  fprintf(gp, "unset rtics\n");
  fprintf(gp, "set samples 160\n");
  fprintf(gp, "set zeroaxis");
    fprintf(gp, "  set trange [0:2*pi]");*/


  fprintf(gp, "set term png\n");
  fprintf(gp, "set output \"c:\\printme.png\"");
  fprintf(gp, "plot .5,1,1.5\n");
   fprintf(gp, "pause -1\n");

     fflush(gp);
Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283

2 Answers2

8

The following program has been tested on Windows using the Visual Studio and MinGW compilers, as well as on GNU/Linux using gcc. The gnuplot binary must be on the path, and on Windows, the piped pgnuplot version of the binary must be used.

I've found that Windows pipes are much slower than the corresponding ones on GNU/Linux. For large datasets, transferring data to gnuplot over a pipe on Windows is slow and often unreliable. Moreover, the key press waiting code is more useful on GNU/Linux, where the plot window will close once pclose() has been called.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

// Tested on:
// 1. Visual Studio 2012 on Windows
// 2. Mingw gcc 4.7.1 on Windows
// 3. gcc 4.6.3 on GNU/Linux

// Note that gnuplot binary must be on the path
// and on Windows we need to use the piped version of gnuplot
#ifdef WIN32
    #define GNUPLOT_NAME "pgnuplot -persist"
#else 
    #define GNUPLOT_NAME "gnuplot"
#endif

int main() 
{
    #ifdef WIN32
        FILE *pipe = _popen(GNUPLOT_NAME, "w");
    #else
        FILE *pipe = popen(GNUPLOT_NAME, "w");
    #endif

    if (pipe != NULL)
    {
        fprintf(pipe, "set term wx\n");         // set the terminal
        fprintf(pipe, "plot '-' with lines\n"); // plot type
        for(int i = 0; i < 10; i++)             // loop over the data [0,...,9]
            fprintf(pipe, "%d\n", i);           // data terminated with \n
        fprintf(pipe, "%s\n", "e");             // termination character
        fflush(pipe);                           // flush the pipe

        // wait for key press
        std::cin.clear();
        std::cin.ignore(std::cin.rdbuf()->in_avail());
        std::cin.get();

        #ifdef WIN32
                _pclose(pipe);
        #else
                pclose(pipe);
        #endif
    }
    else
        std::cout << "Could not open pipe" << std::endl; 
return 0;
}
Nicholas Kinar
  • 1,440
  • 5
  • 24
  • 36
  • There's no `pgnuplot`in the bin folder. Should it be installed separately ? – locke14 Apr 30 '15 at 13:41
  • Try wgnuplot_pipes.exe instead. Essentially there should be a version of gnuplot that supports pipes on Windows. – Nicholas Kinar Apr 30 '15 at 16:53
  • I couldn't find any pipes version of gnuplot in version 5.0.0. But, `pgnuplot` is there in version 4.6.6. – locke14 May 04 '15 at 14:32
  • for version 5.0 use gnuplot.exe instead of pgnuplot. From documentation : gnuplot.exe: Text (console) mode version of the gnuplot executable with full pipe functionality as it is common on other platforms. In contrast to wgnuplot.exe, this program can also accept commands on stdin (standard input) and print messages on stdout (standard output). It replaces pgnuplot.exe and is recommended to be used with 3rd party applications using gnuplot as graph engine, like e.g. Octave (www.octave.org). – Malick Dec 20 '15 at 15:13
4

Of course, following answer is quite similar to the answer of Nicholas Kinar, the only added point is how to save the .png file properly. Also a couple of suggestions:

  1. If input path has spaces, it won't work. In windows 8 using Visual Studio 2013, it shows an error "C:Program" is not recognized as an internal or external command....
  2. If you do not mention any output path, it prints and saves in the directory of C++ program itself. So you must check the proper format for output path, as gnuplot doesn't shows any error, it is difficult to spot the exact reason.

Following is the complete C++ program which works just fine on Visual Studio 2013 on Windows 8.1

#include <cstdio>
#include <iostream>
int main()
{

    FILE* pipe = _popen("C:/gnuplot/bin/pgnuplot.exe", "w");
    if (pipe != NULL)
    {
        fprintf(pipe, "set term win\n");
        fprintf(pipe, "plot(x, sin(x))\n"); //a simple example function
        fprintf(pipe, "set term pngcairo\n");
        fprintf(pipe, "set output \"myFile.png\"\n" );
        fprintf(pipe, "replot\n");
        fprintf(pipe, "set term win\n");
        fflush(pipe);
    }
    else puts("Could not open the file\n");
    _pclose(pipe);
    //system("pause");
    return 0;
}
aniliitb10
  • 1,259
  • 10
  • 14