Skip to content

Chapter 9: Gnuplot Postprocessing

Source slides: V07_gnuplot.pdf. Exercise: E07_gnuplot.pdf. Sample data & solutions: gnuplot_examples/, E07_gnuplot_exercise/. Lecture script: makeAPlotWGnuplot.sh. Lecture solutions: solutions/solutions scripts.txt, Task 4 solutions/plot_datasets.sh.


1. Chapter Overview

After a CFD simulation finishes you have gigabytes of numbers but the supervisor wants a graph. Gnuplot is the lightweight, scriptable, free plotting tool that every HPC engineer uses. It reads plain x y columns, applies styles, fits curves, draws error bars, and exports PNG/PDF/EPS — all from one-line commands you can put inside Bash scripts.

Why it matters in HPC/CFD: residuals vs iteration, mesh-convergence curves, error bars, log–log scaling, multi-plot dashboards, automated daily reports — all best done in gnuplot because (a) it sits on every HPC node, (b) it's purely text-driven, (c) it's reproducible.

What the examiner asks (very common):

  • "Write a gnuplot command to plot file data.txt columns 1 and 2 as red linespoints."
  • "Add labels, title, grid, and save as PNG."
  • "Write a gnuplot script that fits f(x)=a*x**2+b*x+c to a file."
  • "Set up multiplot 2×2."
  • "Plot data with error bars."
  • "Write a Bash loop that plots every .txt in a folder."

What you must master for top grade:

  • The plot command grammar: plot 'file' using 1:2 with <style> linestyle N title '...'.
  • Customisation triple: set title …, set xlabel …, set ylabel …, set grid, set xrange, set yrange, set key, set xtics.
  • Five styles: lines, points, linespoints, boxes, yerrorbars.
  • Multiplot: set multiplot layout R,C.
  • fit f(x) 'file' via a,b,c.
  • PNG/PDF terminal selection: set terminal png; set output 'x.png'; replot.
  • Bash + heredoc to drive gnuplot.

2. Basics from Zero

A gnuplot session is a sequence of small text commands. You start gnuplot, type plot 'data.txt', and a window pops up with x/y points. Any time you want to change something — title, colour, axis range — you write set ... then replot. To save the graph, you switch the terminal from screen to file (png, pdf, eps), give an output filename, then replot.

$ gnuplot
gnuplot> plot 'linear_data.txt' using 1:2 with linespoints
gnuplot> set title "Simple Linear Plot"
gnuplot> set xlabel "X-Axis"; set ylabel "Y-Axis"
gnuplot> set grid; replot
gnuplot> set terminal png
gnuplot> set output 'plot.png'
gnuplot> replot

You can also write a .gp script (gnuplot script.gp) or pipe into gnuplot from a Bash heredoc.

বাংলায়: Gnuplot-এর কাজের ছন্দটা ধরুন: plot দিয়ে আঁকা, set দিয়ে সাজানো, replot দিয়ে নতুন setting-সহ আবার আঁকা। আর ফাইলে save করতে হলে terminal বদলাতে হয় — terminal মানে এখানে "ছবিটা কোথায় যাবে": পর্দায় (qt), নাকি PNG/PDF ফাইলে। এই terminal → output → replot তিন-ধাপ ক্রমটাই chapter-এর সবচেয়ে গুরুত্বপূর্ণ যান্ত্রিক জ্ঞান।

Real-life analogy. Gnuplot is a command-line Excel chart. You don't drag-drop; you tell it: "plot column 1 vs 2, red line, log-y, label it Pressure".

Real-life HPC use. Every CFD post-processing produces residuals.dat, forces.dat, cl_alpha.dat. A 4-line gnuplot script turns each into a PNG; a Bash loop does it for 200 cases overnight.

What if you misunderstand? You forget set terminal png and set output before replot — you get nothing on disk. Or you use , as separator (CSV) without set datafile separator "," — gnuplot reads zero columns.

বাংলায়: দুটো ভুল বারবার নম্বর খায়: (১) set terminal png আর set output দেওয়ার পর replot না দিলে disk-এ কিছুই লেখা হয় না — ফাইল খালি থাকবে; (২) CSV ফাইলে কমা থাকলে আগে set datafile separator বলে দিতে হবে, নইলে gnuplot কোনো column-ই পড়বে না। পরীক্ষার script-প্রশ্নে এই দুটো লাইন আছে কি না, পরীক্ষক প্রথমেই দেখেন।


3. Hard English Made Easy

Hard Term Simple English বাংলা Example
Terminal Output device (screen, png, pdf) আউটপুট স্থান set terminal png
Output File or window for the graph আউটপুট ফাইল set output 'x.png'
Plot style How to draw points (lines, dots…) পয়েন্ট আঁকার ধরন with lines
Linestyle Reusable style block পুনঃব্যবহারযোগ্য স্টাইল set style line 1 ...
Tics Tick marks on axes অক্ষের চিহ্ন set xtics 1
Mtics Minor tick marks ছোট চিহ্ন set mxtics 2
Key (legend) Legend box লেজেন্ড বক্স set key left top
Multiplot Multiple sub-plots in a grid একসাথে অনেক প্লট set multiplot layout 2,2
Fit Curve fit to data কার্ভ ফিট fit f(x) 'd' via a,b,c
Error bar Show measurement uncertainty ত্রুটির বার with yerrorbars
Label / Annotation Text on the graph চার্টে লেখা set label "x" at 5,10
Heredoc Multi-line input in shell একাধিক লাইন ইনপুট gnuplot <<EOF ... EOF
Datafile separator Char between columns কলাম পৃথককারী , for CSV
Replot Redraw with current settings পুনরায় আঁকা replot
Range Min/max axis values পরিসর set xrange [0:12]

4. Deep Theory Explanation

4.1 Anatomy of plot

plot 'file' using <cols> with <style> linestyle <N> title '<text>'  [, ...]
  • 'file' — data file or function (no quotes for functions).
  • using x:y[:err] — pick columns; using ($1*1e3):($2) allows expressions.
  • withlines, points, linespoints, dots, boxes, impulses, steps, yerrorbars, xyerrorbars, errorlines.
  • linestyle N — refer to a previously defined style.
  • title 'X' — legend label; notitle to hide.
  • , — separates multiple curves on one plot.

Anatomy of a finished gnuplot figure — every label is a set command:

                      set title "Convergence history"
        ┌────────────────────────────────────────────────────┐
        │ 1e-2 ┤●                                ┌─────────┐ │
  set   │      │ ●                               │ KEY     │◄┼─ set key
 yrange │ 1e-3 ┤  ●●                             │ ── res  │ │  right top box
 [1e-6: │      │    ●●●                          └─────────┘ │
  1e-2] │ 1e-4 ┤       ●●●●                                  │
        │      │           ●●●●●●                            │
  (set  │ 1e-5 ┤                 ●●●●●●●●●                   │
logscale│      │                          ●●●●●●●●●●●        │
   y)   │ 1e-6 ┼───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬──  │
        │      0  100 200 300 400 500 600 700 800 900       │
        └────────────────────────────────────────────────────┘
   ▲           set xlabel "iteration"            ▲       ▲
   │                                             │       │
 set ylabel "residual"                  set xtics 100  set xrange [0:1000]
                                        (set mxtics 2 → minor tics)
 Not visible on screen but decides the file on disk:
 set terminal pngcairo size 1000,700   +   set output 'res.png'

বাংলায়: একটা figure-এর প্রতিটা অংশের আলাদা set command আছে: উপরের লেখা title, অক্ষের নাম xlabel/ylabel, axis-এর সীমা xrange/yrange, দাগগুলো xtics/mxtics, আর legend-বাক্সটা key। পরীক্ষায় "fully labelled plot" চাইলে এই ছয়টা command এক নিঃশ্বাসে লিখতে পারতে হবে — কোনটা কোন অংশ সাজায় সেটা উপরের ছবি থেকে গেঁথে নিন।

4.2 Customisations

Command Effect
set title "..." top title
set xlabel "...", set ylabel "..." axis labels
set xrange [a:b], set yrange [a:b] axis range
set logscale y logarithmic y
set grid grid lines
set xtics 1, set mxtics 2 tics + minor tics
set key left top box legend position
set style line N lt 1 lw 2 pt 7 ps 1.5 lc rgb "red" reusable style
set arrow from 4,0 to 4,8 head arrow annotation
set label "x" at 5,10 text at point
set datafile separator "," CSV
set terminal png size 800,600 output type
set output 'x.png' output filename
unset key hide legend
set border 3 hide top/right axes

lt = line type (dash pattern), lw = line width, pt = point type, ps = point size, lc = line color.

4.3 Multiplot

set multiplot layout 2,2 title "multiplot array"
    set title "quadratic"
    plot 'quadratic_data.txt' using 1:2 with linespoints title 'Quadratic'
    set title "exponential"
    plot 'exponential_data.txt' using 1:2 with linespoints title 'Exponential'
    set title "sine"
    plot 'sine_wave_data.txt' using 1:2 with linespoints title 'Sine'
    set title "linear"
    plot 'linear_data.txt' using 1:2 with linespoints title 'Linear'
unset multiplot

Each plot fills the next cell in row-major order.

set multiplot layout 2,2 ─── canvas is split into a 2×2 grid;
                             each successive plot fills the next
                             cell in row-major order:

        column 1                 column 2
┌───────────────────────┬───────────────────────┐
│  cell 1 → 1st plot    │  cell 2 → 2nd plot    │ row 1
│  "quadratic"          │  "exponential"        │
├───────────────────────┼───────────────────────┤
│  cell 3 → 3rd plot    │  cell 4 → 4th plot    │ row 2
│  "sine"               │  "linear"             │
└───────────────────────┴───────────────────────┘

unset multiplot ─── back to one full-canvas plot; forgetting this
                    makes the NEXT plot land in a leftover cell

বাংলায়: Multiplot মানে এক canvas-এ কয়েকটা ছোট plot — layout 2,2 দিলে চারটা ঘর তৈরি হয় আর প্রতিটা plot command পরের ঘরটা ভরে, বাঁ থেকে ডানে, উপর থেকে নিচে (row-major)। প্রতিটা ঘরের আগে set title দিয়ে আলাদা শিরোনাম দেওয়া যায়। শেষে unset multiplot দিতে ভুললে পরের plot-টা আবার ছোট ঘরেই আটকে যাবে — এটা একটা চেনা ফাঁদ।

4.4 Curve fitting

f(x) = a*x**2 + b*x + c
fit f(x) 'quadratic_data_with_noise.txt' using 1:2 via a,b,c
plot 'quadratic_data_with_noise.txt' using 1:2 title "Data" with points, \
     f(x) title sprintf("Fit: y = %.2fx² + %.2fx + %.2f", a, b, c) with lines

Gnuplot uses the Marquardt–Levenberg algorithm. fit.log records the iterations, residuals, asymptotic errors. The lecture's example shows fitted parameters a=0.5217, b=1.1905, c=1.1797 with reduced chi-square 0.035. The exact quantity being minimised is derived in §4.9.

বাংলায়: fit command-এর কাজ: আপনি model-এর আকার দেন (যেমন \(a\cdot x^{2}+b\cdot x+c\)), gnuplot data-র সাথে সবচেয়ে ভালো মেলে এমন a, b, c খুঁজে বের করে — "via a,b,c" মানেই এই তিনটা ঘুরিয়ে দেখা হবে। ফলাফল আর প্রতিটা parameter-এর error যায় fit.log ফাইলে। মনে রাখুন: algorithm-এর নাম Levenberg–Marquardt, আর এটা least squares (নিচের §4.9) minimize করে — দুটোই written exam-এ সরাসরি আসে।

4.5 Error bars

Three columns x y y_err:

plot 'data_with_errors.txt' using 1:2:3 with yerrorbars title "Data ± σ"

For asymmetric errors use 1:2:3:4 and xyerrorbars.

4.6 3D plots

splot 'mesh.dat' using 1:2:3 with pm3d

Or analytical:

splot sin(x)*cos(y)

Add set hidden3d for hidden-line removal, set view 60,30 to rotate.

4.7 Output formats

set terminal pngcairo size 1000,700 enhanced font 'Helvetica,12'
set output 'residuals.png'
replot

set terminal pdfcairo size 8cm,6cm
set output 'residuals.pdf'
replot

set terminal eps enhanced color
set output 'residuals.eps'
replot

বাংলায়: Format বাছাইয়ের নিয়ম সহজ: PNG হলো raster (pixel-ভিত্তিক) — slide আর দ্রুত দেখার জন্য; PDF/EPS হলো vector — যত বড় করুন ততই ধারালো, তাই thesis বা paper-এর জন্য pdfcairo সেরা। যে format-ই হোক, ক্রম একটাই: আগে set terminal, তারপর set output, সবশেষে replot।

4.8 Diagram from V07

  • The "plot" workflow diagram: data file → using → style → title → axes → terminal → output. Write: "gnuplot is a pipeline that ingests text, applies styling, and emits a chosen terminal."
  • Multiplot grid 2×2 with four sub-plots.
  • fit log with iterations vs \(\chi^2\).

4.9 What fit minimises — least squares, stated precisely

Given \(N\) data points \((x_i, y_i)\) and a model \(f(x; a, b)\) with free parameters \(a, b\), the fit command minimises the residual sum of squares (RSS):

\[S(a,b) = \sum_{i=1}^{N} \bigl( y_i - f(x_i; a, b) \bigr)^2\]

If a third column of uncertainties \(\sigma_i\) is supplied (using 1:2:3), it minimises the weighted form (chi-square):

\[\chi^2(a,b) = \sum_{i=1}^{N} \left( \frac{y_i - f(x_i; a, b)}{\sigma_i} \right)^2\]

Because \(S\) is generally nonlinear in the parameters, there is no closed-form solution; gnuplot minimises it iteratively with the Levenberg–Marquardt algorithm (a damped blend of gradient descent and Gauss–Newton). It converges to a local minimum — which is why sensible initial guesses matter. fit.log reports the final RSS, the reduced chi-square \(\chi^2/(N - p)\) (with \(p\) = number of fitted parameters), and asymptotic standard errors for each parameter.

Fully worked numeric example. Data: \((1, 2.1)\), \((2, 3.9)\), \((3, 6.2)\). Candidate model \(f(x) = a x\).

  1. Try \(a = 2\): residuals \(= 2.1 - 2 = 0.1\); \(3.9 - 4 = -0.1\); \(6.2 - 6 = 0.2\). \(S(2) = 0.1^2 + (-0.1)^2 + 0.2^2 = 0.01 + 0.01 + 0.04 = 0.06\).
  2. Try \(a = 2.05\): residuals \(= 0.05\); \(-0.20\); \(0.05\). \(S(2.05) = 0.0025 + 0.04 + 0.0025 = 0.045 < 0.06\) — better.
  3. Levenberg–Marquardt automates exactly this search until \(S\) stops decreasing.
            THE FIT WORKFLOW LOOP
┌──────────────────────────────┐
│ 1. define the model          │
│    f(x) = a*x**b             │
└──────────────┬───────────────┘
┌──────────────────────────────┐
│ 2. set initial guesses       │
│    a = 1; b = 1              │
└──────────────┬───────────────┘
┌──────────────────────────────┐      ┌───────────────────────────┐
│ 3. fit f(x) 'data' via a,b   │ ───► │ Levenberg–Marquardt       │
│                              │      │ iterates, minimising RSS  │
└──────────────┬───────────────┘      └───────────────────────────┘
┌──────────────────────────────┐  errors huge / not converged
│ 4. check fit.log:            │ ─────────────────────────────┐
│    RSS, reduced chi², a ± da │                              │
└──────────────┬───────────────┘                              ▼
               │ looks good                      back to step 1 or 2:
               ▼                                 better model or guesses
┌──────────────────────────────┐
│ 5. plot data + fitted f(x)   │
│    and judge by eye          │
└──────────────────────────────┘

বাংলায়: fit আসলে একটা অঙ্ক minimize করে: প্রতিটা data point-এ (আসল y − model-এর f(x))-এর বর্গ যোগ করে যে যোগফল S পাওয়া যায়, সেটাই residual sum of squares — S যত ছোট, fit তত ভালো। Parameter-এর মধ্যে সম্পর্ক nonlinear বলে সরাসরি সমাধান নেই, তাই Levenberg–Marquardt ধাপে ধাপে S কমায়। পরীক্ষায় "What does fit minimize?" এলে সূত্রটা লিখে algorithm-এর নাম বলুন — এই দুই লাইনেই পুরো নম্বর।

4.10 Log-scale mathematics — straight lines from curves

Power law ⇒ straight line on log-log. If \(y = a x^b\), taking \(\log_{10}\) of both sides:

\[\log y = \log a + b \log x\]

On log-log axes this is a straight line with slope \(b\) and intercept \(\log a\). The slope between any two points is

\[b = \frac{\log y_2 - \log y_1}{\log x_2 - \log x_1}\]

Fully worked example. Points \((10, 500)\) and \((100, 5000)\):

  1. \(\log_{10} 500 = 2.699\), \(\quad \log_{10} 5000 = 3.699\).
  2. \(\log_{10} 10 = 1\), \(\quad \log_{10} 100 = 2\).
  3. \(b = \dfrac{3.699 - 2.699}{2 - 1} = \dfrac{1}{1} = 1\).
  4. Prefactor: \(a = y / x^b = 500 / 10^1 = 50\). The data obey \(y = 50x\).

Exponential ⇒ straight line on semilog-y. If \(y = a e^{bx}\), taking the natural log:

\[\ln y = \ln a + b x\]

Straight on a semilog-y plot (log y-axis, linear x-axis), slope \(b\). Worked check: \((0, 3)\) and \((2, 22.17)\): \(b = \dfrac{\ln 22.17 - \ln 3}{2 - 0} = \dfrac{3.099 - 1.099}{2} = 1.0\), and \(a = y(0) = 3\), so \(y = 3e^{x}\).

Memorise the diagnostic pair: straight on log-log ⇒ power law; straight on semilog-y ⇒ exponential.

CFD application — order of accuracy from a grid-convergence study. A discretisation of order \(p\) has error \(E(h) = C h^p\), so \(\log E = \log C + p \log h\): on log-log axes, the slope of error vs mesh size \(h\) is the order of accuracy \(p\).

Fully worked example: a solver gives \(E_1 = 4.0 \times 10^{-3}\) at \(h_1 = 0.02\) and \(E_2 = 1.0 \times 10^{-3}\) at \(h_2 = 0.01\):

\[p = \frac{\log(E_1/E_2)}{\log(h_1/h_2)} = \frac{\log 4}{\log 2} = \frac{0.602}{0.301} = 2\]

The scheme is second-order accurate. The same extraction in gnuplot:

set logscale xy
set xlabel "h"; set ylabel "error"
E(x) = C * x**p
C = 1; p = 1                       # initial guesses
fit E(x) 'convergence.dat' using 1:2 via C,p
plot 'convergence.dat' u 1:2 w p t 'measured error', \
     E(x) w l t sprintf("fit: p = %.2f", p)

বাংলায়: Log-scale-এর জাদুটা হলো বাঁকা রেখাকে সোজা করা: power law (\(y = ax^b\)) log-log-এ সরলরেখা হয় আর ঢালটাই b; exponential (\(y = ae^{bx}\)) semilog-y-তে সরলরেখা হয়। CFD-তে এর সবচেয়ে দামি ব্যবহার grid-convergence: mesh size h-এর বিপরীতে error-এর log-log ঢালই scheme-এর order of accuracy p — ঢাল ২ মানে second-order। দুটো বিন্দু থেকে ঢাল বের করার সূত্রটা (\(\Delta\log y / \Delta\log x\)) হাতে-কলমে প্র্যাকটিস করুন, পরীক্ষায় calculator ছাড়াই করতে হতে পারে।


5. Command / Syntax / Code Breakdown

plot 'data.txt' using 1:2 with linespoints title 'data'

Purpose: basic 2-column plot. Style linespoints shows points connected by lines.

set title "Simple Linear Plot"

Purpose: main title.

set xrange [0:12]

Purpose: lock x-axis range.

set logscale y

Purpose: log-y plot — perfect for residuals.

set style line 1 lt 1 lw 2 pt 7 ps 1.5 lc rgb "red"

Purpose: reusable named line style.

set key outside right top box

Purpose: legend placement and box around it.

set xtics 1; set mxtics 2

Purpose: major tic every 1 unit, 2 minor tics between.

set arrow from 4,0 to 4,8 head

Purpose: arrow annotation with head.

set label "Data Point" at 5,10

Purpose: floating text.

set multiplot layout 2,2 title "..."

Purpose: start a 2x2 grid; finish with unset multiplot.

f(x) = a*x**2 + b*x + c; fit f(x) 'd' via a,b,c

Purpose: curve fit.

plot 'd' using 1:2:3 with yerrorbars

Purpose: error bars.

set terminal png; set output 'x.png'; replot

Purpose: export PNG.

gnuplot <<EOF ... EOF

Purpose: drive gnuplot from a Bash script (heredoc).


6. Mandatory Practical Examples

Example 6.1 — Simple linear plot (E07 Task 1, lecture solution verbatim)

Purpose

Plot linear_data.txt and customise.

Input

linear_data.txt:

1   2
2   4
3   6
4   8
5   10
6   12
7   14
8   16
9   18
10  20

Code

plot 'linear_data.txt' using 1:2 with linespoints
set title "Simple Linear Plot"
set xlabel "X-Axis"
set ylabel "Y-Axis"
replot
set style line 1 lt 1 lw 2 pt 7 ps 1.5
plot 'linear_data.txt' using 1:2 with linespoints linestyle 1
set xrange [0:12]
set yrange [0:22]
set grid
replot
set key left top
replot
set label "Data Point" at 5,10
set arrow from 4,0 to 4,8 head
replot
set terminal png
set output 'linear_plot.png'
replot

Expected Output

A PNG file linear_plot.png with red line+markers, grid, label "Data Point", and an arrow.

Step-by-Step Explanation

  • plot ... linespoints first quick visualise.
  • set title/xlabel/ylabel add textual context.
  • replot redraws with the current state.
  • set style line saves a numbered style.
  • set xrange/yrange lock axes.
  • set key left top move legend.
  • set label / set arrow annotations.
  • set terminal png; set output; replot saves to disk.

Real-Life HPC/CFD Meaning

Plotting iter vs residual from a CFD log file follows the exact same recipe.

Written Exam Relevance

Often a 5–8 mark question: "Customise a basic linear plot." The minimal answer is title + xlabel + ylabel + grid + range + PNG export.

Example 6.2 — Multi-dataset on single plot (E07 Task 2.1)

set title "Advanced Customizations with Multiple Datasets" font "Arial,14"
set xlabel "X-Axis" font "Arial,12"
set ylabel "Y-Axis" font "Arial,12"
set key outside right top box
set style line 1 lt 1 lw 2 lc rgb "red"   pt 7 ps 1.5
set style line 2 lt 2 lw 2 lc rgb "blue"  pt 5 ps 1.5
set style line 3 lt 3 lw 2 lc rgb "green" pt 9 ps 1.5
set style line 4 lt 4 lw 2 lc rgb "yellow" pt 8 ps 1.5
plot 'quadratic_data.txt'   using 1:2 with linespoints linestyle 1 title 'Quadratic Growth', \
     'exponential_data.txt' using 1:2 with linespoints linestyle 2 title 'Exponential Growth', \
     'sine_wave_data.txt'   using 1:2 with linespoints linestyle 3 title 'Sine Wave', \
     'linear_data.txt'      using 1:2 with linespoints linestyle 4 title 'Linear Growth'
set xrange [0:12]; set yrange [-1:25]
set xtics 1; set ytics 5; set mxtics 2; set mytics 5
set grid xtics ytics mxtics mytics linestyle 1 lc rgb "gray" lw 1
replot
set terminal png; set output 'multipledataset.png'; replot

Example 6.3 — Multiplot 2×2 (E07 Task 2.2)

set multiplot layout 2,2 title "multiplot array"
    set title "quadratic growth";  plot 'quadratic_data.txt'   using 1:2 with linespoints title 'Quadratic'
    set title "exponential growth"; plot 'exponential_data.txt' using 1:2 with linespoints title 'Exponential'
    set title "sine wave";          plot 'sine_wave_data.txt'   using 1:2 with linespoints title 'Sine'
    set title "linear growth";      plot 'linear_data.txt'      using 1:2 with linespoints title 'Linear'
unset multiplot
set terminal png; set output 'multiplot.png'; replot

Each plot fills the next cell in row-major order.

Example 6.4 — Mathematical function (E07 Task 3.1)

set title "Plot of y = sin(10x)"
set xlabel "X"
set ylabel "Y"
plot sin(10*x) title "sin(10x)" with lines
set terminal png; set output 'mathematical_function.png'; replot

Example 6.5 — Curve fitting (E07 Task 3.2)

set title "Quadratic Curve Fitting"
set xlabel "X"; set ylabel "Y"
f(x) = a*x**2 + b*x + c
fit f(x) 'quadratic_data_with_noise.txt' using 1:2 via a,b,c
plot 'quadratic_data_with_noise.txt' using 1:2 title "Data" with points, \
     f(x) title sprintf("Fit: y = %.2fx² + %.2fx + %.2f", a, b, c) with lines

The lecture's fitted values: a=0.5217, b=1.1905, c=1.1797, reduced \(\chi^2 = 0.035\) — quote fit.log in the exam.

বাংলায়: fit-প্রশ্নের তিনটা ধাপ: (১) ফাংশনের আকার declare — f(x)=a*x**2+b*x+c, (২) fit ... via a,b,c — gnuplot তখন least-squares-এ parameter খোঁজে, (৩) data + fitted curve একসাথে plot, sprintf দিয়ে legend-এ মান বসানো। fit.log-এ iteration আর error থাকে — ওটার নাম নিলে extra credit।

Example 6.6 — Error bars (E07 Task 3.3)

set title "Data with Error Bars"
set xlabel "X"; set ylabel "Y"
plot 'data_with_errors.txt' using 1:2:3 with yerrorbars title "Data with Errors"

Example 6.7 — Bash automation (E07 Task 4 — plot_datasets.sh)

#!/bin/bash
DATA_DIR="./datasets"

for file in $DATA_DIR/*.txt; do
    filename=$(basename -- "$file")
    filename_no_ext="${filename%.*}"
    gnuplot << EOF
        set title "Plot of $filename_no_ext"
        set xlabel "X"; set ylabel "Y"
        set terminal png
        set output "$DATA_DIR/$filename_no_ext.png"
        plot '$file' using 1:2 title "$filename_no_ext" with linespoints
EOF
    echo "Plot generated for $filename_no_ext"
done

Step-by-step: the loop iterates each text file; basename + ${filename%.*} strip path & extension; the unquoted heredoc lets bash substitute $file into the gnuplot program.

Real-Life HPC/CFD Meaning. Identical pattern for "plot residuals of every case in a sweep".

বাংলায়: Bash-ভেতরে-gnuplot প্রশ্নের কেন্দ্রটা heredoc: gnuplot <<EOF ... EOF। EOF unquoted রাখলে bash আগে $file-এর মতো variable বসিয়ে দেয় — এটাই চাই; quoted (<<'EOF') দিলে gnuplot পাবে আক্ষরিক $file — কিছুই আঁকবে না। এই এক সূক্ষ্মতাই Task 4-এর প্রাণ।

Example 6.8 — Filter then plot (lecture script makeAPlotWGnuplot.sh)

#!/bin/bash
input_file="data_to_filter.txt"
output_file="preprocessed_data.txt"

awk '$2 >= 3' "$input_file" | sort > "$output_file"

gnuplot <<EOF
set terminal png
set output 'filtered_sorted_plot.png'
set title "Filtered and Sorted Data"
set xlabel "X-axis"; set ylabel "Y-axis"
set xrange [0:12]; set yrange [0:12]
set grid
plot "$output_file" using 1:2 with linespoints title 'Filtered Data', \
     "$input_file" using 1:2 with points       title 'Data points'
EOF

rm "$output_file"

Demonstrates awk → sort → gnuplot in one script.


7. Real HPC/CFD Workflow

# 1. Extract residuals from log
awk '/^Time =/ {print $3, $7}' run.log > residuals.dat

# 2. Plot script
cat > plot_res.gp <<'EOF'
set logscale y
set xlabel "time [s]"
set ylabel "residual"
set title "Convergence"
set grid
set terminal pngcairo size 1000,600 enhanced
set output 'residuals.png'
plot 'residuals.dat' using 1:2 with linespoints lt 1 lw 2 pt 7 ps 1 title 'continuity'
EOF

# 3. Run
gnuplot plot_res.gp

# 4. Show
xdg-open residuals.png        # Linux

For 200 cases:

for d in case_*/; do
    awk '/^Time =/ {print $3, $7}' "$d/run.log" > "$d/residuals.dat"
    gnuplot -e "datafile='$d/residuals.dat'; outfile='$d/residuals.png'" plot_res.gp
done

(Use gnuplot -e "var='value'" to pass variables.)


8. Exercises and Solutions

E07 Task 1 → 6.1, Task 2.1 → 6.2, Task 2.2 → 6.3, Task 3.1 → 6.4, Task 3.2 → 6.5, Task 3.3 → 6.6, Task 4 → 6.7.

Marking schemes

  • Task 1 (8 marks): title + xlabel + ylabel + grid + linestyle + range + PNG export + annotations (label/arrow).
  • Task 2.1 (8 marks): four datasets, four colours, axes ranges, tics + mtics, grid, key, PNG.
  • Task 2.2 (5 marks): set multiplot layout 2,2, four plots, unset multiplot, save.
  • Task 3.1 (3 marks): function plot, title, save.
  • Task 3.2 (8 marks): define f(x), fit ... via a,b,c, plot data + fit, sprintf annotation.
  • Task 3.3 (5 marks): using 1:2:3 with yerrorbars, axes/title.
  • Task 4 (10 marks): Bash loop, basename strip, heredoc, terminal switch, PNG output.

Common mistakes

  • Comma-separated CSV without set datafile separator "," → gnuplot reads no columns.
  • using 1,2 (comma) instead of using 1:2 (colon).
  • Forgot replot after set output.
  • Mixed double/single quotes around the filename inside the heredoc.
  • Wrong column order for yerrorbars (must be x:y:err).

Harder versions. Add log-y, twin axes (set y2tics), histogram (smooth freq), heatmap (set view map; splot ... with image).


9. Written Exam Focus

9.1 Short Answers

Q. Which commands export a plot to PNG? A. set terminal png; set output 'x.png'; replot.

Q. What does using 1:2:3 with yerrorbars mean? A. Columns 1, 2, 3 = x, y, y-error; vertical error bars at each point.

Q. How to plot sin(10x)? A. plot sin(10*x) with lines.

Q. How to fit a parabola to data? A. f(x)=a*x**2+b*x+c; fit f(x) 'd' using 1:2 via a,b,c.

Q. Four sub-plots in a 2×2 grid? A. set multiplot layout 2,2; plot …×4; unset multiplot.

9.2 Medium Answers

Q. (8 marks) Plot linear_data.txt and quadratic_data.txt with different colors, log-y, ranges, saved as PDF.

A.

set terminal pdfcairo size 8cm,6cm
set output 'two_curves.pdf'
set logscale y
set xrange [0:12]; set yrange [1:200]
set xlabel "x"; set ylabel "y (log)"
set grid
set key right bottom
plot 'linear_data.txt'    using 1:2 with linespoints lt 1 lw 2 pt 7 lc rgb "red"  title 'linear', \
     'quadratic_data.txt' using 1:2 with linespoints lt 2 lw 2 pt 5 lc rgb "blue" title 'quadratic'

Q. (5 marks) Curve fit a quadratic to noisy data. A. Same as 6.5.

9.3 Long Answer (12 marks)

Q. Describe a complete CFD post-processing pipeline using awk and gnuplot.

A.

Introduction. CFD post-processing extracts time-series from solver logs into reproducible plots.

Main concept. awk parses, gnuplot plots, Bash glues; every step is text-in/text-out.

Step-by-step.

  1. awk '/^Time =/ {print $3,$7}' run.log > residuals.dat.
  2. gnuplot script.gp reads residuals.dat → residuals.png.
  3. The script applies log-y, labels, grid, styles, then exports.
  4. A Bash loop covers case_*/.

Pipeline: run.log → awk → residuals.dat → gnuplot → residuals.png.

Real HPC link. Monitor 200 simulations overnight — an automated dashboard.

Conclusion. Gnuplot's text-driven model + bash + awk = the canonical HPC plotting recipe.

9.4 Output Prediction

plot 'data.txt' using 1:2 with points → scatter plot, no connecting lines.

fit f(x) 'd' using 1:2 via a,b,c → adjusts a,b,c minimising \(\chi^2\); writes fit.log.

9.5 Comparison

Style Lines Points
lines yes no
points no yes
linespoints yes yes
boxes bar chart no
yerrorbars error bars yes
png pdfcairo eps
Type raster vector vector
For papers OK best LaTeX-friendly
For slides best good rare

9.6 Templates

Plot template:

set terminal pngcairo size 1000,700 enhanced font 'Helvetica,12'
set output 'out.png'
set title 'TITLE'
set xlabel 'x'; set ylabel 'y'
set grid; set key right top box
plot 'file' using 1:2 with linespoints lt 1 lw 2 pt 7 lc rgb 'red' title 'data'

Fit template:

f(x)=a*x**n+b
fit f(x) 'd' using 1:2 via a,b,n
plot 'd' u 1:2 t 'data' w p, f(x) t sprintf('fit %.2f',a) w l

Multiplot template: see 6.3.

9.7 Marking Scheme — "Plot two datasets with custom styles" (8 marks)

  • 1 title. 1 axis labels. 1 grid. 2 linestyles (colour, point). 1 ranges. 1 legend. 1 file export.

10. Very Hard Questions

Beginner

  1. Plot sin(x). → plot sin(x).
  2. Add title. → set title "..."; replot.
  3. Save as png. → terminal+output+replot.
  4. Plot col 1 vs col 3. → using 1:3.
  5. Legend top-right. → set key right top.

Intermediate

  1. Log-y axis. → set logscale y.
  2. Two curves. → plot 'a' u 1:2, 'b' u 1:2.
  3. Red dashed thick. → lt 2 lw 3 lc rgb "red" dt 2.
  4. Error bars. → using 1:2:3 with yerrorbars.
  5. Multiplot 2×2. → 6.3.

Hard

  1. Fit a*exp(-b*x). → f(x)=a*exp(-b*x); fit f(x) 'd' via a,b.
  2. Twin y-axis. → set ytics nomirror; set y2tics; plot 'd' u 1:2 axes x1y1, '' u 1:3 axes x1y2.
  3. Histogram from raw data. → bin(x)=floor(x); plot 'd' using (bin($1)):(1) smooth frequency with boxes.
  4. Conditional colour by sign. → plot 'd' using 1:2:($2>0?1:2) lc variable.
  5. 3-D surface. → splot 'mesh.dat' u 1:2:3 with pm3d.

Very Hard

  1. awk filter + gnuplot in one script. → 6.8.
  2. Animate frames into a GIF. → bash loop producing PNGs, then convert -delay 10 *.png a.gif.
  3. LaTeX-ready labels. → set terminal cairolatex eps.

Deep Integration

  1. Script that plots every case_*/run.log. → §7 + 6.7.
  2. Why gnuplot over Excel for HPC? → reproducible, scriptable, terminal output, runs headless on the cluster.

Coding/Command

  1. x in milliseconds (×1000). → plot 'data.txt' using ($1*1000):2 with lines.
  2. Horizontal reference at y=1e-6. → set arrow from graph 0,first 1e-6 to graph 1,first 1e-6 nohead lt 0.

Debugging

  1. plot file.csv shows nothing. → set datafile separator ",".
  2. PNG not written. → forgot replot after set output (or never closed via set output).

Long Written

  1. (250 words) Gnuplot as an HPC post-processing tool. Use §7.

11. Debugging and Mistake Analysis

Mistake Why wrong Correct Explanation
using 1,2 comma not colon using 1:2 colon separates cols
CSV without separator reads 0 cols set datafile separator "," tell gnuplot
Missing replot after set output empty file output first, then replot order matters
with errorbars for asymmetric wrong style xyerrorbars + 4 cols right style
Forgot unset multiplot next plot lands in sub-cell always close hygiene
' inside '…' heredoc shell parsing breaks escape or use "…" quoting
Non-numeric column values "empty x range" warning clean with awk first data hygiene
set logscale y with zeros log(0) undefined filter zeros or offset math

বাংলায়: gnuplot-ভুলের অর্ধেকই দুটো জিনিসে: column-আলাদা-করা : (কমা নয়!) আর set output-এর পরে replot। আর CSV মানেই আগে set datafile separator "," — নাহলে নীরবে শূন্য কলাম পড়ে। এই তিনটা চেক আগে করো, তারপর অন্য কিছু।


12. Mini Project for Mastery

Goal: Build a reproducible CFD plot generator.

#!/bin/bash
set -euo pipefail
SCRIPT=plot_residuals.gp
cat > $SCRIPT <<'EOF'
set terminal pngcairo size 1200,700 enhanced font 'Helvetica,12'
set output OUTFILE
set title TITLE
set xlabel "iteration"; set ylabel "residual"
set logscale y
set grid; set key right top
plot DATAFILE using 1:2 with linespoints lt 1 lw 2 pt 7 ps 0.5 title 'continuity', \
     DATAFILE using 1:3 with linespoints lt 2 lw 2 pt 5 ps 0.5 title 'momentum-x', \
     DATAFILE using 1:4 with linespoints lt 3 lw 2 pt 9 ps 0.5 title 'momentum-y'
EOF

for d in case_*/; do
    case_name=$(basename "$d")
    awk '/^Iter/ {print NR, $2, $3, $4}' "$d/run.log" > "$d/residuals.dat"
    gnuplot \
        -e "OUTFILE='$d/residuals.png'" \
        -e "TITLE='Residuals — $case_name'" \
        -e "DATAFILE='$d/residuals.dat'" \
        $SCRIPT
done

Connection to exam: Bash loop, gnuplot -e variables, multi-curve plot, log-y, styling, PNG export.


13. Final Chapter Cheat Sheet

Item Memorise
Basic plot 'd' using 1:2 with linespoints title 't'
Function plot sin(10*x) with lines
Title/labels set title; set xlabel; set ylabel
Range set xrange [a:b]; set yrange [c:d]
Log set logscale y
Grid set grid
Key set key right top box
Linestyle set style line 1 lt 1 lw 2 pt 7 ps 1.5 lc rgb "red"
Tics set xtics 1; set mxtics 2
Fit f(x)=...; fit f(x) 'd' via ... (Levenberg–Marquardt minimising RSS)
RSS \(S=\sum_i (y_i - f(x_i))^2\)
Log-log slope power law \(y=ax^b\) → slope = b
Semilog-y line exponential \(y=ae^{bx}\)
Errorbars using 1:2:3 with yerrorbars
Multiplot set multiplot layout R,C … unset multiplot
3D splot 'd' u 1:2:3 with pm3d
PNG set terminal png; set output 'x.png'; replot
PDF set terminal pdfcairo; set output 'x.pdf'; replot
Heredoc gnuplot <<EOF … EOF
-e gnuplot -e "var='val'" script.gp
Trap forget replot after set output
Top phrase "gnuplot is a text-driven plotting pipeline: data + style + terminal."

14. Mock Exam — Four Levels

Level 1 — Basic (definitions & syntax)

Q1. Write the minimal command plotting column 4 against column 2 of res.dat with lines.

Solution: plot 'res.dat' using 2:4 with lines

Q2. Which three commands turn the current plot into the file out.png?

Solution: set terminal png ; set output 'out.png' ; replot.

Q3. Command for a logarithmic y-axis?

Solution: set logscale y.

Q4. What is fit.log?

Solution: The file gnuplot writes during fit: iterations, final parameters, asymptotic standard errors, reduced \(\chi^2\).

Q5. How do you start and end a 1×3 multiplot?

Solution: set multiplot layout 1,3 … three plots … unset multiplot.

Level 2 — Intuitive (predict / explain why)

Q1. Your residuals span \(10^{-1}\) to \(10^{-9}\). Why is a linear y-axis useless and what do you use?

Solution: On a linear axis everything below ~\(10^{-2}\) collapses onto the x-axis — 8 decades are invisible. set logscale y gives each decade equal space; convergence becomes a readable straight-ish slope.

Q2. plot 'd.csv' using 1:2 draws nothing; the file looks fine in an editor. First suspect?

Solution: Comma separators — gnuplot defaults to whitespace. set datafile separator "," fixes it.

Q3. Data follows \(y = 5x^{2}\). What do you see on log-log axes, and what is the slope?

Solution: A straight line with slope 2 (and intercept log 5): \(\log y = \log 5 + 2\log x\) — power laws are lines on log-log.

Q4. A colleague's fit returns immediately with absurd parameters. The function is f(x)=a*exp(b*x) and data values reach \(10^8\). Why, and the standard fix?

Solution: Bad initial guesses (a=b=1 default) put the exponential astronomically far from data — the optimiser lands in a useless local minimum or overflows. Fix: set sensible starts before fitting (a=1e-2; b=2) or fit the LINEARISED model (log y vs x) first to seed a, b.

Q5. Why does plot 'd' u 1:2, '' u 1:3 work — what does '' mean?

Solution: The empty filename reuses the previous datafile — an idiom for plotting several columns of the same file.

Level 3 — Hard (exam level)

Q1. (8 marks) Write the complete gnuplot script: res.dat columns iter, ρ-residual, p-residual → two curves, log-y, grid, legend top-right, title "Convergence", PNG 1200×700.

Solution:

set terminal pngcairo size 1200,700 enhanced
set output 'convergence.png'
set title "Convergence"
set xlabel "iteration"; set ylabel "residual"
set logscale y
set grid
set key right top box
plot 'res.dat' using 1:2 with linespoints lt 1 lw 2 pt 7 title 'rho', \
     'res.dat' using 1:3 with linespoints lt 2 lw 2 pt 5 title 'p'
বাংলা ইঙ্গিত: checklist-মাফিক লেখো — terminal, output, title, labels, log, grid, key, দুই curve — প্রতিটা আইটেম এক নম্বর।

Q2. (8 marks) Grid-convergence: errors \(e(h)\) measured at \(h = 0.1, 0.05\) give \(e = 4\times10^{-3}, 1\times10^{-3}\). Compute the observed order of accuracy \(p\) from the log-log slope.

Solution: \(p = \dfrac{\log(e_1/e_2)}{\log(h_1/h_2)} = \dfrac{\log(4\times10^{-3}/1\times10^{-3})}{\log(0.1/0.05)} = \dfrac{\log 4}{\log 2} = 2\) — second-order accurate. বাংলা ইঙ্গিত: দুই বিন্দুর log-log slope-ই order — সূত্রটা \(\log(e_1/e_2)/\log(h_1/h_2)\); CFD-পরীক্ষার অতি প্রিয় হিসাব।

Q3. (8 marks) Fit an exponential decay \(f(t) = A e^{-t/\tau}\) to decay.dat and print τ with its error in the plot legend. Script?

Solution:

A=1; tau=1
f(x) = A*exp(-x/tau)
fit f(x) 'decay.dat' using 1:2 via A,tau
plot 'decay.dat' u 1:2 t 'data' w p, \
     f(x) t sprintf("fit: tau = %.3g +- %.2g", tau, tau_err) w l
