-
Notifications
You must be signed in to change notification settings - Fork 19
/
app_gnuplot.tex
451 lines (371 loc) · 13.4 KB
/
app_gnuplot.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
\subsection{gnuplot}
gnuplot is a famous and widely used command-line program that can
generate two- and three-dimensional plots of functions, data, and data fits.
It dates back to 1986 and runs on all operating systems (Linux, Unix, Microsoft Windows, macOS).
\url{http://www.gnuplot.info/}
\url{http://www.gnuplotting.org/}
\url{http://lowrank.net/gnuplot/index-e.html}
The gray boxes indicate that its content takes place in the terminal.
%----------------------------------------
\subsubsection*{Installing gnuplot}
If you are using Ubuntu, you can install gnuplot as follows:
\begin{mdframed}[backgroundcolor=gray!10]
\begin{verbatim}
> sudo apt-get install gnuplot
\end{verbatim}
\end{mdframed}
%----------------------------------------
\subsubsection*{Interactive use}
In the following pages I explain how to use the gnuplot program from the terminal.
Having done so, in the terminal simply type
\begin{mdframed}[backgroundcolor=gray!10]
\begin{verbatim}
> gnuplot
\end{verbatim}
\end{mdframed}
The following should then appear:
\begin{mdframed}[backgroundcolor=gray!10]
\begin{verbatim}
G N U P L O T
Version 5.2 patchlevel 2 last modified 2017-11-01
Copyright (C) 1986-1993, 1998, 2004, 2007-2017
Thomas Williams, Colin Kelley and many others
gnuplot home: http://www.gnuplot.info
faq, bugs, etc: type "help FAQ"
immediate help: type "help" (plot window: hit 'h')
Terminal type is now 'wxt'
gnuplot>
\end{verbatim}
\end{mdframed}
The prompt means that gnuplot is expecting instructions. We start by making sure that
the terminal type is such that a window appears in this interactive mode. We test this
by plotting a simple function, $f(x)=x$:
\begin{mdframed}[backgroundcolor=gray!10]
\begin{verbatim}
gnuplot> plot x
\end{verbatim}
\end{mdframed}
You should then obtain something similar:
\begin{center}
\includegraphics[width=7cm]{images/gnuplot/gnuplot1}
\end{center}
You can specify the $x$ range, change the function to $x^2+\sqrt{x}$ and label the axes as follows:
\begin{mdframed}[backgroundcolor=gray!10]
\begin{verbatim}
gnuplot> set xlabel 'time'
gnuplot> set ylabel 'cost'
gnuplot> plot[-5:7] x**2+sqrt(x)
\end{verbatim}
\end{mdframed}
\begin{center}
\includegraphics[width=7cm]{images/gnuplot/gnuplot2}
\end{center}
We can also plot functions of both $x$ and $y$ as follows:
\begin{mdframed}[backgroundcolor=gray!10]
\begin{verbatim}
gnuplot> splot x*y, x**2+y
\end{verbatim}
\end{mdframed}
and we get
\begin{center}
\includegraphics[width=7cm]{images/gnuplot/gnuplot3}
\end{center}
Another nice feature in the interactive is the fact that you can use the left button of the mouse
to rotate the plot!
Finally, let us assume that there is a file {\filenamefont results.dat} in the folder and that it contains
results from experimental measurements or numerical values organised in columns as follows:
\begin{verbatim}
1e17 8 0 256000 384000 4.91094e-12 -0.00533647 -714769 0
1e17 32 0 256000 384000 1.96438e-11 -0.0213459 -2.85908e+06 0
1e17 128 0 256000 384000 7.8575e-11 -0.0853835 -1.14363e+07 0
2e17 8 0 256000 384000 3.43871e-12 -0.00533555 -714753 0
2e17 32 0 256000 384000 1.37548e-11 -0.0213422 -2.85901e+06 0
2e17 128 0 256000 384000 5.50193e-11 -0.0853688 -1.1436e+07 0
4e17 8 0 256000 384000 4.13458e-12 -0.00533372 -714720 0
...
67108864e17 128 0 256000 384000 -5.28841e-12 -0.0212269 -1.27701e+07 0
134217728e17 8 0 256000 384000 2.93622e-13 -0.00132619 -798163 0
134217728e17 32 0 256000 384000 1.17449e-12 -0.00530475 -3.19265e+06 0
134217728e17 128 0 256000 384000 4.69795e-12 -0.021219 -1.27706e+07 0
268435456e17 8 0 256000 384000 4.03077e-13 -0.00132594 -798181 0
268435456e17 32 0 256000 384000 1.61231e-12 -0.00530376 -3.19272e+06 0
268435456e17 128 0 256000 384000 6.44923e-12 -0.0212151 -1.27709e+07 0
\end{verbatim}
In this case we may with to plot the 6th column as a function of the 1st one:
\begin{mdframed}[backgroundcolor=gray!10]
\begin{verbatim}
plot 'results.dat' using 1:6 with linespoint linewidth 2 title 'velocity'
\end{verbatim}
\end{mdframed}
\begin{center}
\includegraphics[width=7cm]{images/gnuplot/gnuplot4}
\end{center}
Since typing these instructions time and time again is a bit tedious gnuplot
allows the user to use short versions of these commands:
\begin{mdframed}[backgroundcolor=gray!10]
\begin{verbatim}
gnuplot> plot 'results.dat' u 1:6 w lp lw 2 t 'velocity'
\end{verbatim}
\end{mdframed}
We see that the range of $x$ values spans many order of magnitudes so we wish to use
a logarithmic scale on the $x$-axis.
\begin{mdframed}[backgroundcolor=gray!10]
\begin{verbatim}
gnuplot> set log x
\end{verbatim}
\end{mdframed}
Also, I can combine data with analytical function:
\begin{mdframed}[backgroundcolor=gray!10]
\begin{verbatim}
gnuplot> set log x
gnuplot> plot 'results.dat' u 1:6 w lp lw 2 t 'velocity', 1e7/x lw 3 , 6e-11
\end{verbatim}
\end{mdframed}
\begin{center}
\includegraphics[width=7cm]{images/gnuplot/gnuplot5}
\end{center}
Finally, we may wish to export the plot to a file, say a pdf file. We must then
re-assign the terminal, give a name to the file and re-plot:
\begin{mdframed}[backgroundcolor=gray!10]
\begin{verbatim}
gnuplot> set term pdf
gnuplot> set output 'results.pdf'
gnuplot> plot 'results.dat' u 1:6 w lp lw 2 t 'velocity', 1e7/x lw 3 , 6e-11
\end{verbatim}
\end{mdframed}
You can exit the session by typing
\begin{mdframed}[backgroundcolor=gray!10]
\begin{verbatim}
gnuplot> exit
\end{verbatim}
\end{mdframed}
You should find {\filenamefont results.pdf} in your folder next to {\filenamefont results.dat}.
%----------------------------------------
\subsubsection*{Scripting gnuplot}
Although the interactive approach is very useful its workflow
is not practical if one wishes (for instance) to produce the same plot
with different data, or to communicate a figure to another scientist.
We will therefore now turn to scripting. The idea is simple:
write all the gnuplot commands in a text file, say {\filenamefont script.gnuplot}
and pass this script as argument to gnuplot:
\begin{mdframed}[backgroundcolor=gray!10]
\begin{verbatim}
> gnuplot script.gnuplot
\end{verbatim}
\end{mdframed}
This file contains the following:
\begin{verbatim}
set term pdf font "Times,12pt"
set output 'results.pdf'
set grid
set xlabel 'x'
set ylabel 'cost'
set log x
set title 'my title above the plot'
plot 'results.dat' u 1:6 w lp lw 2 t 'velocity', 1e7/x lw 3 t 'fit' , 6e-11 t 'threshold'
\end{verbatim}
\begin{center}
\includegraphics[width=7cm]{images/gnuplot/results.pdf}
\end{center}
Note that I have added a title to the plot as well.
%----------------------------------------
\subsubsection*{Greek letters}
In order to display Greek letters the {\tt /Symbol} command:
\begin{verbatim}
set xlabel '{/Symbol d}{/Symbol r}'
\end{verbatim}
\begin{tabular}{ll|ll|ll|ll}
\hline
Alphabet&Symbol &Alphabet &Symbol &Alphabet &Symbol &Alphabet &Symbol \\
\hline
\hline
A &Alpha &N &Nu &a &alpha ($\alpha$) &n &nu $\nu$\\
B &Beta &O &Omicron &b &beta ($\beta$) &o &omicron \\
C &Chi &P &Pi &c &chi ($\chi$) &p &pi $\pi$\\
D &Delta &Q &Theta &d &delta ($\delta$) &q &theta $\theta$ \\
E &Epsilon &R &Rho &e &epsilon ($\epsilon$) &r &rho $\rho$\\
F &Phi &S &Sigma &f &phi ($\phi$) &s &sigma $\sigma$\\
G &Gamma &T &Tau &g &gamma ($\gamma$) &t &tau $\tau$\\
H &Eta &U &Upsilon &h &eta ($\eta$) &u &upsilon $\upsilon$\\
I &iota &W &Omega &i &iota ($\iota$) &w &omega $\omega$\\
K &Kappa &X &Xi &k &kappa ($\kappa$) &x &xi $\xi$\\
L &Lambda &Y &Psi &l &lambda ($\lambda$) &y &psi $\psi$\\
M &Mu &Z &Zeta &m &mu ($\mu$) &z &zeta $\zeta$\\
\hline
\end{tabular}
%----------------------------------------
\subsubsection*{piecewise function}
You can define piecewise functions as follows:
\begin{verbatim}
f(x) = x<a ? 1 : 1/0
g(x) = x>=a ? 1 : 1/0
\end{verbatim}
and then use these functions like any function, e.g.:
\begin{verbatim}
plot[-10:12] f(x),g(x)
\end{verbatim}
%----------------------------------------
\subsubsection*{linetype and dashtype}
There are essentially three ways of plotting data:
\begin{verbatim}
plot 'results.dat' u 1:6 w l
plot 'results.dat' u 1:6 w p
plot 'results.dat' u 1:6 w lp
\end{verbatim}
corresponding to (from left to right):
\begin{center}
\includegraphics[width=5cm]{images/gnuplot/results_a.pdf}
\includegraphics[width=5cm]{images/gnuplot/results_b.pdf}
\includegraphics[width=5cm]{images/gnuplot/results_c.pdf}
\end{center}
The following script
\begin{verbatim}
set output 'linetypes.pdf'
plot[][]\
x+0 w l lt 0 t 'linetype 1',\
x+1 w l lt 1 t 'linetype 2',\
x+2 w l lt 2 t 'linetype 3',\
x+3 w l lt 3 t 'linetype 4',\
x+4 w l lt 4 t 'linetype 5',\
x+5 w l lt 5 t 'linetype 6',\
x+6 w l lt 6 t 'linetype 7',\
x+7 w l lt 7 t 'linetype 8',\
x+8 w l lt 8 t 'linetype 9',\
x+9 w l lt 9 t 'linetype 10',\
x+10 w l lt 10 t 'linetype 11',\
x+11 w l lt 11 t 'linetype 12'
\end{verbatim}
generates the following plot:
\begin{center}
\includegraphics[width=7cm]{images/gnuplot/linetypes.pdf}
\end{center}
We see that the colours repeat from linetype 10.
Fortunately we can also combine linetypes with dashtypes.
The following script
\begin{verbatim}
set output 'dashtypes.pdf'
plot[][]\
x+1 w l lt 1 dt 1 t 'linetype 2',\
x+2 w l lt 2 dt 2 t 'linetype 3',\
x+3 w l lt 3 dt 3 t 'linetype 4',\
x+4 w l lt 4 dt 4 t 'linetype 5',\
x+5 w l lt 5 dt 5 t 'linetype 6',\
x+6 w l lt 6 dt 6 t 'linetype 7',\
x+7 w l lt 7 dt 7 t 'linetype 8',\
x+8 w l lt 8 dt 8 t 'linetype 9',\
x+9 w l lt 9 dt 9 t 'linetype 10',\
x+10 w l lt 10 dt 10 t 'linetype 11',\
x+11 w l lt 11 dt 11 t 'linetype 12'
\end{verbatim}
generates the following plot
\begin{center}
\includegraphics[width=7cm]{images/gnuplot/dashtypes.pdf}
\end{center}
and we see that there are only 5 different dash types.
Finally, we turn to point types.
The following script
\begin{verbatim}
set output 'pointtypes.pdf'
plot[][]\
x+0 w p pt 0 ps .5 t 'linetype 1',\
x+1 w p pt 1 ps .5 t 'linetype 2',\
x+2 w p pt 2 ps .5 t 'linetype 3',\
x+3 w p pt 3 ps .5 t 'linetype 4',\
x+4 w p pt 4 ps .5 t 'linetype 5',\
x+5 w p pt 5 ps .5 t 'linetype 6',\
x+6 w p pt 6 ps .5 t 'linetype 7',\
x+7 w p pt 7 ps .5 t 'linetype 8',\
x+8 w p pt 8 ps .5 t 'linetype 9',\
x+10 w p pt 10 ps .5 t 'pointtype 10',\
x+11 w p pt 11 ps .5 t 'pointtype 11',\
x+12 w p pt 12 ps .5 t 'pointtype 12',\
x+13 w p pt 12 ps .5 t 'pointtype 13',\
x+14 w p pt 12 ps .5 t 'pointtype 14'
\end{verbatim}
generates the following plot
\begin{center}
\includegraphics[width=7cm]{images/gnuplot/pointtypes.pdf}
\end{center}
Note that I have used the {\tt ps} command ('point size') to make the points 50\% smaller
than normal.
%----------------------------------------
\subsubsection*{Moving the 'key'}
The default is inside top right, but it can be changed, e.g.:
\begin{verbatim}
set key outside
set key bottom left
\end{verbatim}
corresponding to (from left to right):
\begin{center}
\includegraphics[width=6cm]{images/gnuplot/results_d.pdf}
\includegraphics[width=6cm]{images/gnuplot/results_e.pdf}
\end{center}
%----------------------------------------
\subsubsection*{Plotting arrows}
Let us now turn to the {\filenamefont velocity.dat} file which consists of
four columns: x, y, $\upnu_x$ and $\upnu_y$.
\begin{verbatim}
set output 'velocity_1.pdf'
set xlabel 'x'
set ylabel 'y'
set xtics 0.125
set ytics 0.333333333333
set grid
set size square
plot[0:1][0:1]\
'velocity.dat' u 1:2:3:4 w vectors lt -1 notitle
\end{verbatim}
Note that I have required the plot to be square, that the tics on the
$x$-axis should be spaced 0.125 while the tics on the $y$-axis should be
spaced 0.333.
We obtain the left plot a):
\begin{center}
\includegraphics[width=8cm]{images/gnuplot/velocity_1.pdf}
\includegraphics[width=8cm]{images/gnuplot/velocity_2.pdf}
\end{center}
The arrows are too small, so we scale each vector component by a factor 4.
All we need to do is as follows:
\begin{verbatim}
plot[0:1][0:1]\
'velocity.dat' u 1:2:($3*4):($4*4) w vectors lt -1 notitle
\end{verbatim}
Note the dollar sign which
means that gnuplot takes the value in column 3 or 4 and multiplies it by 4.
The resulting figure is shown in b).
%----------------------------------------
\subsubsection*{Powers of 10}
\begin{verbatim}
set format y "10^{%L}"
\end{verbatim}
%----------------------------------------
\subsubsection*{Least square fit}
Assuming we have a file containing data, e.g. {\tt data.ascii}, that
we want to fit by means of a linear relationship over the range $[-1,+1]$:
\begin{verbatim}
f(x)=a*x+b
fit [-1:1] f(x) 'data.ascii' u 1:2 via a,b
\end{verbatim}
This should return a few lines in the console indicating whether
convergence was reached and then also the $a$ and $b$ values.
In order to plot the line, simply do:
\begin{verbatim}
plot[] 'data.ascii' u 1:2, f(x)
\end{verbatim}
%----------------------------------------
\subsubsection*{coloring areas}
\begin{verbatim}
set style rect fc lt -1 fs solid 0.1 noborder
set obj rect from 0, graph 0 to 15, graph 1
\end{verbatim}
%----------------------------------------
\subsubsection*{vertical line}
To draw a vertical line from the bottom to the top of the graph at x=3, use:
\begin{verbatim}
set arrow from 3, graph 0 to 3, graph 1 nohead
\end{verbatim}
%----------------------------------------------------
\subsubsection*{Show list of all available colors}
In an interactive gnuplot session type:
\begin{verbatim}
> show colors
\end{verbatim}