c++ - 从 C++ 更新没有文件的 gnuplot 数据集

标签 c++ gnuplot pipe

我有一个程序可以产生大量数据。我想每秒绘制该数据,以便我可以监控它的进度。在下面的示例中,我使用“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
}

此外,您可能还想在绘图之前设置 xrangeyrange 以查看实际情况:

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/

相关文章:

c++ - 编译器是否以类似方式对待 int 和 bool 类型?

c++ - 比较 float 的相等计算输出

c++ - 在 C++ 中检查子进程的状态

plot - GNUplot Autoscale 设置 xrange 等于 yrange

c - 管道 - 与多个 fork 的子进程通信

c - 从 1 个管道读取多个写入器

c++ - 使用 Visual Studio 2005 为 Windows NT 4.0 构建?

GnuPlot:堆叠直方图导致悬停条

gnuplot - 如何在 gnuplot 中对整个列求和?

powershell - 通过CMD和PowerShell进行管道传输时,行为和输出不同