【问题标题】:Gnuplot - save outputGnuplot - 保存输出
【发布时间】:2015-02-14 06:00:15
【问题描述】:

我使用 linux,C++。我想保存 gnuplot 的输出。我怎样才能使用 C++ 做到这一点?我试过下面的代码。它生成一个png文件。但它没有情节点。我想做两个任务

  • 在运行时显示图表
  • 程序结束后将图形保存为 gif。

我该怎么做?

FILE *pipe = popen("gnuplot -persist", "w");

// set axis ranges
fprintf(pipe,"set xrange [0:11]\n");
fprintf(pipe,"set yrange [0:]\n");

fprintf(pipe, "set terminal png\n");
fprintf(pipe, "set output 'b.png'\n");
int b = 5;int a;
// to make 10 points
std::vector<int> x (10, 0.0); // x values
std::vector<int> y (10, 0.0); // y values
for (a=0;a<5;a++) // 10 plots
{
    x[a] = a;
    y[a] = 2*a;// some function of a
    fprintf(pipe,"plot '-'\n");
    // 1 additional data point per plot
    for (int ii = 0; ii <= a; ii++) {
        fprintf(pipe, "%d %d\n", x[ii], y[ii]); // plot `a` points
    }

    fprintf(pipe,"e\n");    // finally, e
    fflush(pipe);   // flush the pipe to update the plot
    usleep(1000000);// wait a second before updating again
}

【问题讨论】:

    标签: c++ png gnuplot gif


    【解决方案1】:

    您的问题似乎不是 C 代码本身(或以这种方式控制 gnuplot),因为它运行良好。您可以生成图像,如果省略 set terminalset output 命令,您(至少我)会在屏幕上看到一个 gnuplot 窗口。

    但是,您要绘制的内容并不完全清楚。 从您的代码中,您似乎希望每次生成新的 xy 对时都更新绘图。在这种情况下,请注意几个后续绘图命令的不同终端的不同行为:

    • PNG:文件在第一次绘图后关闭(如您所说,它只显示一个点!)
    • PDF (pdfcairo):多页 PDF,每页一个图
    • 带有选项 animate 的 GIF:动画 GIF,每帧一个图。
    • Window (x11, wxt, ...):每个绘图命令一个接一个地处理,只有最后一个保持可见(您之前可能已经看到其他命令在屏幕上闪烁)

    如果这是您想要的,您可以首先将所有内容绘制到屏幕上(如前所述,没有set terminalset output),最后将最后一个绘图转储到文件中:

    plot sin(x) title 'a curve'          # opens a window on screen and shows curve
    
    set term 'pngcairo'
    set output 'b.png'
    replot                # redo the last plot command
    unset output          # clean closing of file
    

    但如果你想将几个数据集绘制成一个图,你需要一个plot 命令:

    plot '-' title 'first plot', '-' title 'second plot'
    input data ('e' ends) > 1 2
    input data ('e' ends) > 2 3
    input data ('e' ends) > 5 6
    input data ('e' ends) > e
    input data ('e' ends) > 7 8
    input data ('e' ends) > 9 10        
    input data ('e' ends) > 11 12
    input data ('e' ends) > e
    
    
    set terminal pngcairo
    set output 'b.png'
    replot
    unset output
    

    【讨论】:

    • 我可以绘制每秒添加一个点的图形。我想在我的程序完成时保存这个输出。如果可能的话,我想保存 gif 文件。
    • 如前所述,不要在程序开始时发送set terminalset output,而是在程序结束时发送。您将在一个窗口中获得更新的图。最后,使用set terminal gifset outputreplot。 (gnuplot 通常支持 gif)
    • 我添加了它们。 gif 文件创建。我无法显示它。它说“无法加载 gif”。 fprintf(pipe, "设置终端 gif\n"); fprintf(pipe, "设置输出'b.gif'\n"); fprintf(pipe, "replot\n");
    • 首先,您是否也关闭了文件 (unset output)?其次,由于这不是您的C代码的问题,您是否在gnuplot中手动尝试过?您的 gnuplot 版本可能不支持 gif。最后,您的评论很难理解。谁说“无法加载 gif”?
    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多