【问题标题】:How to plot graphs in Gnuplot in Real time in C++? [closed]如何在 C++ 中实时在 Gnuplot 中绘制图形? [关闭]
【发布时间】:2011-05-25 15:33:00
【问题描述】:

我正在尝试使用 GNUplot 和 C++ 实时绘制图表。 有谁知道这样做的好图书馆? 谢谢

【问题讨论】:

    标签: c++ real-time gnuplot


    【解决方案1】:

    gnuplot 支持通过管道输入(在 Windows 上,有一个单独的可执行文件,pgnuplot)。然后你的程序就可以向 gnuplot 发送新的命令了,比如replot,就像你直接在 gnuplot 界面中输入它们一样。

    如何设置管道连接并从 C++ 程序写入管道的发送端因操作系统而异,因此如果您需要更多帮助,您必须告诉我们您正在使用什么。

    在 Windows 上,有 CreatePipe,然后您设置传递给 CreateProcessSTARTUPINFO 结构的 hStdInput 元素。如果您需要来自pgnuplot 的状态消息,请使用hStdOutput

    在 POSIX(Unix、Linux、Mac OSX 等)上,您可以使用 popen 作为获得单向连接的快捷方式。对于双向,它更像在 Windows 上工作:pipe 获取到末端的句柄,然后 fork 并在子进程中调用 dup2 以将 stdin 和 stdout 与管道关联,然后 exec 拥有 @987654335 @替换子进程,保留你设置的管道。

    编辑:来自the gnuplot documentation

    特殊文件名 '-' 指定 数据是内联的;即,他们 听从命令。只有数据 听从命令; plot 选项,例如 过滤器、标题和线条样式 留在 plot 命令行上。这 类似于 unix shell 脚本中的 using 选项 到这些数据——用它来过滤 他们通过一个函数可能使 有道理,但可能选择列 没有!

    【讨论】:

    • 我想通了,但是发送单个点的命令是什么?我只知道如何告诉它绘制数据文件或函数。如何像 x1 y1、x2 y2 一样逐点绘制?
    • @chutsu:我没有提供这些细节,因为你说你已经知道 gnuplot 部分。无论如何,从文档中添加了相关段落。
    【解决方案2】:

    您是否尝试过gnuplot interfaces in ANSI C?,这是一个 C 接口,但在相同的链接中有一些 C++ 接口。或者你可以试试PlPlot

    【讨论】:

      【解决方案3】:

      如果您对软实时绘图感兴趣,您可能最好使用硬件加速图形 API(例如 OpenGL)并自己绘制图表。

      【讨论】:

      • 我真的希望使用 gnuplot,因为这就是我所知道的,我已经看到了各种使用 perl 将真实数据流式传输到 gnuplot 的接口,但因为这是我的 uni 课程的作业,课程导演真的希望我用 C++ 来做。
      • 定义“实时”对您意味着什么。如果它的意思是“每秒 25 次更新”,那么 gnuplot 可能不适合你。
      • 一秒钟对我来说足够了。
      【解决方案4】:

      看看http://search.cpan.org/~dkogan/feedGnuplot-1.08/bin/feedGnuplot 这是一个命令行实用程序,但如果通过程序控制也能很好地工作。

      【讨论】:

      • 也可用于 Mac OS 的 HomeBrew - brew install feedgnuplot
      【解决方案5】:

      在我的 C++ 代码中,这有效(在 mac OsX mavericks 上,使用 g++ Apple LLVM 5.0 版):

      #include <sys/types.h>
      #include <unistd.h>
      
      ...
      
      // ready to make a plot
      
      pid_t childpid=fork();
      if(childpid==0) {
        // child process makes plot
        std::FILE* pipehandle=popen("gnuplot -persist","w");
        // make some plot. You can send multiple commands to the pipe each ending in \n
        std::fprintf(pipehandle,"plot \"results.txt\" using 1:2 with lines\n");
        std::fprintf(pipehandle,"quit\n");
        std::fflush(pipehandle);
        std::fclose(pipehandle);
        // child process exits
        exit(0);
      }
      // parent process waits for child process to exit
      waitpid(childpid,NULL,0);
      
      // you can now repeat to make other gnuplots; all will appear simultaneously in the 
      // terminal and are persistent after the parent process has finished.
      

      【讨论】:

      • 这应该是可能的,而无需打开外部 gnuplot 应用程序 - 您知道如何在 Windows 中进行的任何更改 - 我知道这是通过。外汇。 ubuntu 上的 sigpack
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 2013-03-22
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多