gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:20:43 25 4
gpt4 key购买 nike

我知道 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com