kesto.de Cheatsheets

Gnuplot

Plot a specific x-range (and y-range)

plot [-5:5] sin(x),cos(x)
plot [-5:5] [-2:2] sin(x),cos(x)

Replot last plot

replot

Set labels of axis

set xlabel "x"
set ylabel "y"

Specify line width when plotting

plot [-5:5] sin(x) lw 2,cos(x) lw 10

Specify title for key

plot sin(x) title "Sine function"

Output as PNG

set terminal png
set output "example.png"

List all available output methods (latex, ...):

set terminal

Enable 0-axis

set zeroaxis

Enable background grid

set grid

Specify resolution of background grid

set xtics 3.14159
set ytics 0.5

Using variables and functions

v = 2
m = 0
f(x) = 1/(v*sqrt(2*pi))*exp(-0.5*((x-m)/v)**2)
plot [-9:9] [-0.1:0.5] f(x)

Using the ternary operator

plot [-5:5] [-3:3] (x > 3.14159/2 ? sin(x) : cos(x))

Plot x-y-pairs form CSV file connected with lines

plot "example.csv" using 1:2 with lines

Use formatted date as X-coordinate for CSV input

set xdata time
set timefmt "%d.%m.%Y" # date format used in input file
set xtics rotate by -45
set format x "%Y-%m-%d" # date format for output
plot [] [-20:40] "data.csv" using 1:2 title "Column 2" with lines, "data.csv" using 1:3 title "Column 3" with lines

Scatterplot from column 2 and 3

plot [-15:35] [-15:35] "data.csv" using 2:3

Calculating with values from CSV files (difference)

plot [-15:35] [-15:35] "data.csv" using 3:($3-$4)

Multiple plots (2 rows, 1 column):

set multiplot layout 2,1
plot sin(x)
plot x**2

Recursive function definitions

set xrange [0:30]
set sample 30
q(x) = (x <= 2) ? 1 : q(x-q(x-1))+q(x-q(x-2))
plot q(x)

Polynomial interpolation using fit

p(x) = a0+a1*x+a2*x**2
fit p(x) "data.csv" via a0, a1, a2
plot "data.csv" using 1:2 title "measurements", p(x) title "interpolation"