Gnuplot,来自windows的c ++.命令窗口打开和关闭

Pou*_*sen 6 c++ gnuplot pipe

我有以下,无论我尝试什么命令窗口再次打开和关闭.没有显示图表,也没有写入文件.任何有c +使用gnuplot的解决方案的人.我有4.4和4.6rc1可用.

#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);
Run Code Online (Sandbox Code Playgroud)

Nic*_*nar 7

以下程序已在Windows上使用Visual Studio和MinGW编译器以及使用GNU/Linux进行了测试gcc.该gnuplot二进制文件必须在路径上,和在Windows上,管道pgnuplot必须使用的二进制版本.

我发现Windows管道比GNU/Linux上的相应管道慢得多.对于大型数据集,gnuplot在Windows上通过管道传输数据的速度很慢且通常不可靠.此外,按键等待代码在GNU/Linux上更有用,其中绘图窗口将pclose()在调用后关闭.

#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;
}
Run Code Online (Sandbox Code Playgroud)


ani*_*b10 5

当然,下面的答案与Nicholas Kinar的答案非常相似,唯一增加的一点是如何.png正确保存文件。还有一些建议:

  1. 如果输入路径有空格,则不起作用。在使用 Visual Studio 2013 的 Windows 8 中,显示错误"C:Program" is not recognized as an internal or external command....
  2. 如果不指定任何输出路径,它将打印并保存在 C++ 程序本身的目录中。所以你必须检查输出路径的格式是否正确,因为gnuplot没有显示任何错误,所以很难找出确切的原因。

以下是完整的 C++ 程序,它在 Windows 8.1 上的 Visual Studio 2013 上运行良好

#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;
}
Run Code Online (Sandbox Code Playgroud)