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

标签 c pipe gnuplot

我知道 gnuplot 是一个非常好的工具,并且有很多功能,但我只需要它来绘制一个简单的 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;
}

最佳答案

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

#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;
}

关于c - 需要帮助通过 C 管道使用 gnuplot 绘制最简单的 x,y 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28456410/

相关文章:

c - C 中 volatile 关键字的正确使用

c - 显示无边框图片并退出纯c

winapi - DuplicateHandle 的 DUPLICATE_CLOSE_HANDLE 标志有什么意义?

gzip - gnuplot能否透明打开压缩数据文件以节省存储空间?

gnuplot - 在 gnuplot 中使用对数刻度时如何增加抽动次数

c - 使用三重指针将内存分配给 3D 数组

python - 可以用PyCharm Professional替代CLion-主要用于python

python - 从 async.subprocess.PIPE 读取

c - 兄弟子进程之间的管道份额

qt - 通过qt在gnuplot中保存pdf输出