我有一个程序可以产生大量数据。我想每秒绘制该数据,以便我可以监控它的进度。在下面的示例中,我使用“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;
}
最佳答案
好吧,首先,您需要将 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");
关于c++ - 从 C++ 更新没有文件的 gnuplot 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24121617/