gnuplot: Tips and tricks

Presented in UCDavis Physics and Astronomy code review on March 17, 2023.

plotting functions

gnuplot> f(x) = x*sin(x)
gnuplot> plot f(x)


gnuplot> a=2
gnuplot> f(x) = a*x*sin(x)
gnuplot> plot f(x)


gnuplot> plot sin(x) title 'Sine Function', log(x) title 'Log function'
gnuplot> f(x) = x*sin(x)
gnuplot> plot f(x)


gnuplot> a=2
gnuplot> f(x) = a*x*sin(x)
gnuplot> plot f(x)


gnuplot> plot sin(x) title 'Sine Function', log(x) title 'Log function'

Data

Loading csv files
Need to set datafile separator

gnuplot> set datafile separator ","
gnuplot> set datafile separator ","

Load and plot boston planes dataset

gnuplot> set datafile separator ","     
gnuplot> plot 'Planes/boston_planes_48h.csv' using 2:3 with points pointsize 0.1 title "Latitude, Longitude of planes"
gnuplot> set datafile separator ","     
gnuplot> plot 'Planes/boston_planes_48h.csv' using 2:3 with points pointsize 0.1 title "Latitude, Longitude of planes"

Instead of indexing the columns with 2 and 3, can also use column names instead

gnuplot> plot 'Planes/boston_planes_48h.csv' using 2:3 with points pointsize 0.1 title "Latitude, Longitude of planes", 'Planes/boston_planes_6h.csv' using "latitude":"longitude" with points pointsize 0.1 linecolor 6
gnuplot> plot 'Planes/boston_planes_48h.csv' using 2:3 with points pointsize 0.1 title "Latitude, Longitude of planes", 'Planes/boston_planes_6h.csv' using "latitude":"longitude" with points pointsize 0.1 linecolor 6

Load multiple datasets

gnuplot> set datafile separator ","     

gnuplot> plot 'Planes/boston_planes_48h.csv' using 2:3 with points pointsize 0.1 linecolor -1 title "Latitude, Longitude of planes", 'Planes/boston_planes_6h.csv' using 2:3 with points pointsize 0.1 linecolor 6 
gnuplot> set datafile separator ","     

gnuplot> plot 'Planes/boston_planes_48h.csv' using 2:3 with points pointsize 0.1 linecolor -1 title "Latitude, Longitude of planes", 'Planes/boston_planes_6h.csv' using 2:3 with points pointsize 0.1 linecolor 6 

Curve fitting

f1(x) = a1*tanh(x/b1) # define the function to be fit
a1 = 300; b1 = 0.005; # initial guess for a1 and b1
fit f1(x) 'force.dat' using 1:2 via a1, b1

Animations

Sine wave

gnuplot> set terminal gif animate delay 1
gnuplot> f(x,t) = sin(x-t)
gnuplot> set output "test.gif"
gnuplot> do for [i=1:96] {plot f(x,i*0.05)}
gnuplot> set terminal gif animate delay 1
gnuplot> f(x,t) = sin(x-t)
gnuplot> set output "test.gif"
gnuplot> do for [i=1:96] {plot f(x,i*0.05)}

Boston airplane 360 rotation

gnuplot> set terminal gif animate delay 1

Terminal type is now 'gif'
Options are 'nocrop enhanced animate delay 1 loop 0 nooptimize size 640,480 font "arial,12.0" '
gnuplot> set output "boston3D.gif"
gnuplot> do for [i=1:360]{
more> splot 'Planes/boston_planes_48h.csv' using 2:3:4 with points ps 0.02 lc -1
more> set view 60, i, 1, 1
more> }
gnuplot> set terminal gif animate delay 1

Terminal type is now 'gif'
Options are 'nocrop enhanced animate delay 1 loop 0 nooptimize size 640,480 font "arial,12.0" '
gnuplot> set output "boston3D.gif"
gnuplot> do for [i=1:360]{
more> splot 'Planes/boston_planes_48h.csv' using 2:3:4 with points ps 0.02 lc -1
more> set view 60, i, 1, 1
more> }

Running it as scripts

Save the required commands to a text file and run it with

gnuplot "filename"

If you want to see the graphic window, then

gnuplot -p "filename"

pass in variables to the script

Say there is a script plot.gnuplot which plots the sine function

plot sin(x) linetype LT
plot sin(x) linetype LT

Sometimes, I want to plot the same function but try it with different linetypes. In that case, you can pass the variable LT as a command line argument:

gnuplot -p -e "LT=3" plot.gnuplot

This sets the variable LT = 3 and runs the rest of the code.