gnuplot stores each parameter's asymptotic standard error as <name>_err after the fit. বাংলা ইঙ্গিত: tau_err automatic variable-টা জানা = "harder" প্রশ্ন এক লাইনে শেষ; না জানলে fit.log থেকে হাতে লিখতে হত।

Q4. (10 marks) Make a 2×1 multiplot: top = residual vs iteration (log-y), bottom = lift coefficient vs iteration (linear), sharing the same x-range 0–5000, exported as PDF.

Solution:

set terminal pdfcairo size 16cm,12cm
set output 'dashboard.pdf'
set multiplot layout 2,1
set xrange [0:5000]
set logscale y
set ylabel "residual"
plot 'res.dat' u 1:2 w l lt 1 t 'rho'
unset logscale y
set ylabel "C_L"
set xlabel "iteration"
plot 'forces.dat' u 1:2 w l lt 2 t 'lift'
unset multiplot
Key detail: unset logscale y before the second panel — multiplot state persists between cells. বাংলা ইঙ্গিত: multiplot-এ আগের cell-এর সেটিং পরের cell-এ লেগে থাকে — log উঠাতে ভুললে নিচের প্যানেল ভুতুড়ে দেখাবে; এটাই প্রশ্নের লুকানো দাঁত।

Q5. (10 marks) Explain mathematically what fit minimises and why "reduced \(\chi^2 \approx 1\)" indicates a good fit (when errors are known).

Solution: fit minimises the (weighted) residual sum of squares \(S = \sum_i \left(\frac{y_i - f(x_i;\mathbf{a})}{\sigma_i}\right)^2\) over parameters \(\mathbf{a}\) via Levenberg–Marquardt. Reduced \(\chi^2 = S/(N-m)\) (N points, m parameters). If the model is right and \(\sigma_i\) are the true errors, each term averages ~1 ⇒ reduced \(\chi^2 \approx 1\). \(\gg 1\): model too poor (or errors underestimated); \(\ll 1\): overfitting or overestimated errors. বাংলা ইঙ্গিত: তিনটা মামলা মুখস্থ: \(\approx 1\) ভালো, \(\gg 1\) মডেল/\(\sigma\) ভুল, \(\ll 1\) overfit — ব্যাখ্যাসহ লিখলে পূর্ণ নম্বর।

Level 4 — Beyond the lecture (transfer + coding)

Q1. Write a single bash+gnuplot tool quickplot.sh FILE XCOL YCOL that plots any column pair of any whitespace file to FILE.png, with axis labels taken from the file's header line (line 1 starts with #).

Solution:

#!/bin/bash
set -euo pipefail
f=$1; xc=$2; yc=$3
xlab=$(awk -v c="$xc" 'NR==1 && /^#/ {print $(c+1); exit}' "$f")
ylab=$(awk -v c="$yc" 'NR==1 && /^#/ {print $(c+1); exit}' "$f")
gnuplot <<EOF
set terminal pngcairo size 1000,600
set output '${f%.dat}.png'
set xlabel "${xlab:-col$xc}"; set ylabel "${ylab:-col$yc}"
set grid
plot '$f' using $xc:$yc with linespoints title '${ylab:-data}'
EOF
echo "wrote ${f%.dat}.png"
The $(c+1) skips the leading # field; ${var:-fallback} guards headerless files. বাংলা ইঙ্গিত: generic tool-প্রশ্নে নম্বর থাকে edge-case-এ — header নেই? fallback আছে; এই দু-একটা guard-ই উত্তরকে "production" করে।

Q2. Your sweep produced scaling.dat (ranks, time). Plot measured speed-up AND the ideal line AND the Amdahl curve for f=0.05 in one figure (Ch 14 transfer). Give the script.

Solution:

set terminal pngcairo size 1000,700
set output 'scaling.png'
set xlabel "ranks N"; set ylabel "speed-up S"
set key left top
t1 = system("awk '$1==1{print $2}' scaling.dat") + 0
amdahl(N) = 1/(0.05 + 0.95/N)
plot 'scaling.dat' using 1:(t1/$2) with linespoints pt 7 title 'measured', \
     x title 'ideal' with lines dt 2, \
     amdahl(x) title 'Amdahl f=0.05' with lines lt 3
Tricks: system() pulls \(T_1\) out of the data; (t1/$2) computes speed-up on the fly; plotting x draws the ideal diagonal. বাংলা ইঙ্গিত: তিন স্তরের তুলনা-ছবিই scaling-study-র সমাপ্তি; using 1:(expr) — কলামের উপর অঙ্ক — gnuplot-এর গুপ্ত অস্ত্র।

Q3. A 4 GB log on the cluster: you need ONLY the plot on your laptop. Two designs: (a) rsync the log home and plot locally, (b) awk+gnuplot remotely, rsync the PNG. Compare quantitatively (100 Mbit/s link) and give the remote one-liner.

Solution: (a) transfers 4 GB ≈ \(4\times10^9 \times 8 / 10^8 = 320\) s minimum; (b) transfers ~100 KB PNG ≈ instant, plus seconds of remote awk/gnuplot. One-liner:

ssh hpc 'awk "/^Time/{print \$3,\$7}" run.log > r.dat && gnuplot -e "set term png; set output \"r.png\"; set logscale y; plot \"r.dat\" w l"' && rsync -azP hpc:r.png .
বাংলা ইঙ্গিত: "compute where the data lives" — হিসাবটা দেখাও (320 s বনাম ~5 s), নীতিটা বলো; দুটোতেই নম্বর।

Q4. Your professor wants the SAME figure regenerated for the paper after every new run (CI thinking). Describe the reproducibility setup: what is versioned, what is generated, and the one make rule that ties it together (Ch 12 transfer).

Solution: Versioned in git: the gnuplot script, the awk extractor, the Makefile — never the PNG (generated artefact, in .gitignore). Generated: residuals.dat, residuals.png. Make rule:

residuals.png: plot_res.gp residuals.dat
    gnuplot plot_res.gp
residuals.dat: run.log extract.awk
    awk -f extract.awk run.log > $@
make residuals.png rebuilds exactly what's stale — figure regeneration becomes a dependency graph, not a manual chore. বাংলা ইঙ্গিত: "code versioned, artefacts generated" — এই বিভাজন + timestamp-ভিত্তিক rebuild = পুরো কোর্সের দর্শন এক প্রশ্নে।


End of Chapter 9.