Top: Table of contents
Next: Chapter 6. Input, Output
Previous: Chapter 4.Plotting environment

Chapter 5. Plotting

GNUPLOT 以函數或數據資料為繪製圖形的來源。此兩者皆使用 plot(splot) 指令繪圖,plot 用於繪製 2 維的函數或數據資料;而 splot 則用以繪製 3 維空間 中的曲面。其基本語法為:

    plot {ranges} {<function> | {"<datafile>" {using ...}}}
                  {title} {style} {, <function> {title} {style}...}

    splot {ranges} {<function> | {"<datafile>" {index i} {using ...}}}
                   {title} {style} {, <function> {title} {style}...}

由修改各參數的設定,可產生許多種表達圖形的方式。 其參數的設定大致分為以下四類:

  1. range : 設定圖形所顯示的區域。
  2. function or datafile : 設定所繪的函數或存有 數據資料的檔案。
  3. title : 設定標題內容。
  4. style : 設定顯示圖形的顏色、圖案等。
以下分別描述之。

5.1 Ranges

設定此次 plot(splot) 所顯示的範圍。在設定繪圖環境中亦有 xrange、yrange、 zrange、rrange、trange、urange、vrange 等相似功能的參數可加以設定, 但其影響設定後所有的繪圖狀態。設定 range 的語法為:

    [{<dummy-var> =} {<xmin> : <xmax>}] { [{<ymin> : <ymax>}] }

其中參數 <dummy-var> 為變數名稱 (預設為 X Y,可由 set dummy 而更改), 而 min 和 max 則是設定顯示範圍,均為常數表示式。並非 min 和 max 要同時存在, 不設定表示不改變此項數值。一但設定了 range 之後,會自動將 autoscaling 關閉。 以下是一些範例:

  plot cos(x)                          # 使用目前的 range。
  plot [-10:30] sin(pi*x)/(pi*x)       # 設定 X range。
  plot [t = -10 :30]  sin(pi*t)/(pi*t) # dummy-variable 改成 t,設定 t range。
  plot [-pi:pi] [-3:3]  tan(x), 1/x    # 設定 X 和 Y range。
  # 僅設定 Y range,但關閉 X 軸 、Y 軸 的 autoscaling。
  plot [ ] [-2:sin(5)*-8] sin(x)**besj0(x) 
  plot [:200] [-pi:]  exp(sin(x))      # 僅設定 xmax 和 ymin。
  splot [0:3] [1:4] [-1:1] x*y         # 設定 X、Y、Z range。


5.2 Function

函數為一數學表示式。可以是 GNUPLOT 提供的函數或是自行定義或合成的函數。 若使用參數式 (parametric function),則可能是一對 (使用 plot) 或 3 個 (使用 splot) 數學表示式。要注意的是 dummy variable 必須符合當時所設定的, 一般為 X、Y、Z,若為參數式則為 t (2 維) 或 u v (3 維)。以下是一些範例:

  plot sin(x)   # 描繪數學三角函數 sin(x)。
  splot x * y   # 描繪函數 x*y。


5.3 Datafile

GNUPLOT 依照檔案中的數據,逐點描出圖形。表4 即是一組數據資料。

#
#  datafile.example 
#
0       11.062  9.757   12.667  2.345
2       9.212   8.0908  10.932  1.725
5       8.55    7.6612  9.0388  2.846
10      8.122   7.327   8.617   0.957
15      8.156   7.6101  8.9019  1.577
20      8.122   7.5431  8.8009  3.141
25      7.988   6.3007  9.2753  1.57
27      8.096   6.665   10.555  2.172
29      7.936   6.9395  8.325   0.582
30      7.854   6.4909  9.8171  0.603
35      8.01    7.1556  8.644   1.063
40      8.02    7.0301  9.7099  2.152
45      8.092   6.9164  9.7676  1.44
50      8.072   7.2993  8.4447  1.96
60      8.286   7.7265  8.755   1.0
表4: An example of datafile.(I)

數字之間以空白或 tab 區別。每筆數字為一欄位,每行則由幾個欄位構成, 儲存組數字,描繪數據圖形的基本語法為

    plot "datafile" { using { <ycol> |
                              <xcol>:<ycol> |
                              <xcol>:<ycol>:<ydelta> |
                              <xcol>:<ycol>:<ylow>:<yhigh> |
                              <xcol>:<ycol>:<ylow>:<yhigh>:<boxwidth> }
                            {"<scanf string>"} } ...

    splot "datafile" { using { <xcol>:<ycol>:<zcol> | <zcol> }
                             {"<scanf string>"} } ...

plot "datafile" with lines即能產生如圖23 的 結果。此例中 GNUPLOT 依序用線條連接檔案 "datafile" 內各點數據所定出的座標值。 將第一欄位視為 X 座標值,將第二欄位視為 Y 座標值。表4 為一組資料檔案。參數 <xcol> <ycol> 和 <zcol> 均為在檔案中選擇適當的欄位分別做為 X、Y、Z 的座標值。 如在 plot 語法中第一行僅用 <ycol> 則以檔案中的行數代替 X 座標值 繪出圖形。第二行則為同時標出 X、Y 座標值。第三、四行為畫 errorbars 時 使用 <ydelta>、<ylow> 及 <yhigh>。第五行為畫 boxes 或 boxerrorbars 時使用,可設定 box 的寬度;此時若以 boxes 為圖樣 (即不必使用 <ylow> 及 <yhigh>),亦要設定 <ylow> 及 <yhigh>。圖13 即是使用表4 為 datafile 運用不同 敘述所繪的結果。

set xrange [-1 : 15]
plot "datafile" using 2 
plot "datafile" using 1:2 
set xrange [-3 : 65]
plot "datafile" using 1:2:5 with errorbars
set xrange [-3 : 65]
plot "datafile" using 1:2:3:4 with errorbars

圖 12: Plotting unsorted and sorted data

plot 語法中第六行是採用 C 語言中 scanf 讀入浮點數字的 語法 %f 來表達使用數字的情形。

      plot "datafile" using "%*f%f%*20[^\n]%f" with lines

是忽略第一行欄位的數字,採用第二欄位的數字為 X 座標值,再忽略 20 個 非換行字元的文字,再將接者的數字做為 Y 座標值。這樣就可忽略數字之間的文 字說明了。如 6.2.3 節中由設定輸出為 table 所產生的資料 檔案 "datafile" 。可用

    plot "datafile" using "%*4[^\n]%f%3[^\n]%f" with lines

得到 "datafile" 所描述的圖形。使用 %*4[^\n] 忽略前四個字 "i x=" %*3[^\n] 忽略 " y=" 三個字。

儲存檔案中的數據或許未按大小順序排列者,此時使用 "points" 為圖樣畫出數據 尚無影響。但若使用 "lines" 為圖樣畫出數據,則圖形顯得雜亂。如圖12 左半部,此時可用 unix 指令

    sort -n data.old > data.new 

將數據依順序,由小而大排列。則將圖12 左半部改成圖12 右半部。


圖 12: Plotting unsorted and sorted data

這方法適用於 2 維上的繪圖,至於 3 維上繪圖 則須設定 dgrid3d 此項參數。

在一檔案中亦可包含幾個數值資料圖形,只要在不同數值資料圖形之間以兩行 空白隔開,就可僅畫其中一個數值資料圖形(僅能運用於畫 3D 圖形)。範例如下:

    splot "datafile" index 0 

datafile 內容如表5 ,index 0 表示畫第一個數值資料圖形。 此時若要畫第三個圖形,可使用 index 2。


#  index 0
0  11.062   9.757
2   9.212   8.0908 
     .
     .


#  index 1
10   8.122  7.327
15   8.156  7.6101
      .
      .


#  index 2
25   7.988  6.3007
27   8.096  6.665
      .
      .
表5: An example of datafile.(II)


5.4 Style

GNUPLOT 描繪數據資料圖形是以讀入檔案中的座標值後,以圖樣繪上。 而描繪函數圖形是計算若干點的函數值後,以某種圖樣將函數值繪上。 一般是取樣 100 點及採取線條作為圖樣。GNUPLOT 可提供 9 種圖樣,分別是


圖14: Various plotting style

若改變圖樣,則可得到不同形式的輸出。圖14 以九種圖樣為範例分別 繪出數值資料圖形。其中 errorbars, steps, boxesboxerrorbars 僅能在平面 (2D) 上使用。在設定繪圖環境之中, 亦可由 set function style 和 set data style 分別設定描繪函數與數據資料 的圖形,得到相同的結果。

設定圖樣的語法為

    with <style> {<linetyle> {<pointtype>}}

其中參數 <style> 為 lines, points, linespoints, impulses, dots, steps, errorbars 之中一種, 參數 <linetype> 和 <pointtype> 均為一正數,分別選定繪圖顏色及 畫 points 的符號。GNUPLOT 提供 <linetype> 和 <pointtype> 各六種選擇,供繪圖循環使用。以下舉出一些使用範例:



5.5 Title

一般使用 GNUPLOT 繪圖時,會將函數名稱或存有數據資料的檔案名稱設定為 title。我們可用

    title "<title>"
改變 title。如圖14 中即是運用
    plot "datafile" title "style : lines" with lines
將名稱由描述檔案名稱改為描述圖案名稱。我們亦可使用 "notitle" 使 title 不出現。 如
     plot my_function(x), 1 notitle
不將函數名稱 "1" 顯示在圖形上,而得到 y=1 直線,而這條直線可作為輔助線。



Top: Table of contents Next: Chapter 6. Input, Output