Skip to content

Instantly share code, notes, and snippets.

@t-nissie
Last active August 29, 2015 14:01
Show Gist options
  • Save t-nissie/623722da9fff4403ee0d to your computer and use it in GitHub Desktop.
Save t-nissie/623722da9fff4403ee0d to your computer and use it in GitHub Desktop.
GNUPLOTのserial evaluationを使う

GNUPLOTのserial evaluationを使う

“Serial evaluation occurs only in parentheses and is guaranteed to proceed in left to right order. The value of the rightmost subexpression is returned. (GNUPLOTのhelp operator binaryより)” 例えば、(y=$3<10 ? $3 : lasty, lasty=$3, y)。 以下のsample.gpでは1つ前の値をlastyに取っておいて、ノイズを消すのに使っている。 データの簡単な微分に使うこともできる→ http://www.ss.scphys.kyoto-u.ac.jp/person/yonezawa/contents/program/gnuplot/diff_data1.html

この文章は Gist https://gist.github.com/t-nissie/623722da9fff4403ee0d に置いてある。 git clone [email protected]:623722da9fff4403ee0d.git serial_evaluationでクローンできる。

GNUPLOTスクリプトとデータファイル

sample.gp:

#!/usr/bin/env gnuplot   # Shebang for UNIX
# See help of: encoding, terminal, ternary, operator binary, datafile, command
# epstopdf.pl: http://www.ctan.org/tex-archive/support/epstopdf
##
set encoding iso_8859_1  # for Angstrom
set terminal postscript portrait enhanced color dashed "Times-Roman" 20
set output "sample.eps"
set xlabel '{/Times-Italic x} [km]'
set ylabel '{/Times-Italic y} [\305]'   # Angstrom
set ytics 1
set size 1.0, 0.4
plot 'sample.dat' using 1:2 title 'column 2' with linespoints lt 1 lw 3 ps 2,\
     'sample.dat' using 1:(y=$3<10 ? $3 : lasty, lasty=$3, y) \
                            title 'column 3' with linespoints lt 2 lw 3 ps 2
set output   # ensure to close the output file
!epstopdf.pl sample.eps

sample.dat:

# Sample data file with a
# blank record and a noise
##
 0  4.1    4.8
 1  3.4    4.0
 2  1.9  123.4  Noise
 3  1.0    2.2
 4  1.1    1.0
 5  1.4    0.7

 6  1.5    0.8
 7  2.6    0.6
 8  2.8    0.5
 9  2.9    0.4
10  2.7    0.2

実行方法と実行結果

UNIXの場合は gnuplot sample.gp 、 Windowsの場合は gnuplot.exe sample.gp などと実行する。 実行結果は図のとおりになる。

figure

図: sample.gpを実行して得られるEPSファイルをJPEG化したもの。

まとめ

  • GNUPLOTをスクリプト言語として実行して
  • データファイルを読み込ませて
  • EPSファイルとしてグラフをプロットした
  • データに空白行があるとデータは線で結ばれない
  • 「Å」を出すこともできる
  • serial evaluationを使ってみた
  • EPSをさらにPDFに自動的に変換した
  • 3項演算子も使ってみた
  • 蛇足: SlideShareの GNUPLOTをうまく使って美麗なフォノンの分散関係を描く方法
# -*-Makefile-*-
##
MD = 00GNUPLOT_with_serial_evaluation.md
all: sample.pdf index.html
sample.pdf: sample.gp sample.dat
gnuplot $<
sample.gp: $(MD)
sed -n "/env gnuplot/,/epstopdf\.pl /p" $< > $@
sample.dat: $(MD)
sed -n "/Sample data file with a/,/10 2/p" $< > $@
index.html: $(MD)
gfm2html.rb --readme -l ja -n 'Takeshi Nishimatsu' -s style.css \
-j https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js $(MD) > $@
clean:
rm -f sample.eps sample.pdf sample.gp sample.dat index.html
/* -*-CSS-*-
* style.css for README.html of feram
* Time-stamp: <2014-05-28 18:31:51 takeshi>
* Author: Takeshi NISHIMATSU
*/
body {
color: black;
font-family: verdana, arial, helvetica, sans-serif;
}
h1, h2, h3, h4, h6 {
font-family: verdana, arial, helvetica, sans-serif;
}
h1 {
color: #dd0000;
background-color: #fff0f0;
font-size: 240%;
}
h2 {
border-top: red 5px solid;
border-bottom: red 1px solid;
padding-left: 8px;
background-color: #fff0f0;
}
h3 {
border-top: red 2px solid;
border-bottom: red 1px solid;
padding-left: 4px;
}
h4 {
border-top: red 1px solid;
padding-left: 4px;
background-color: #fff0f0;
}
h5 {
font-size: larger;
font-family: courier, verdana, arial, helvetica, sans-serif;
padding-top: 10px;
color: darkred;
}
pre {
font-family: monospace, courier, verdana, arial, helvetica, sans-serif;
padding-right: 0.5em;
padding-left: 0.5em;
padding-top: 0.1ex;
padding-bottom: 0.1ex;
margin-left: 0.5em;
margin-right: 1.0em;
white-space: pre;
color: darkred;
background-color: #f3f3f3;
}
div.figure img {
width:50%;
margin: auto;
display: block;
}
div.figure div.figcaption {
width: 60%;
margin: auto;
display: block;
}
div.navi {
text-align: right;
margin-right: 1.0em;
}
div.contents {
margin-left: 10%;
}
img{
width: 30%;
margin: auto;
margin-top: 3.0em;
display: block;
}
figure figcaption{
width: 60%;
margin: auto;
margin-bottom: 3.0em;
display: block;
}
table {
border: blue 2px solid;
text-align: center;
margin: auto;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment