GNUPLOT

Web

gnuplot tips (not so Frequently Asked Questions)  第一章 機能概要 Gnuplot Central http://www.sci.muni.cz/~mikulik/gnuplot.html GnuPlot Manual

GnuPlot manual


gnuplot のページ (Takeno Lab) 


Gnuplotるさんちまん GNUPLOT 日本語リファレンス・ガイド 吉川研究室 グラフは Gnuplot にお任せ GPTCALL
3次元のグラデーション付き等高線図を何で描くか?
 ツールの紹介   Web Server Logs UnignuPlot gnuplot interfaces in ANSI C C 言語でgnuplotを呼ぶ GnuPlot  --not so Frequently Asked Questions--

 

Controlling GNUPLOT from C++

tips

 SCRIPT Examples  コマンド 定数 set 組み込み演算子 関数定義
変数範囲 グラフの表示スタイル terminal  変数設定表示 出力 誤差付きのデータをプロット
線幅、サイズ 最小自乗法 CANDLE  凡例の表示場所を変更 背景を黒にする makefile

 

終了はquit

Examples

sin(x*y) 2次元グラフ 関数定義 2本 title  using
 3lines グラフのタイトルと軸名 XYの範囲を変更する 軸に目盛をいれる test 誤差付きのデータをプロット
2本のY軸          


sin(x*y) splot [0:pi][0:pi] sin(x*y)  
2次元グラフ

plot range exressions

plot [0:10] 1-exp(-x)
関数定義

 

gnuplot> fun(s,t) = A * exp( -s*t)*sin(t)
gnuplot> A = 0.1
gnuplot> plot [t=0:20] fun(1,t) title 's=1', fun (0.1,t) title 's=0.1'
using
gnuplot> plot "aaa.dat" using 1:2 with lines
  0.10  9.0484E-01  9.0484E-01  9.0484E-01
  0.20  8.1873E-01  8.1871E-01  8.1875E-01
  0.30  7.4082E-01  7.4074E-01  7.4091E-01
  0.40  6.7032E-01  6.7010E-01  6.7059E-01
  0.50  6.0653E-01  6.0606E-01  6.0714E-01
                    ....
  4.60  1.0052E-02 -7.0237E-02  5.7632E-01
  4.70  9.0953E-03 -7.2510E-02  6.0325E-01
  4.80  8.2297E-03 -7.4627E-02  6.3077E-01
  4.90  7.4466E-03 -7.6597E-02  6.5886E-01
  5.00  6.7379E-03 -7.8431E-02  6.8750E-01
3lines
gnuplot> plot "aaa.dat" using 1:2 with lines,\
> "aaa.dat" using 1:3 with dots,\
> "aaa.dat" using 1:4 with points

グラフのタイトルと軸名

 

gnuplot> plot "aaa.dat" using 1:2 title "Line1" with lines,\
> "aaa.dat" using 1:3 title "Line2" with dots,\
> "aaa.dat" using 1:4 title "Line3" with points
gnuplot> set xlabel "x"
gnuplot> set ylabel "y=exp(-x)"
gnuplot> set title "TEST GRAPH"
gnuplot> replot
 
XYの範囲を変更する
gnuplot> set xrange[0:2]
gnuplot> set yrange [0:1]
gnuplot> set xrange [0:2]
gnuplot> replot

 

軸に目盛をいれる gnuplot> set xtics 1
gnuplot> set mxtics 5
gnuplot> set ytics 0.5
gnuplot> set mytics 5
gnuplot> replot
test
gnuplot> set term x11
gnuplot> test

 

2本のY軸
gnuplot> set y2tics
gnuplot> set y2range [-5:15]
gnuplot> plot sin(x),cos(x) axes x1y2

 

 


組み込み演算子

演算子 意味
** べき乗
- ~ ! ! 単項負演算子、ビットごと否定、論理否定(!expr)、階乗(!expr)
* / % 乗算、除算、剰算
+ - 加算、減算
== != 等値、非等値
& ビットごとの論理積
^ ビットごとの排他的論理和
| ビットごとの論理和
&& 論理積
|| 論理和
?: 条件付演算子

 

 


コマンド

save  
load load "test.plt"        or   gnuplot test.plt 
exit  
quit  
help  
   
plot 2次元プロット
splot 3次元プロット
replot 再描画
   
shell

!command

gnuplotから一時的にshellに抜けることができます.これには shell コマンドを使います.
exit でshellを終了し,gnuplotのプロンプトに戻ってきます.
1行だけのshellコマンドなら, ! command を使ってgnuplot内から実行することもできます.
pwd

cd

pwd と cd は gnuplotのコマンドラインからでも実行できます.
pwdはカレントディレクトリを表示します.
gnuplotは起動時のディレクトリをカレントディレクトリとします.
別のディレクトリに移動するには cd を使いますが, cd "../" の様に,
ディレクトリ名を必ず引用符で括る必要があります.
   

bind call cd clear
exit fit fit_control help
history if load pause
plot print pwd quit
replot reread reset save
set shell show splot
splot_overview system test unset
update

 

 

set   

(reset ですべてdefault  例外:

the terminal set with `set term`, the output file set with `set
 output` and directory paths set with `set loadpath` and `set fontpath`.

isosamples 解像度
hidden3d 陰線処理
view 視点の設定
  set xlabel "X-AXIS"
  set ylabel "Y-AXIS"
  set xrange [0:5] 
  set yrange [-2:2]
angles  
arrow   
autoscale  
bars  
bmargin  
border   
boxwidth  
cbdata  
cbdtics  
cblabel  
cbmtics  
cbrange  
cbtics  
clabel  
clip  
cntrparam  
colorbox   
contour  
datafile  
date_specifiers  
decimalsign  
dgrid3d  
dummy  
encoding  
fit  
fontpath   
format set format x "%10.3f"
set format y "%T %t %g"      
      
      
<軸>はx、y、z、xy。省略時はxyを意味     
      
Format 意味 (_はblank)
f   小数点表示 %6.3f __6.00
    %10.0f 整数表示
e,E  指数表示 %11.4e _5.0000e+01 
x,X 16進数表示 %x   fffffffb 
o,O  8進数表示 %o  37777766
  仮数部 %t  
  10の冪乗(指数部) %T  
    %g  
       


set format x "" ... 目盛は残して,数字だけを消す

 

grid  set grid
hidden3d  
historysize  
 isosamples  
key  set key left bottom     凡例の表示場所を変更
left 左側
right 右側
top 上側
bottom 下側
outside 図の右側の外
below 図の下側の外
 set nokey  凡例は全部消える
(plotで notitle を指定→指定したものの凡例だけが消える)
 set key 100,100

なら,(X,Y)=(100,100)の位置に表示
一つ目の凡例の,表題と点や線の真中が,この座標
この座標はプロットしている XY軸の数値と一致
label  
lmargin  
loadpath  
locale  
log   
logscale set logscale x
set logscale y
mapping  
margin  
missing  
mouse   
multiplot  
mx2tics  
mxtics  
my2tics  
mytics  
mztics  
   
offsets  
origin   
output  set output "plot.ps"
set terminal postscript とすると,gnuplot はPostScriptの図を生成します.
そのままの状態でplotコマンドを実行すると,出来上がったPostScriptは画面上を流れて行ってしまいます.
この出力先を変更するのが set output です.
outputに続けて出力ファイル名を指定すれば,PostScriptはこのファイルに出力されます.

 

palette  
parametric   
pm3d   
pointsize   
polar  
print   
rmargin   
rrange  
samples  
size   
style  
surface  
term  
terminal      
標準では,gnuplotは画面にグラフを表示します.この出力先を変えて,
プリンタで印刷したりPostScriptの図を作ったりするために使うのが, 
set terminal コマンドです.
gnuplotはターミナル毎に,それに合ったドライバを使って図を描くことで,
機種の依存性を無くしています.
出力可能なターミナルは, set terminal コマンドを実行すると分かります		

 

tics   
ticscale   
ticslevel  
time   
time_specifiers  
timefmt  
timestamp  
title  
tmargin   
trange   
urange  
view   
vrange  
x2data   
x2dtics  
x2label   
x2mtics   
x2range  
x2tics  
x2zeroaxis   
xdata   
xdtics  
xlabel  
xmtics  
xrange  set yrange [1:50]
xtics   set xtics 0,0.1 の様に2つ与えると最初の数字が初期値, 2番目が増分になります.
与えた数字が3つなら,3番目が最終値になります
xzeroaxis  
y2data  
y2dtics   
y2label   
y2mtics  
y2range  
y2tics   set ytics (1,2,5,10,20,50)
y2zeroaxis  
ydata  
ydtics  
ylabel   
ymtics  
yrange  
ytics  
yzeroaxis  
 zdata   
zdtics  
zero  
zeroaxis  
zlabel  
zmtics  
zrange   
ztics
 

 


Available terminal types:
           aed512  AED 512 Terminal
           aed767  AED 767 Terminal
             aifm  Adobe Illustrator 3.0 Format
         bitgraph  BBN Bitgraph Terminal
              cgm  Computer Graphics Metafile
            corel  EPS format for CorelDRAW
             dumb  printer or glass dumb terminal
              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
         epslatex  LaTeX (Text) and encapsulated PostScript
     epson_180dpi  Epson LQ-style 180-dot per inch (24 pin) printers
      epson_60dpi  Epson-style 60-dot per inch printers
      epson_lx800  Epson LX-800, Star NL-10, NX-1000, PROPRINTER ...
              fig  FIG graphics language for XFIG graphics editor
              gif  GIF format [mode] [fontsize] [size] [colors]
             gpic  GPIC -- Produce graphs in groff using the gpic preprocessor
          hp2623A  HP2623A and maybe others
           hp2648  HP2648 and HP2647
           hp500c  HP DeskJet 500c, [75 100 150 300] [rle tiff]
             hpdj  HP DeskJet 500, [75 100 150 300]
             hpgl  HP7475 and relatives [number of pens] [eject]
           hpljii  HP Laserjet series II, [75 100 150 300]
             hppj  HP PaintJet and HP3630 [FNT5X9 FNT9X17 FNT13X25]
           imagen  Imagen laser printer
             jpeg  JPEG images using libgd and TrueType fonts
       kc_tek40xx  MS-DOS Kermit Tek4010 terminal emulator - color
       km_tek40xx  MS-DOS Kermit Tek4010 terminal emulator - monochrome
            latex  LaTeX picture environment
               mf  Metafont plotting standard
              mif  Frame maker MIF 3.00 format
               mp  MetaPost plotting standard
          nec_cp6  NEC printer CP6, Epson LQ-800 [monocrome color draft]
          okidata  OKIDATA 320/321 Standard
              pbm  Portable bitmap [small medium large] [monochrome gray color]
             pcl5  HP Designjet 750C, HP Laserjet III/IV, etc. (many options)
              pdf  PDF (Portable Document File) file driver
              png  PNG images using libgd and TrueType fonts
       postscript  PostScript graphics language [mode "fontname" font_size]
          pslatex  LaTeX picture environment with PostScript \specials
            pstex  plain TeX with PostScript \specials
         pstricks  LaTeX picture environment with PSTricks macros
              qms  QMS/QUIC Laser printer (also Talaris 1200 and others)
            regis  REGIS graphics language
             rgip  RGIP metafile (Uniplex). Option: fontsize (1-8)
          selanar  Selanar
            starc  Star Color Printer
              svg  W3C Scalable Vector Graphics driver
            table  Dump ASCII table of X Y [Z] values to output
      tandy_60dpi  Tandy DMP-130 series 60-dot per inch graphics
          tek40xx  Tektronix 4010 and others; most TEK emulators
          tek410x  Tektronix 4106, 4107, 4109 and 420X terminals
          texdraw  LaTeX texdraw environment
             tgif  TGIF X11 [mode] [x,y] [dashed] ["font" [fontsize]]
         tkcanvas  Tk/Tcl canvas widget [perltk] [interactive]
             tpic  TPIC -- LaTeX picture environment with tpic \specials
          uniplex  RGIP metafile (Uniplex). Option: fontsize (1-8) (Same as rgip)
          unknown  Unknown terminal type - not a plotting device
            vttek  VT-like tek40xx terminal emulator
              x11  X11 Window System
              X11  X11 Window System (identical to x11)
             xlib  X11 Window System (gnulib_x11 dump)
出力 
gnuplot> set term postscript
gnuplot> set output "output.ps"
gnuplot> replot
gnuplot> save "output.plt"
gnuplot> quit

背景を黒にする

gnuplot -rv    #X11 の反転表示


定数

pi


変数範囲

[-3:10] 独立変数xの定義域設定、関数の値域は自動
[-3:10][0:5] 独立変数xの定義域、関数の値域設定
[-3:10][0:] 関数の値域の下限設定
[-3:10][:5] 関数の値域の上限設定
[t=-3:5] 独立変数はtであることの宣言

変数設定表示

gnuplot> a=10
gnuplot> print a
10
gnuplot> a=1+2*sqrt(3)
gnuplot> print log(a)
1.49606798806764
gnuplot> set xrange [-2*pi:2*pi]
gnuplot> a=0.5
gnuplot> plot a*sin(x)

 


 

グラフの表示スタイル

数式の後にwithに続けてスタイル指定

lines plot x with line
points plot sin(x) with points
dots plot x**2 with dots
linespoints plot 1-exp(1-x) with linespoints 
impulses plot x with impulses
boxes plot -x with boxes
steps
plot "aaa.dat" using 1:2 with steps

 

yerrorbars 誤差付きのデータをプロットするには,with yerrorbars
gnuplot> reset
gnuplot> plot "plotexp.dat" index 0:0 using 1:2:3 with yerrorbars,\
> "plotexp.dat" index 1:1 using 1:2:3 with yerrorbars,\
> "plotexp.dat" index 2:2 using 1:2:3 with yerrorbars
gnuplot> 

 

線幅、サイズ

     

 

 

 

PostScriptで出力できる記号と番号の対応

1 2 3 4 5 6 7 8 9 10 11 12 13
×

 


最小自乗法

fit コ マンドを用い,求めたいパラメータを via というオプションに 続けて書く

 

 
gnuplot> fit f(x) "exp.dat" using 1:2:3 via a,b,c,d
 
 
Iteration 0
WSSR        : 96618.1           delta(WSSR)/WSSR   : 0
delta(WSSR) : 0                 limit for stopping : 1e-05
lambda    : 1150.73
 
initial set of free parameter values

    ...

After 17 iterations the fit converged.
final sum of squares of residuals : 3341.93
rel. change during last iteration : -5.29173e-06
 
degrees of freedom (ndf) : 47
rms of residuals      (stdfit) = sqrt(WSSR/ndf)      : 8.43237
variance of residuals (reduced chisquare) = WSSR/ndf : 71.1049
 
Final set of parameters            Asymptotic Standard Error
=======================            ==========================
 
a               = 0.26191          +/- 0.005759     (2.199%)
b               = 0.00251445       +/- 0.0008358    (33.24%)
c               = 0.00541346       +/- 0.0009206    (17.01%)
d               = 0.182469         +/- 0.007329     (4.016%)
 
 
correlation matrix of the fit parameters:
 
               a      b      c      d
a               1.000
b               0.042  1.000
c              -0.229  0.783  1.000
d               0.210 -0.538 -0.768  1.000
gnuplot> replot

 

 


データファイルの表示

plot '1.dat'

1.dat 2.dat 3.dat
2.5
1.2
0.8
-1.3
1.5
0.0 2.5
1.2 1.2

0.0 0.1 1.0
0.1 0.1 0.8

 

#で始まる行は無視されます.


step

各データのブロックの間には2行の空行をいれてあります.データ点は,X座標(エネルギー),Y座標(測定値),Yの誤差の3つの組で与えられています.誤差は絶対値で与えます(つまり測定値と同じ単位を持っているということ).#で始まる行は無視されます.

# Data No. 1
 2.1500E-02 1.3060E+00 5.3098E-02
 2.3900E-02 1.2220E+00 4.7043E-02
 2.6800E-02 1.3430E+00 4.9854E-02
 2.9700E-02 1.2580E+00 4.5860E-02
 3.2500E-02 1.2430E+00 4.4506E-02
               ....
 9.4500E-01 1.2290E+00 3.7317E-02
 1.0350E+00 1.2630E+00 4.1449E-02
 1.1330E+00 1.2670E+00 4.2289E-02


# Data No. 2
 2.4000E-02 1.2970E+00 3.1387E-02
 4.0000E-02 1.3060E+00 2.8993E-02
 6.0000E-02 1.2960E+00 2.8382E-02
 8.0000E-02 1.3300E+00 2.8728E-02
              ....
 7.0000E+00 1.2210E+00 2.5031E-02
 7.2000E+00 1.1990E+00 2.5299E-02
 7.4000E+00 1.1860E+00 2.5618E-02


# Data No.3
 2.2500E-02 1.3310E+00 3.4606E-02
 2.7500E-02 1.3370E+00 2.4066E-02
 3.5000E-02 1.3440E+00 2.6880E-02
              ....
 1.8936E+01 1.0080E+00 2.9232E-02
 2.0064E+01 9.6300E-01 2.9853E-02
 2.1296E+01 1.0310E+00 3.1961E-02

 

一つのファイルに書かれた3つの実験データのブロックは,indexを使ってばらばらにアクセスすることができます. index 0:0 もしくはindex 0なら一番最初のデータ, index 2:2もしくはindex 2 なら3番目のデータです. index 0:1とすると,1と2のデータを合わせたものになります

誤差の値が必要なので,データファイルは3カラムあり,using 1:2:3 としてその3つを読み込みます.誤差の数値が%で与えられている場合は,using 1:2:($2*$3/100.0) とすれば絶対値に変換できます.

see


コメント 

#


CANDLE

open low high close #date 
..... 

というデータを用意して 

plot 'filename' with financebars 
plot 'filename' with candlesticks 

 

=begin 

=== データファイル 

date open low high close 

=== 設定ファイル 

set xdata time 

set timefmt "%Y-%m-%d" 

plot 'test1.dat' using 1:2:3:4:5 with financebars 

=end 

 


script

gnuplot -persist <script>

set xdata time

set timefmt "%Y-%m-%d"

plot 'test1.dat' using 1:2:3:4:5 with candlesticks

$ cat p.plt
#!/usr/local/bin/gnuplot -persist
set xdata time
set timefmt "%Y-%m-%d"
set title " TEST "
plot "output.dat" using 1:2:3:4:5 title "CANDLE" with candlesticks, \
     "output.dat" using 1:6 title "VOLUME" w boxes, \
         "output.dat" using 1:7 title "MACD" w l, \
         "output.dat" using 1:8 title "SIGNAL" w l
#    EOF

 

 

ex1.

gnuplot <script>   ...... -persist なし

#sample1.plt
plot x,sin(x)     # 関数を羅列出来ます
replot cos(x)    # 関数を重ね表示します
pause 5           # 5秒間停止
plot sqrt(x)      # 新たに表示されます。
pause -1 {"return END"}  # 終了するかのダイアログボックス????
# テキスト 終わり    

 

ex.2

#!/usr/local/bin/gnuplot -persist はきかない

# set terminal postscript landscape noenhanced monochrome \
#              dashed defaultplex "Helvetica" 14
# set output 'output.ps'
set xlabel "x" 0.000000,0.000000  ""
set ylabel "y=exp(-x)" 0.000000,0.000000  ""
set title "Pade approximation" 0.000000,0.000000  ""
set xrange [ 0 : 2 ] noreverse nowriteback
set yrange [ 0 : 1 ] noreverse nowriteback
set mxtics 5.000000
set mytics 5.000000
set xtics border mirror norotate 1
set ytics border mirror norotate 0.5
plot "output.dat" using 1:2 title "Analytical" w l, \
     "output.dat" using 1:3 title "L=1, M=2" w l, \
     "output.dat" using 1:4 title "L=2, M=1" w l
#    EOF

Cから呼び出し

#include <stdio.h>
#include <stdlib.h>

#include "fft.h"

int plot_fft(double * gain , double * phase, int samples){
// draw chart in frequency domain
	FILE * fp;
	int i;

	// create output.dat
    fp = fopen("fft.plt","w");
    if(fp==NULL) {
        perror("fft.plt");
        exit(1);
    }
	for(i=0; i< samples; i++){
		fprintf(fp,"%d\t%g\t%g\n",i,gain[i],phase[i]);
	}
	fclose(fp);

	// create fft.plt
    fp = fopen("fft.plt","w");
    if(fp==NULL) {
        perror("p1.plt");
        exit(1);
    }
    fprintf(fp,"set key left top\n");
    fprintf(fp,"set title \"FFT TEST\"\n");
	//  fprintf(fp,"set yrange [%ld:%ld]\n",yrange.low,yrange.high);
    fprintf(fp,"set format y \"%%10.0f\"\n");
    fprintf(fp,"plot \"output.dat\" using 1:2 title \"%s\" with line, \\\n","REAL");
    fprintf(fp,"     \"output.dat\" using 1:3 title \"%s\" with line\n","IMAG");
    fclose(fp);

	system("gnuplot -geometry 640x480+0+0 -persist fft.plt");

	return 0;
}
	

 


makefile

test: $(TARGET).exe p.plt
        ./$(TARGET).exe -o $(TEST_OUT)
        gnuplot -rv -geometry 640x480+0+0 -persist p.plt

p.plt:
        echo set key left top > $@
        echo set title \"FFT TEST\" >> $@
        echo set format y \"%10.0f\" >> $@
        echo plot \"$(TEST_OUT)\" using 1:2 title \"RE\" with line, \\ >> $@
        echo "     "\"$(TEST_OUT)\" using 1:3 title \"IM\" with line >> $@