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

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

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

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

安装 gnuplot_i 非常简单,在此不再描述,读者可以直接参考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 等。
 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值