利用 gnuplot_i 在你的 c 程序中调用 GNUPLOT

这是一篇非常早曾经写的小文章,最初发表于我的搜狐博客(2008-09-23 22:55)。由于自从转移到这里后,sohu 博客就不再维护了,所以把这篇文章也一起挪了过来。

GNUPLOT 是一款功能强大的跨平台的科学数据可视化工具,能够满足大多数的数据显示功能,可是 GNUPLOT 本身没有提供其它语言的接口。尽管我们能够利用操作系统提供的管道(pipe)功能操纵GNUPLOT, 可是这并不是是一件很easy的事情。gnuplot_i 为我们完毕这项工作,它提供了一组简单、友好的程序接口,能够让我们轻松的在程序中调用GNUPLOT 完毕复杂的图形显示功能。

当前版本号的gnuplot_i 包括2个程序文件:
  gnuplot_i.c
  gnuplot_i.h

安装 gnuplot_i 很easy。在此不再描写叙述。读者能够直接參考README 文件。



以下是一个最简单的样例:

#include "gnuplot_i.h"
int main(int argc, char *argv[])
{
    gnuplot_ctrl * h ;
    h = gnuplot_init() ;
    gnuplot_plot_equation(h, "sin(x)", "Sine wave");
    gnuplot_close(h);
}



gnuplot_init() 用来开启一个GNUPLOT 进程,并返回一个handle。
gnuplot_close(h)用来关闭这个GNUPLOT 进程。

gnuplot_plot_equation()用来显示一个函数。

其它接口函数包含:
1。

显示设置函数,包含

gnuplot_setstyle() sets the plotting style of the next plots
gnuplot_set_xlabel() sets the X label for the next plots
 gnuplot_set_ylabel() sets the Y label for the next plots

比如:

gnuplot_setstyle(h, "impulses") ;
gnuplot_set_xlabel(h, "my X label") ;
gnuplot_set_xlabel(h, "my Y label") ;  

style 还能够是 line linepoints step 等。



gnuplot_cmd()函数能够向GNUPLOT 发送不论什么命令。

比如:

char myfile[] = "/data/file_in.dat" ;
int  i ;

gnuplot_cmd(handle, "plot '%s'", myfile);
for (i=0 ; i<10 ; i++) {
gnuplot_cmd (handle, "plot y=%d*x", i);
}

gnuplot_cmd(h, "set terminal postscript") ;
gnuplot_cmd(h, "set output "curve.ps"") ;


利用gnuplot_cmd(),GNUPLOT中全部能够以命令行方式设置的选项都能够实现。

绘图命令包含:

gnuplot_plot_slope() to display a slope
gnuplot_plot_equation() to display an equation
gnuplot_plot_x() to display user-defined 1d data with a single variable. Input is a list of values, assumed regularly spaced on the X-axis.
gnuplot_plot_xy() to display user-defined 1d data with two variables. Provide x and y through two arrays of doubles.
gnuplot_resetplot() requests the current gnuplot display to be cleared before next display is done.

gnuplot_i 的不足

gnuplot_i 并非是毫无缺点,首先 GNUPLOT 中的splot命令在gnuplot_i没有提供对应的接口,GNUPLOT 中的鼠标交互操作也无法利用gnuplot_i控制。因为使用管道,效率并非非常高。当程序异常退出后会留下暂时文件。但总得来说,gnuplot_i 还是能够满足一般的须要的。假设须要很多其它的功能,建议还是使用那些专门的数据图像显示库,比方plplot,qwt 等。
 

原文地址:https://www.cnblogs.com/jhcelue/p/6897249.html