2011年6月17日

[心得]gnuplot in action -- chapter 6

這一章節是極重要的一章,裡面介紹了許多繪圖軟體都用的到的概念,首先看這張圖



整個圖形來說,會有個x, y alexes的框框,叫做border的部分,其中又分成了first跟second兩個部分,first當然就是xrange/yrange旁邊放xlabel跟ylabel,second放x2range/y2range跟x2label/y2label,這樣大致上就知道first跟second是啥意思
接著外面的部分叫做margin,也就是在容納那些xlabel跟ylabel字體的地方
接著graph是指畫圖的部分,坐下角為0,0,這也牽涉到了怎樣繪製label跟arrow(箭頭)在graph部分的座標系統(總得知道原點才知道相對應的位置)

接著介紹arrow(箭頭),用來加強資料說明
先設定箭頭的樣式
set style arrow {idx:index} [ nohead | head | backhead | heads ]
                            [ size {flt:length} [,{flt:angle}]
                                   [,{flt:backangle}] ]
                            [ filled | empty | nofilled ]
                            [ front | back ]
                            [ [ linetype | lt {idx:type} ]
                              [ linewidth | lw {int:width} ]
                              | [ linestyle | ls {idx:style} ] ]
index用來作為設定樣式的代號,nohead/head/backhead/heads分別是沒有箭頭/箭頭在結束的地方/箭頭在開頭/雙箭頭
接著設定箭頭由那裡指向哪裡
set style arrow {idx:index} [ nohead | head | backhead | heads ]
                            [ size {flt:length} [,{flt:angle}]
                                   [,{flt:backangle}] ]
                            [ filled | empty | nofilled ]
                            [ front | back ]
                            [ [ linetype | lt {idx:type} ]
                              [ linewidth | lw {int:width} ]
                              | [ linestyle | ls {idx:style} ] ]

接著有箭頭就必須有些說明的文字,不然只指向某個地方,人家也不知道你是啥用意
set label [{idx:tag}] [ "{str:text}" ] [ at {pos:location} ]
                      [ left | center | right ]
                      [ rotate [ by {int:degrees} ] | norotate ]
                      [ font "{str:name}[,{int:size}]" ]
                      [ [no]enhanced ] [ front | back ]
                      [ textcolor | tc [ {clr:colorspec}
                                         | lt {idx:type}
                                         | ls {idx:style} ] ]
                      [ point lt|pt {idx:pointtype}
                              | ps {idx:pointsize} | nopoint ]
                      [ offset {pos:off} ]


這裡面有個地方沒有標示的就是legend,在gnuplot內使用set key來設定一些相關的係數,legend就是那些每一個資料群組是啥意思的地方,一般來說如果plot sin(x),就會在右上角看到一個"sin(x)"的文字,就是這個意思,接著來看legend選項
set key [ on|off ] [ default ]
        [ [ at {pos:position} ]
          | [ inside | lmargin | rmargin | tmargin | bmargin ] ]
        [ left | right | center ] [ top | bottom | center ]
        [ vertical | horizontal ] [ Left | Right ]
        [ [no]reverse ] [ [no]invert ]
        [ [no]autotitle [columnheader] ] [ [no]enhanced ]
        [ samplen {flt:len} ] [ spacing {flt:factor} ]
        [ title "{str:text}" ]
        [ width {int:chars} ] [ height {int:chars} ]
        [ [no]box [ [ linetype | lt {idx:type} ]
                    [ linewidth | lw {int:width} ]
                    | [ linestyle | ls {idx:style} ] ] ] 
有了上面的概念之後,大概就相對應知道這是如何運作,把legend放到哪邊相對應的位置上了

另外介紹了gnuplot的columnhead,也就是在data file的第一列放入的不是資料,而是對應的field name,範例如下
set style data linesp
set key autotitle columnhead
plot "data" u 1:2, "" u 1:3, "" u 1:4
等同(差別是在data file第一列放入地是否為field name)
plot "data" u 1:2 t "Wheat", "" u 1:3 t "Barley", "" u 1:4 t "Rye"

章節最後給了一個用了許多特色的範例,我覺得非常有學習的效果,讓人理解到如何使用各種工具來讓圖形更清楚

set terminal wxt enhanced
set key top left box
set arrow 1 from -4,.35 to -3.4,0.2
set label 1 "Cusp at x=-{/Symbol p} ?" at -6,0.4
set arrow 2 from 5,-.2 to 5,.95 nohead linetype 4
set label 2 "Singularity at x=5" at 5,1 center
set arrow 3 from -1,0.2 to 1,0.2 heads size 0.1,90
set label 3 "Full Width at\nHalf Maximum" at 0,0.15 center
plot [-7:7][-.2:1.1] "spectrum.dat" u 1:2 t "data" w p, 1/(1+x**2) t "Lorentzian", exp(-x**2) t "Gaussian" 

沒有留言: