我有以下内容,无论我尝试什么,命令窗口都会再次打开和关闭。没有显示任何图,也没有写入任何文件。任何有解决方案的人都可以使用 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);
以下程序已在 Windows 上使用 Visual Studio 和 MinGW 编译器以及在 GNU/Linux 上使用进行了测试gcc
. The 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)