找回密码
 欢迎注册
查看: 21257|回复: 4

[原创] C语言调用GNUPlot 画图

[复制链接]
发表于 2011-2-25 21:06:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?欢迎注册

×
查了官方资料,GNUPlot 的确是没有提供C语言的API的。 但我们可以google到很多关于gnuplot的第三方的调用包,不过,我没去试。 但我们应该知道,gnuplot有一个很强大的地方,就是它支持很多的终端输出。 这个,你可以敲入 set term 命令得到证实。 比如,我的有这些:
gnuplot> set term Available terminal types: canvas HTML Canvas object cgm Computer Graphics Metafile corel EPS format for CorelDRAW dpu414 Seiko DPU-414 thermal printer [small medium large] dumb ascii art for anything that prints text dxf dxf-file for AutoCad (default size 120x80) eepic EEPIC -- extended LaTeX picture environment emf Enhanced Metafile format emtex LaTeX picture environment with emTeX specials epslatex LaTeX picture environment using graphicx package epson_180dpi Epson LQ-style 180-dot per inch (24 pin) printers epson_60dpi Epson-style 60-dot per inch printers epson_lx800 Epson LX-800, Star NL-10, NX-1000, PROPRINTER ... fig FIG graphics language for XFIG graphics editor gif GIF images using libgd and TrueType fonts hp2623A HP2623A and maybe others hp2648 HP2648 and HP2647 hp500c HP DeskJet 500c, [75 100 150 300] [rle tiff] hpdj HP DeskJet 500, [75 100 150 300] hpgl HP7475 and relatives [number of pens] [eject] hpljii HP Laserjet series II, [75 100 150 300] hppj HP PaintJet and HP3630 [FNT5X9 FNT9X17 FNT13X25] imagen Imagen laser printer jpeg JPEG images using libgd and TrueType fonts latex LaTeX picture environment lua Lua generic terminal driver mf Metafont plotting standard mif Frame maker MIF 3.00 format mp MetaPost plotting standard nec_cp6 NEC printer CP6, Epson LQ-800 [monocrome color draft] okidata OKIDATA 320/321 Standard pbm Portable bitmap [small medium large] [monochrome gray color] pcl5 HP Designjet 750C, HP Laserjet III/IV, etc. (many options) pdfcairo pdf terminal based on cairo png PNG images using libgd and TrueType fonts pngcairo png terminal based on cairo postscript PostScript graphics, including EPSF embedded files (*.eps) pslatex LaTeX picture environment with PostScript \specials pstex plain TeX with PostScript \specials pstricks LaTeX picture environment with PSTricks macros qms QMS/QUIC Laser printer (also Talaris 1200 and others) starc Star Color Printer svg W3C Scalable Vector Graphics driver tandy_60dpi Tandy DMP-130 series 60-dot per inch graphics texdraw LaTeX texdraw environment tgif TGIF X11 [mode] [x,y] [dashed] ["font" [fontsize]] tikz TeX TikZ graphics macros via the lua script driver tkcanvas Tk/Tcl canvas widget [perltk] [interactive] tpic TPIC -- LaTeX picture environment with tpic \specials unknown Unknown terminal type - not a plotting device windows Microsoft Windows wxt wxWidgets cross-platform windowed terminal
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2011-2-25 21:29:35 | 显示全部楼层
2# wayne 于是,我们就可以写一个C程序,重定向输出:
  1. #include<stdio.h>
  2. int main()
  3. {
  4. FILE* fp=popen("binary\\gnuplot","w");
  5. fprintf(fp,"set terminal png \n");
  6. fprintf(fp,"set output '2.png'\n");
  7. fputs("plot x*x with filledcurves, 50-x*x with filledcurves, x*x with line lt 1",fp);
  8. fflush(fp);
  9. pclose(fp);
  10. return 0;
  11. }
复制代码
把该C代码 放在 G:\gnuplot 目录下,编译即可得到 png图片
2.png
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2011-2-25 21:31:30 | 显示全部楼层
如果把上面的第7行代码换一下,比如 load 'a.plot' a.plot 里保存着gnuplot脚本, 那么,照样可以生成很不错的图片
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2011-2-25 22:51:46 | 显示全部楼层
libgd 是个好玩意
  1. #include <stdio.h>
  2. #include <gd.h>
  3. int main (int argc, char *argv[])
  4. {FILE *in;
  5. FILE *out;
  6. gdImagePtr im;
  7. int radius;
  8. in = fopen("mypicture.jpg", "rb");
  9. if (!in) {
  10. im = gdImageCreateTrueColor(300, 300);
  11. } else {
  12. im = gdImageCreateFromJpeg(in);
  13. fclose(in);
  14. }
  15. if (gdImageSX(im) < gdImageSY(im)) {
  16. radius = gdImageSX(im) / 2;
  17. } else {
  18. radius = gdImageSY(im) / 2;
  19. }
  20. gdImageStringFTCircle(im,gdImageSX(im)/2 ,gdImageSY(im)/2,
  21. radius,radius / 2,0.8,"times",40,"Hello,emath","This is wayne",
  22. gdTrueColorAlpha(240, 240, 255, 32));
  23. out = fopen("2.png", "wb");
  24. if (!out) {
  25. fprintf(stderr, "Can't create 2.png\n");
  26. return 1;
  27. }
  28. gdImagePng(im, out);
  29. fclose(out);
  30. gdImageDestroy(im);
  31. return 0;
  32. }
复制代码
2.png
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2011-2-26 17:22:13 | 显示全部楼层
很好的资料,抽空学习一下。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-9-8 08:47 , Processed in 0.026598 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表