本文介绍如何在gnuplot使用变量,若是字符串数组中的变量含有空格,gnuplot5.0+版本以上才支持,本文给出了如何安装新版本的gnuplot。
1. 问题描述
假设我有两组数据,作成线图放在同一张图片中,用不同的label表示不同的线,定义成一个字符串数组,如下:
titles = "'With XOR' 'Without XOR'"
但问题是,gnuplot 4.6不支持[1],上述的数组titles被视为4个元素,即:With, XOR, Without, XOR。幸运的是gnuplot 5.0+版本支持这个特性。用sudo apt-get install
不能直接安装。以下介绍如何安装。
2. 安装新版本gnuplot
在官网这里下载最新的安装包,安装步骤如下:
#decompress it: tar -xvf gnuplot-5.0.1.tar.gz #install the dependency libraries for cairo-based terminals, like pdfcairo sudo apt-get install libcairo2-dev sudo apt-get install libpango1.0-dev #build it: cd gnuplot-5.0.1 ./configure make #test it: #make check #install it: sudo make install
值得注意的是,如果要用gnuplot产生pdf格式的矢量图片,上述第二步不可少,否则会提示“pdfcairo ‘unknown or ambiguous terminal type’ ”。
分析./configure输出的结果,可以看到缺失的包,如下:
$ ./configure > output_configure.txt 2>&1
$ grep " no" output_configure.txt | grep cairo
Package requirements (cairo >= 0.9.0 pango >= 1.10 pangocairo >= 1.10) were not met:
Package requirements ( cairo >= 1.2 cairo-pdf >= 1.2 pango >= 1.10 pangocairo >= 1.10 glib-2.0) were not met:
configure: WARNING: The cairo terminals will not be compiled.
cairo-based terminals: no (requires cairo>1.2, pango>1.10)
wxt terminal: no (requires C++, wxWidgets>2.6, cairo>0.9, pango>1.10)
可见,缺失cairo和pango,安装之,即可:
sudo apt-get install libcairo2-dev sudo apt-get install libpango1.0-dev
装完gnuplot之后,pdfcario就有了,如下:
gnuplot> set term Available terminal types: cairolatex LaTeX picture environment using graphicx package and Cairo backend canvas HTML Canvas object cgm Computer Graphics Metafile context ConTeXt with MetaFun (for PDF documents) corel EPS format for CorelDRAW 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 epscairo eps terminal based on cairo epslatex LaTeX picture environment using graphicx package fig FIG graphics language for XFIG graphics editor hpgl HP7475 and relatives [number of pens] [eject] latex LaTeX picture environment mf Metafont plotting standard mp MetaPost plotting standard pcl5 HP Designjet 750C, HP Laserjet III/IV, etc. (many options) pdfcairo pdf terminal based on cairo pngcairo png terminal based on cairo postscript PostScript graphics, including EPSF embedded files (*.eps) Press return for more:
3. 完整例子
## filename: plot.plt # gnuplot -e "dir = '$dir'; item = '$item'; dataset = '$dataset'" plot.plt set grid set key top left #set logscale x set xtics (0, 20, 40, 60, 80, 100) #different overlapping intervals XLABEL = "The overlapping interval (s)" YLABEL = "The delivery probability" set title "The delivery probability in different overlapping intervals" set xlabel XLABEL set ylabel YLABEL ########## plot data ############## set terminal pdfcairo size 3.0in,2.25in font ',8' outfile = dir.dataset."_".item.".pdf" set output outfile set style line 1 pt 4 lc rgb 'red' set style line 2 pt 2 lc rgb 'green' routers = "RouterA RouterB" titles = "'With XOR' 'Without XOR'" plot for [i = 1:words(routers)] dir."tmp_".item."_".word(routers, i).".txt" using 2:3 with lp ls i title word(titles, i)
PS: 我花了两个多小时,先是经人提醒,升级到5.0,而后花了不少时间解决不能产生pdf矢量图片。打算以后转向用matplotlib。
参考资料:
[1]StackOverflow: How can I define a string (contain space) array in gnuplot?
[2]StackOverflow: pdfcairo ‘unknown or ambiguous terminal type’ in gnuplot
微信赞赏
支付宝赞赏
Pingback: The ONE使用笔记:MessageStatsReport仿真结果可视化 | | Spark & Shine