【发布时间】:2014-06-09 13:45:06
【问题描述】:
我有一个程序会产生大量数据。我想每秒绘制一次数据,以便我可以监控它的进度。在下面的示例中,我正在使用“a”循环创建 10 个图形(每秒一个),如果我绘制一个函数而不是数据点,这可以正常工作。
在 b 循环中,我想创建一组新的 x-y 数据,然后将其绘制出来。我可以创建数据,但不知道如何将其传递给 gnuplot。
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <unistd.h> // usleep
using namespace std;
int main(){
// Code for gnuplot pipe
FILE *pipe = popen("gnuplot -persist", "w"); // open pipe to gnuplot
fprintf(pipe, "\n");
fprintf(pipe,"plot '-' using 1:2\n"); // so I want the first column to be x values, second column to be y
int b;
for (int a=0;a<10;a++) // 10 plots
{
for (b=0;b<10;b++); // 10 datapoints per plot
{
// this is the bit I can't get right:
fprintf(pipe,"%d %d\n",a,b); // passing x,y data pairs one at a time to gnuplot
}
fprintf(pipe,"e\n"); // finally, e
fflush(pipe); // flush the pipe to update the plot
usleep(1000000);// wait a second before updating again
}
// Don't forget to close the pipe
fclose(pipe);
return 0;
}
编辑:下面的代码应该可以工作 - 每秒绘制 10 个数据点,持续 10 秒:
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <unistd.h> // usleep
using namespace std;
int main(){
// Code for gnuplot pipe
FILE *pipe = popen("gnuplot -persist", "w");
// set axis ranges
fprintf(pipe,"set xrange [0:11]\n");
fprintf(pipe,"set yrange [0:11]\n");
int b;
for (int a=0;a<10;a++) // 10 plots
{
fprintf(pipe,"plot '-' using 1:2 \n"); // so I want the first column to be x values, second column to be y
for (b=0;b<10;b++) // 10 datapoints per plot
{
fprintf(pipe, "%d %d \n",a,b); // passing x,y data pairs one at a time to gnuplot
}
fprintf(pipe,"e \n"); // finally, e
fflush(pipe); // flush the pipe to update the plot
usleep(1000000);// wait a second before updating again
}
// Don't forget to close the pipe
fclose(pipe);
return 0;
}
【问题讨论】: