【发布时间】:2015-02-14 06:00:15
【问题描述】:
我使用 linux,C++。我想保存 gnuplot 的输出。我怎样才能使用 C++ 做到这一点?我试过下面的代码。它生成一个png文件。但它没有情节点。我想做两个任务
- 在运行时显示图表
- 程序结束后将图形保存为 gif。
我该怎么做?
FILE *pipe = popen("gnuplot -persist", "w");
// set axis ranges
fprintf(pipe,"set xrange [0:11]\n");
fprintf(pipe,"set yrange [0:]\n");
fprintf(pipe, "set terminal png\n");
fprintf(pipe, "set output 'b.png'\n");
int b = 5;int a;
// to make 10 points
std::vector<int> x (10, 0.0); // x values
std::vector<int> y (10, 0.0); // y values
for (a=0;a<5;a++) // 10 plots
{
x[a] = a;
y[a] = 2*a;// some function of a
fprintf(pipe,"plot '-'\n");
// 1 additional data point per plot
for (int ii = 0; ii <= a; ii++) {
fprintf(pipe, "%d %d\n", x[ii], y[ii]); // plot `a` points
}
fprintf(pipe,"e\n"); // finally, e
fflush(pipe); // flush the pipe to update the plot
usleep(1000000);// wait a second before updating again
}
【问题讨论】: