gnuplot: 一種更為簡潔的曲線,柱狀圖繪圖軟件


gnuplot: 一種更為簡潔的曲線,柱狀圖繪圖軟件

gnuplot簡單介紹

關於gnuplot的簡單性認識可以通過百度百科,上面介紹了在windows以及linux下如何安裝該命令行交互式繪圖工具。該工具的使用使得數據處理和圖形繪制,成為了兩個相對獨立的內容,可以更好的將注意力集中到數據處理或是圖形的繪制。

各種繪圖示例

在一般的數據處理中經常需要繪制的圖形有以下幾類:

  1. 散點圖
  2. 曲線圖
  3. 柱狀圖

另外經常還會改變圖形的各種屬性,如:文字大小,曲線大小,曲線顏色,散點類型,散點大小,坐標刻度,坐標軸標識,添加誤差bar,設置圖形大小,圖形分辨率,在圖像中添加文字,圖像繪制,colorbar設置等等。

下面先來介紹一下線的粗細,點的類型,箭頭的方向,文字位置等的示例內容。

Example

Example

散點圖

# 文件名:spectrum.txt
# 示例光譜數據
500.0000  24.3323
501.0000  24.4242
502.0000  24.3711
503.0000  24.4060
504.0000  24.3026
505.0000  24.3572
506.0000  24.4649
507.0000  24.3331
508.0000  24.3422
509.0000  24.3661
...       ...

接下來就用gnuplot來進行繪制:

plot "spectrum.txt" using 1:2 with points

得到的結果如下:

spectrum image

spectrum image

從繪圖的命令行中,我們可以看到使用gnuplot的命令就像直接使用了英文進行表述的。翻譯成中文就是:從文件spectrum.txt中取得第一行和第二行的數據,采用散點的方式進行繪制。上面的圖中,得到的三點是由加號表示的,那么如果要選用別的類型的點呢。在第一個圖中,已經給出了不同類型的點代表的數字。那么只要在命令行后面添加pointtype 6即可。此時完整的命令為:

plot "spectrum.txt" using 1:2 with points pointtype 6

如果要將右上角的文字替換為“example”,增加title "example"即可。

plot "spectrum.txt" using 1:2 title "example" with points pointtype 6

要額外的添加橫坐標,縱坐標的標示呢。

set xlabel "Wavelength [nm]"
set ylabel "Reflectance Intensity"
plot "spectrum.txt" using 1:2 title "example" with points

那么繪制兩條曲線呢,只要在繪圖plot的那一行的末尾添加相應的繪制內容即可。

set xlabel "Wavelength [nm]"
set ylabel "Reflectance Intensity"
plot "spectrum.txt" using 1:2 title "example1" with points pointtype 6, "" using  1:($2+3) title "example2" with points pointtype 6

結果如下所示:

two spectrum

two spectrum

曲線圖

曲線圖的繪制和散點圖類似,只要將上述命令行中的with points更改為with lines即可。

柱狀圖

假設有一組如下的數據:

# file: data.txt
# Year Red Green Blue
1990 33 45 18
1991 35 42 19
1992 34 44 14
1993 47 15 30
1994 41 14 32
1995 42 20 35

那么如何對該數據進行繪制呢。比較好的展示方式就是分組的bar圖。通過上面兩個例子,很容易想到具體的繪制命令的更改應該將with lines改成相應的with histograms。確實如此,但是只是采用這樣的方式,得到的結果的美觀性還有所欠缺。命令以及結果如下:

plot "data.txt" using 2 title "Red" with histograms, "" using 3 title "Green" with histograms, "" using 4 title "Blue" with histograms

繪制的結果為:

bar

bar

為了增加美觀性,需要在繪制之前對格式進行一定的設定。

# bar1
set style fill solid
set palette rgbformulae 7,5,15
plot "data.txt" using 2 title "A" w histograms palette frac 0.1, "" u 3 t "B" w histograms palette frac 0.5, "" u 4 t "C" w histograms palette frac 0.9

# bar3
set style fill solid
set palette rgbformulae 3,11,6
plot "data.txt" using 2 title "a" w histograms palette frac 0.1, "" u 3 t "b" w histograms palette frac 0.5, "" u 4 t "c" w histograms palette frac 0.9

# bar3
set style fill solid
set palette rgbformulae 33,13,10
plot "data.txt" using 2 title "a" w histograms palette frac 0.1, "" u 3 t "b" w histograms palette frac 0.5, "" u 4 t "c" w histograms palette frac 0.9

bar1: bar1 bar2: bar2 bar3: bar3

可見很容易繪制出不同顏色的bar圖。另外發現圖中的橫坐標的刻度沒有和年份對應起來,想要實現這個改變其實是很簡單的事情。

set style fill solid
set palette rgbformulae 7,5,15
plot "data.txt" using 2:xtic(1) title "A" w histograms palette frac 0.1, "" u 3 t "B" w histograms palette frac 0.5, "" u 4 t "C" w histograms palette frac 0.9
bar4

bar4

還有一種常見的繪圖就是在各個柱狀圖上面添加相應的errorbar。那么上述的數據將進行更改。如下:

# file: data.txt
# Year Red Rederror Green Greenerror Blue Blueerror
1990 33 2 45 4 18 3
1991 35 4 42 3 19 2
1992 34 3 44 6 14 2
1993 47 2 15 2 30 3
1994 41 5 14 2 32 2
1995 42 4 20 4 35 2

實現的命令如下:

set style fill solid
set palette rgbformulae 7,5,15
unset colorbox
set style histogram errorbars lw 2
plot "data.txt" using 2:3:xtic(1) title "A" w histograms palette frac 0.1, "" u 4:5 t "B" w histograms palette frac 0.5, "" u 6:7 t "C" w histograms palette frac 0.9

最后的結果為:

bar5

bar5

結果導出

這一步借用了一個單個的命令文件,內容如下:

set terminal push # 將終端的命令暫時保存
set terminal postscript eps size 8cm, 6cm color solid # 設置導出的文件格式,尺寸
set output "$0"
replot                                         # 重新繪制圖形
set output                                     # 恢復到最初設置
set terminal pop

在終端使用:call "export.gp" "result.ps",就能夠獲得最終導出的圖像。將ps格式的圖形轉換成一定分辨率其他格式的圖形,可以使用’imagemagick convert’軟件,采用的命令如:convert -density 300x300 result.ps result.png

進一步的資料

  1. 《gnuplot in action》,《gnuplot cookbook》,《gnuplot manual》,見傳送門
  2. 官網上的各種示例


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM