C语言调用GNUPlot 画图
查了官方资料,GNUPlot 的确是没有提供C语言的API的。但我们可以google到很多关于gnuplot的第三方的调用包,不过,我没去试。
但我们应该知道,gnuplot有一个很强大的地方,就是它支持很多的终端输出。
这个,你可以敲入 set term 命令得到证实。
比如,我的有这些:
gnuplot> set term
Available terminal types:
canvasHTML Canvas object
cgmComputer Graphics Metafile
corelEPS format for CorelDRAW
dpu414Seiko DPU-414 thermal printer
dumbascii art for anything that prints text
dxfdxf-file for AutoCad (default size 120x80)
eepicEEPIC -- extended LaTeX picture environment
emfEnhanced Metafile format
emtexLaTeX picture environment with emTeX specials
epslatexLaTeX picture environment using graphicx package
epson_180dpiEpson LQ-style 180-dot per inch (24 pin) printers
epson_60dpiEpson-style 60-dot per inch printers
epson_lx800Epson LX-800, Star NL-10, NX-1000, PROPRINTER ...
figFIG graphics language for XFIG graphics editor
gifGIF images using libgd and TrueType fonts
hp2623AHP2623A and maybe others
hp2648HP2648 and HP2647
hp500cHP DeskJet 500c,
hpdjHP DeskJet 500,
hpglHP7475 and relatives
hpljiiHP Laserjet series II,
hppjHP PaintJet and HP3630
imagenImagen laser printer
jpegJPEG images using libgd and TrueType fonts
latexLaTeX picture environment
luaLua generic terminal driver
mfMetafont plotting standard
mifFrame maker MIF 3.00 format
mpMetaPost plotting standard
nec_cp6NEC printer CP6, Epson LQ-800
okidataOKIDATA 320/321 Standard
pbmPortable bitmap
pcl5HP Designjet 750C, HP Laserjet III/IV, etc. (many options)
pdfcairopdf terminal based on cairo
pngPNG images using libgd and TrueType fonts
pngcairopng terminal based on cairo
postscriptPostScript graphics, including EPSF embedded files (*.eps)
pslatexLaTeX picture environment with PostScript \specials
pstexplain TeX with PostScript \specials
pstricksLaTeX picture environment with PSTricks macros
qmsQMS/QUIC Laser printer (also Talaris 1200 and others)
starcStar Color Printer
svgW3C Scalable Vector Graphics driver
tandy_60dpiTandy DMP-130 series 60-dot per inch graphics
texdrawLaTeX texdraw environment
tgifTGIF X11 ["font" ]
tikzTeX TikZ graphics macros via the lua script driver
tkcanvasTk/Tcl canvas widget
tpicTPIC -- LaTeX picture environment with tpic \specials
unknownUnknown terminal type - not a plotting device
windowsMicrosoft Windows
wxtwxWidgets cross-platform windowed terminal 2# wayne
于是,我们就可以写一个C程序,重定向输出:#include<stdio.h>
int main()
{
FILE* fp=popen("binary\\gnuplot","w");
fprintf(fp,"set terminal png \n");
fprintf(fp,"set output '2.png'\n");
fputs("plot x*x with filledcurves, 50-x*x with filledcurves, x*x with line lt 1",fp);
fflush(fp);
pclose(fp);
return 0;
}把该C代码 放在 G:\gnuplot 目录下,编译即可得到 png图片
如果把上面的第7行代码换一下,比如 load 'a.plot'
a.plot 里保存着gnuplot脚本, 那么,照样可以生成很不错的图片 libgd 是个好玩意#include <stdio.h>
#include <gd.h>
int main (int argc, char *argv[])
{FILE *in;
FILE *out;
gdImagePtr im;
int radius;
in = fopen("mypicture.jpg", "rb");
if (!in) {
im = gdImageCreateTrueColor(300, 300);
} else {
im = gdImageCreateFromJpeg(in);
fclose(in);
}
if (gdImageSX(im) < gdImageSY(im)) {
radius = gdImageSX(im) / 2;
} else {
radius = gdImageSY(im) / 2;
}
gdImageStringFTCircle(im,gdImageSX(im)/2 ,gdImageSY(im)/2,
radius,radius / 2,0.8,"times",40,"Hello,emath","This is wayne",
gdTrueColorAlpha(240, 240, 255, 32));
out = fopen("2.png", "wb");
if (!out) {
fprintf(stderr, "Can't create 2.png\n");
return 1;
}
gdImagePng(im, out);
fclose(out);
gdImageDestroy(im);
return 0;
} 很好的资料,抽空学习一下。
页:
[1]