顯示具有 gnuplot 標籤的文章。 顯示所有文章
顯示具有 gnuplot 標籤的文章。 顯示所有文章

2011年7月17日

gnuplot -- multiplot tip

不知道我有沒有使用錯方式,multiplot好像無法透過save/load方式來儲存,save的原因我不知道,在load時候似乎會呼叫gnuplot,並不會進入multiplot模式而導致錯誤
於是解決方案就是用call指令,我將底下的文字存成sample.plt
set xrange[-10:20]
set yrange[0:1]
g(x,mean)=exp(-(x-mean)*(x-mean)/v/v/2.0)/sqrt(2.0*pi)/v
v = 2
set style line 1 lt 1
set style line 2 lt 2
set style line 3 lt 3
set multiplot title "The decay model is P{/*0.5 0}/(distance{\^{/Symbol a}}) (P{/*0.5 0})=100.\nThe sample distances are 5m and 10m.\nThe recevied powerunder defferent distance.\n" layout 2,2
set title "{/Symbol a}=1.5"
plot g(x,0),g(x,8.94) lt 2,g(x,3.16) lt 3
set title "{/Symbol a}=1.75"
plot g(x,0),g(x,5.98) ls 2,g(x,1.78) ls 3
set title "{/Symbol a}=2"
plot g(x,0),g(x,4) ls 2,g(x,1) ls 3
set title "{/Symbol a}=2.5"
plot g(x,0),g(x,1.79) ls 2,g(x,0.316) ls 3

unset multiplot 
打算用來觀察高斯分布的特色,另一點有問題的是style line似乎無法很好的運作在wxt+multiplot模式底下
set term wxt enhanced
call 'sample.plt'

2011年7月15日

gnuplot繪製高斯分布

g(x,mean)=exp(-(x-mean)*(x-mean)/g/g/2.0)/sqrt(2.0*pi)/g
gnuplot g(x,0),g(x,1)
 

2011年6月25日

gnuplot in action地圖

如果你是一個希望很快可以開始使用gnuplot工作的人,我建議以下的閱讀順序

chapter 1, 2, 11,第一章讓你知道簡單的gnuplot,接著第二章可以讓你知道更多一點的事情,像是如何用資料來繪圖,第十一章讓你瞭解到各個terminal如何輸出一些必要的圖形跟檔案,你會學到要輸出圖檔並不用關閉gnuplot,也知道eps跟png之間的差異為何
chapter 5,它讓你知道如何挑選不同的圖形的種類,如bar chart/line chart等等,使用適當的圖形種類是很需要的,更多的圖形你可以在gnuplot的demo網頁找到,它的重要性並不輸給第十一章
chapter 6, 7,第六章給你一個圖形的重要元件的觀念,讓你知道xlabel或者arrow等等元素應該會被擺放到圖形的哪個位置,讓你知道如何去調整他

有了以上的概觀,大致上就可以建立一些適當的圖形,還有其他有需要的部分反而是可以反過來說,有這樣的需要再去看
chapter 8, 9,點出3D圖形的建立
chapter 3, 4,指出如何使用gnuplot的資料檔案
chapter 10讓你在一張圖片放入多張統計圖
chapter 11也點出了如何繪製一些特殊符號,如alpha, beta等希臘文字
chapter 12給出了一些有用的macro跟一些環境變數的內容
chapter 13, 14,講解如何用fitting function等等,這方面應該看別的書會有更深入的介紹,這裡只是一個概觀
chapter 15,只有一句話:生存者偏差,要用正確的眼光解讀資料

其實一本書也不能完全說完gnuplot,還有很多方面的需要可以透過google搜尋到,學海無涯,這裡只是開始,不是結束^^預祝各位學習一路順風!!

[心得]gnuplot in action -- chapter 13~15

該書13, 14章講解如何使用一些fitting的功能嘗試去尋找資料的關聯性,不過站在某些科學的觀點,這是不大對勁的。一般由研究者會對所屬領域的特有模型去做一些問題的模型建立,再由數學模型去得到fitting的function,除非該領域沒有特別的模型,所以只能只用圖形資料來"猜測"。
其實這兩張的內容已經有點超出gnuplot作為繪圖工具的範圍,fitting function應該用來解釋推演出來的數學模型跟資料之間的關係,而不是反過來因為資料的樣式而去弄出一個fitting function來當作模型,這樣模型隨時都會隨著資料的特徵而不同
因為如此,這兩章我並沒有深入研究,以後有空再來補好了

最後一張,提出了一個生存偏差的問題,解說圖形的解說方式也會隨著人們的解釋而相異,資料的呈現還是要靠人們正確眼光的解讀

最後,很高興把gnuplot in action這本書看了一回,雖然有些部分並沒有完全筆記下來,但是讓我瞭解到了gnuplot不少東西,然而也對於一些統計圖形的各個部分有了一個大概的瞭解,很有趣的是,大多大學的課程並不教導這樣的東西,也就是很多的科學工具並沒有良好的可成來教導也學生,讓我覺得很可惜。其實如果要把一個學生培養成一個研究者,這方面的訓練並不可少,雖然統計圖形的繪製以及書寫軟體(如latex或者M$ Word)並不是研究的核心,但是這方面的工具是很有用的,我看到亞洲的環境,似乎是把學生推到水裡讓他自然學會游泳這件事情一樣XD個人認為,應該把這類課程加入大學課程,應該有助於提升學生做研究的水準

2011年6月23日

[心得]gnuplot in action -- chapter 12

12章介紹了gnuplot的macro還有呼叫其他程式跟被其他程式呼叫
gnuplot 4.2開始支援string variable,也就是可以被解釋為'string',比方說
file_name='data1.txt'
plot file_name title file_name  #對比 plot 'data1.txt' title 'data1.txt'
又或者以類是函數的方式
file_name(prefix)=prefix.'.txt'
plot file_name('data1')
其中file_name就是被取代成字串(有附帶引號喔),但是如果不是字串的部分,比方如using 1:2就要使用macro,macro會被解釋為命令的一部份
set macro
cols = 'using 1:3'
style= 'with linespoints 3"
plot 'data1.txt' @cols @styles
使用@這個符號就可以呼叫macro但是字串裡面呼叫macro是行不通的,比方
file_name='data1'
plot '@file_name.txt'
書中也給出了一個不錯的例子,如果要使用一個function來產生一段指令來執行可以
export(file)=sprintf("set t push;set t png; set o '%s';replot set o; set t pop",file);
呼叫的方式
set macro
cmd=export('out.png')
@cmd

在gnuplot裡面呼叫shell command使用system這個指令
system "ls"
system "lpr -P laser plot.ps"
如果要取得執行結果就直接assign給字串變數
now=system("date")
如果log file是由時間順序編碼,可以用這樣子取得
其實我覺得由shell script來接手這一段或許是個不錯的選擇,接著就是由其他程式語言來呼叫gnuplot產生圖形,這是個不錯的idea(自我認為XD)

