我有一个会产生大量数据的程序。我想每秒绘制一次数据,以便我可以监控它的进度。在下面的示例中,我使用' 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;
}
答案 0 :(得分:1)
首先,您需要将plot
置于循环内,如:
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=100;b<110;b++) // 10 datapoints per plot
{
// this is the bit I can't get right:
fprintf(pipe,"%d, %d\n",(a+1)*b,(a+1)*b*b); // passing x,y data pairs one at a time to gnuplot
// fprintf(pipe, "%d %d\n",b,a); // 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
}
此外,您还可能需要设置xrange
和yrange
,然后才能看到实际情况:
FILE *pipe = popen("gnuplot -persist", "w"); // open pipe to gnuplot
fprintf(pipe,"set xrange [0:1115]\n");
fprintf(pipe,"set yrange [0:122500]\n");