【问题标题】:need help in plotting the most simple x,y graph using gnuplot through C pipes在通过 C 管道使用 gnuplot 绘制最简单的 x,y 图时需要帮助
【发布时间】:2015-02-11 14:08:30
【问题描述】:

我知道 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;
}

【问题讨论】:

  • 您要绘制文件data.dat 还是要在C 代码中传递给gnuplot 的数字?
  • 我想绘制我通过管道的数字..

标签: c pipe gnuplot


【解决方案1】:

您需要将数据绘制到一个临时文件中,您需要将其指定给 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;
}

【讨论】:

    【解决方案2】:

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

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

    现在用同样的程序,图形弹出来很亮...

    感谢大家的帮助:)

    【讨论】:

      【解决方案3】:

      如果我理解正确,您需要在“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 的内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-07
        • 1970-01-01
        • 2020-02-27
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 2023-03-24
        相关资源
        最近更新 更多