我特別喜歡python,所以這邊只寫有關python的部分,其實書中還有shell script或者perl的呼叫方式,shell大家應該很熟,perl我不喜歡:P
import os
import math
gp=os.popen('/usr/bin/gnuplot', 'w')
gp.write("set output 'graph.png'; set term png;")
gp.write("plot '-u' u 1:2 w lines, '-' u 1:2 w lines\n")
x=-5.0
while (x<=5.0):
  gp.write("%f %f\n" %(x,math.sin(x))
  x+=0.5
gp.write("e\n")
gp.close()
書中也提醒,其實很常常忘記加上\n這個換行符號導致錯誤,這個錯誤不容易發覺,且每次所有設定都要由程式設定一次。我覺得可以把常用的設定寫入某個xxx.plt,然後透過call放進來,再把動態資料產生或者其他動態的部分用python去填補。其實我覺得產生的資料應該由python輸出到檔案,一來可以保留log,二來在輸入很方便,不是每次計算(除非每次資料都不一樣),由python呼叫的好處是,可以直接在一種程式語言內完成所有工作

另外此章節還有提到一些gnuplot的環境變數,比方說GNUTERM、GNUPLOT_FONTPATH...等等,請自行參考書本或網路資料吧。

最後作者demo了如何結合gnuplot繪製一些圖形,直接透過cgi的方式放到網頁上,其實我認為大多數的web script language都有GD function,可以透過這些GD function,繪製一些圖形應改也是輕鬆寫意啦,gnuplot是提供了另外一種選擇

2011年6月22日

[心得]gnuplot in action -- chapter 11

這一章應該早點看到的,如果已經在做圖形輸出的人,其實這一章應該先讀,由我早前的文章可以知道,其實各個terminal之間的差異非常大。其實terminal基本上就可以看成gnuplot輸出的平台,gnuplot雖然後稱支援七八十種的平台,其實常用的就那十來種,而且大致上可以分成三類
  • 圖形格式: 舉凡png/jpeg/gif/bitmap都是
  • 互動格式: 目前4.2加入的wxt/win/x11這類最常用來調校選項跟互動的
  • 印表機格式: 主要是postscript/eps

除了互動格式的terminal之外,大多的terminal都是輸出到檔案,未必是即時輸出。另外一點就是常常使用互動格式terminal繪製圖形完成之後,想切terminal輸出,也是一樣。gnuplot只有在切換terminal的時候或者設定輸出檔案改變的時候,才會輸出檔案內容(flush file context),最後就是最不應該做的關閉gnuplot時候才會輸出。
 所以可以使用set output然強制輸出,一開始切換terminal的時候,內容會輸出至本來的terminal並不是新的terminal的output file

terminal共同的選項基本上有三個size/font/enahnced,size就是畫布大小(這一點postscript有點不一樣),font是指定使用的字型,enhanced則是加強模式,其實目前常用的terminal都支援,主要用以加入一些特殊符號,如geek symbol的pi(3.1415),基本使用格式如下





圖形的terminal語法
set terminal XXX [ size {int:x},{int:y} ] [ [no]crop ]
[ tiny | small | medium | large | giant ]
[ font [ {str:name} [ {int:size} ] ]
| [ "{str:path} [, {int:size} ]" ] ]
[ [no]enhanced ]
[ rounded | butt ]
[ {color0} {color1} {color2} ...]
其中值得注意的是tiny/small/medium/large/giant這一選項,他是一些預設字體大小的設定,tiny是5x8 pixels,small 6x12,medium 7x13(bold),large 8x16,giant 9x15
如果不使用預設的,可以使用set terminal png font "FreeSans,11"這樣的方式,當然字體也可以填入font path,直接指向某個ttf檔案。
png多出了三個重要選項transparent interlace truecolor,這三者不是互斥的,表示是否使用透明,是否用interlace特色(網路才比較用的到),跟使用truecolor,jpeg只有interlace選項


列印terminal語法,postscript可以看這裡,他是有點歷史淵源的,雖然在UI有點過時,但是在當時可以說是設計的相當不錯,目前許多printer依舊可以使用這樣的language
set terminal postscript [ landscape | portrait | eps ]
[ color | mono ] [ blacktext | colortext ]
[ simplex | duplex | defaultplex ]
[ size {flt:x}[in|cm], {flt:y}[in|cm] ]
[ [font] "{str:name}" [ {int:size} ] ]
[ [no]enhanced ]
[ solid | dashed ]
[ linewidth | lw {flt:factor} ]
[ dashlength | dl {flt:factor} ]
[ rounded | butt ]
set terminal postscript [ fontfile [add|delete] "{str:filename}"
| nofontfiles ]
其中重要的是postscript後面的三個設定,可能表示兩種不同的排版,eps是嵌入式圖形比較常用到,color建議要選用mono表示單色(預設是這個:P),當然也使用colortext比較好,這邊特別的是size使用的單位為英吋跟公分,不是pixel,另外有個重要選項就是solid,可讓線條自動變成solid line,postscript除了第一條線是solid line其他開始用不同的dash lines,如果選用solid就會都是solid line,這跟wxt之類的interactive terminal比較一致一點,預設使用的字型是Helvetica。

最後是互動式的terminal,語法分別是
跨平台的wxt,4.2加進來的,覺得相當好用,4.4之後已經變成了預設的terminal

set terminal wxt [ {int:winid} ] [ title "{str:title}" ]
[ [no]enhanced ]
[ font "{str:name} [,{int:size}]" ]
[ [no]persist ] [ [no]raise ] [ [no]ctrl ]
set terminal wxt {int:winid} close

x11 terminal大多linux/unix使用
set terminal x11 [ {int:winid} ] [ title "{str:title}" ]
                 [ [no]enhanced ]
[ font "{str:fontspec}" ]
[ [no]persist ] [ [no]raise ] [ [no]ctrlq ]
[ solid | dashed ]
set terminal x11 [ {int:winid} ] close

aqua,這是mac os常用
set terminal aqua [ {int:winid} ] [ title "{str:title}" ]
                  [ size {int:x} {int:y} ]
[ [no]enhanced ]
[ font "{str:name} [,{int:size}]" ]
[ solid | dashed][dl {flt:dashlength} ]

windows
set terminal windows [ color | monochrome ]
[ [no]enhanced ]
[ font "{str:name} [,{int:size}]" ] 

其他不常用的,書本上也沒啥介紹了 ,我想也懶著看XD