【问题标题】:Update gnuplot dataset without files from c++更新 gnuplot 数据集,没有来自 C++ 的文件
【发布时间】: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;
}

【问题讨论】:

    标签: c++ gnuplot pipe


    【解决方案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
    }
    

    此外,您可能还需要设置 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");
    

    【讨论】:

    • 您好,感谢您的意见。我已经进行了上述修改,但有些地方不对劲。我没有绘图窗口,也没有输出到终端。它只是在那里停留了 10 秒,然后返回时没有显示。有什么进一步的见解吗?
    • 您使用的是哪个操作系统?这在带有 Gnuplot 4.4 的 Ubuntu 12.04 上对我来说很好
    • @zotty,很高兴!本质上,绘图窗口每次都会重绘,并且在循环的每次迭代中,您的代码只会给它一个绘制点,这就是为什么您每次只看到一个点。
    • @zotty,我明白了!我没有想到一个可行的解决方案,但我很确定您需要为此使用replot 之类的东西,这样才能不重绘画布。
    • @zotty,干得好!老实说,我认为我从这个问题中学到的东西比我想象的要多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    相关资源
    最近更新 更多