需要帮助通过 C 管道使用 gnuplot 绘制最简单的 x、y 图

need help in plotting the most simple x,y graph using gnuplot through C pipes

我知道 gnu plot 是一个非常好的工具并且有很多功能,但我只需要它来绘制一个简单的 X 和 Y 图表,其中数据值通过管道从 C 程序提供

在这里,我编写了一个简单的程序来绘制一些值,它在某些系统中运行良好,但在我的系统中却无法运行!!

是的,我确实在一个小时前通过 apt-get 在我的 ubuntu 上安装了 gnuplot,在这个程序执行后仍然没有弹出图形,请帮助我让它工作并且需要它简单。 .谢谢

这是我的代码:

#include<stdio.h>

int main()
{
FILE *p = popen("gnuplot -persist","w");
fprintf(p,"plot 'data.dat' with linespoints\n");
fprintf(p,"%d\t%d\n",100,200);
fprintf(p,"%d\t%d\n",200,400);
fprintf(p,"%d\t%d\n",300,600);
fprintf(p,"e\n");
fclose(p);
return 0;
}

您需要将数据绘制到一个临时文件中,您需要将其指定给 gnu plot 才能实际绘制。在临时文件中写入你的坐标,然后传递命令。

#include <stdlib.h>
#include <stdio.h>
#define COMMANDS 2

int main()
{
    char * commandsForGnuplot[] = {"set title \"My Little Graph\"", "plot 'data.temp'"};
    FILE * temp = fopen("data.temp", "w"); // write coordinates here.
    FILE * gnuplotPipe = popen ("gnuplot -persistent", "w");
    int i;
    fprintf(temp, "%lf %lf \n", 100.0, 200.0); //Write the data to a temporary file
    fprintf(temp, "%lf %lf \n", 200.0, 400.0); 
    for (i=0; i < COMMANDS; i++)
    {
        fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]); //Send commands to gnuplot one by one.
    }
    return 0;
}

好吧,我终于找到了这个错误,它不是我的程序而是 gnuplot..

我不得不安装 gnuplot-x11 和 gnuplot 所以我做了一个 sudo apt-get install gnuplot-x11

现在使用与上面相同的程序,图表会明亮地弹出...

谢谢大家的帮助:)

如果我没理解错的话,您需要在 "plot" 命令字符串中使用特殊文件名“-”,它告诉 gnuplot 读取通过标准输入流传递的值:

#include<stdio.h>

int main()
{    
    FILE *p = popen("gnuplot -persist","w");
    fprintf(p,"plot '-' with linespoints\n");
    fprintf(p,"%d\t%d\n",100,200);
    fprintf(p,"%d\t%d\n",200,400);
    fprintf(p,"%d\t%d\n",300,600);
    fprintf(p,"e\n");
    fclose(p);
    return 0;
}

您问题中的代码忽略了您传递给它的值,而是显示文件 data.dat 的内容。