Introduction to Gnuplot Plotting Functions and Data ██ Gnuplot 2 / 31 ██ What is Gnuplot? Gnuplot is a command-driven plotting program used throughout science and engineering. • Runs interactively or from scripts • Plots mathematical functions and data files • Produces publication-quality output (PNG, PDF, SVG, and more) • Available on Linux, macOS, and Windows Unlike GUI tools (Excel, Origin), gnuplot is driven by commands — which means you can automate and reproduce your plots exactly.
3 / 31 ██ Starting Gnuplot At the terminal: $ gnuplot
You'll see the gnuplot prompt: G N U P L O T
...
gnuplot>
Type commands at the gnuplot> prompt. Type quit or press Ctrl-D to exit.
4 / 31 ██ Plotting Functions 5 / 31 ██ The plot Command The simplest gnuplot command: gnuplot> plot sin(x)
This opens a window with a plot of sin(x) over the default x range. More examples: gnuplot> plot cos(x)
gnuplot> plot x**2
gnuplot> plot exp(-x**2)
6 / 31 ██ Built-in Math Functions Gnuplot knows the standard mathematical functions: • sin(x), cos(x), tan(x) — trigonometric (angles in radians)
• exp(x) — exponential (e^x)
• log(x), log10(x) — natural and base-10 logarithm
• sqrt(x) — square root
• abs(x) — absolute value
• x**n — exponentiation (x to the nth power)
• pi — the constant π ≈ 3.14159...
Plot two functions at once by separating them with a comma: gnuplot> plot sin(x), cos(x)
7 / 31 ██ Setting the Range Control which x values are plotted by adding [min:max] right after plot:
gnuplot> plot [0:2*pi] sin(x)
gnuplot> plot [-5:5] x**2
gnuplot> plot [0:10] exp(-x)
Works with multiple functions too: gnuplot> plot [0:2*pi] sin(x), cos(x), sin(2*x)
8 / 31 ██ Demo - Plotting Functions 9 / 31 ██ Configuring Your Plot 10 / 31 ██ set Commands Gnuplot settings are controlled with set:
gnuplot> set title "My Plot"
gnuplot> set xlabel "Time (s)"
gnuplot> set ylabel "Position (m)"
After changing settings, run replot to refresh the plot:
gnuplot> replot
Or just issue a new plot command.
11 / 31 ██ Title and Axis Labels gnuplot> set title "Damped Oscillation"
gnuplot> set xlabel "Time (s)"
gnuplot> set ylabel "Amplitude"
gnuplot> plot [0:10] exp(-x/3) * cos(2*pi*x)
Give each curve a name in the legend using title in the plot command:
gnuplot> plot sin(x) title "sine wave", cos(x) title "cosine wave"
To suppress the legend entry for a curve: gnuplot> plot sin(x) notitle
12 / 31 ██ Axis Ranges Set axis ranges persistently with set xrange and set yrange:
gnuplot> set xrange [0:10]
gnuplot> set yrange [-1.5:1.5]
gnuplot> replot
Reset back to auto-scaling: gnuplot> set xrange [*:*]
gnuplot> set yrange [*:*]
Or remove the setting entirely: gnuplot> unset xrange
gnuplot> unset yrange
13 / 31 ██ The Legend (key) By default, gnuplot shows a legend (called the "key") in the upper right. Move it: gnuplot> set key top left
gnuplot> set key bottom right
gnuplot> set key outside right
Hide it entirely: gnuplot> unset key
14 / 31 ██ Demo - Settings 15 / 31 ██ Plotting Data 16 / 31 ██ Data File Format Gnuplot reads plain text data files: # time(s) position(m) velocity(m/s)
0.0 0.000 1.000
0.5 0.479 0.878
1.0 0.841 0.540
1.5 0.997 0.071
2.0 0.909 -0.416
• Lines starting with # are comments (ignored)
• Columns separated by whitespace (spaces or tabs) • One data point per line 17 / 31 ██ Plotting a Data File gnuplot> plot "data.txt"
By default, this plots column 2 vs. column 1. Choose the plot style with with:
gnuplot> plot "data.txt" with lines
gnuplot> plot "data.txt" with points
gnuplot> plot "data.txt" with linespoints
Shorthand (w for with, l/p/lp for style):
gnuplot> plot "data.txt" w lp
18 / 31 ██ The using Option Select which columns to plot with using xcol:ycol:
gnuplot> plot "data.txt" using 1:2 # time vs position
gnuplot> plot "data.txt" using 1:3 # time vs velocity
Short form is u:
gnuplot> plot "data.txt" u 1:2 w lp title "position"
gnuplot> plot "data.txt" u 1:3 w l title "velocity"
19 / 31 ██ Column Arithmetic using can do math on columns — wrap expressions in parentheses and use $N for column N:
# Column 2 is in meters, convert to centimeters
gnuplot> plot "data.txt" using 1:($2 * 100)
# Column 2 is temperature in Celsius, convert to Fahrenheit
gnuplot> plot "data.txt" using 1:($2 * 9/5 + 32)
# Compute kinetic energy: mass in col 2, velocity in col 3
gnuplot> plot "data.txt" using 1:(0.5 * $2 * $3**2)
20 / 31 ██ Plotting Multiple Datasets Plot multiple columns from the same file by separating plots with a comma: gnuplot> plot "data.txt" u 1:2 title "position", \
"data.txt" u 1:3 title "velocity"
The \ continues the command on the next line.
Or from different files: gnuplot> plot "run1.txt" u 1:2 title "run 1", \
"run2.txt" u 1:2 title "run 2"
21 / 31 ██ Demo - Plotting Data 22 / 31 ██ Saving Your Plots 23 / 31 ██ Output to a File By default, gnuplot plots to the screen. To save to a file: gnuplot> set terminal pngcairo
gnuplot> set output "plot.png"
gnuplot> plot "data.txt" u 1:2 title "position"
gnuplot> set output # close/flush the output file
Other terminals: gnuplot> set terminal pdf
gnuplot> set terminal svg
gnuplot> set terminal pdfcairo # higher quality PDF
To go back to plotting on screen: gnuplot> set terminal qt
24 / 31 ██ Gnuplot Scripts Save your commands in a .gp file instead of typing interactively:
# plot_position.gp
set title "Position vs Time"
set xlabel "Time (s)"
set ylabel "Position (m)"
set terminal pngcairo
set output "position.png"
plot "data.txt" using 1:2 with linespoints title "position"
set output
Run it from the terminal: $ gnuplot plot_position.gp
This is how you make reproducible plots. The script is the record of exactly what you did — just like a shell script for commands. 25 / 31 ██ Practice Replicate these plots Note: This plot was created using the data in Ocular_Transmission_Human.csv (in the sandbox). Column 1 is wavelength, column 6 is total 26 / 31
transmission. ██ Summary 27 / 31 ██ What We Covered • plot f(x) — plot a mathematical function
• plot [min:max] f(x) — set the x range inline
• plot f(x), g(x) — plot multiple functions at once
• set title, set xlabel, set ylabel — label your plot
• set xrange, set yrange — control axis ranges
• set key / unset key — move or hide the legend
• title "name" / notitle — label individual curves
• plot "file.txt" — plot data from a file
• with lines / with points / with linespoints — plot styles
• using 1:2 — select which columns to plot
• using 1:($2 * factor) — arithmetic on columns
• set terminal, set output — save plots to files
• Gnuplot scripts — reproducible, automated plots 28 / 31 ██ Next Time In the next module, we'll cover function fitting:
• Defining a model function with adjustable parameters • Fitting a model to data with fit
• Interpreting fit results and uncertainties • Plotting the fit alongside the data 29 / 31 ██ Last Slide This space intentionally left blank 30 / 31 31 / 31