This page provides a summary and excerpts from the following resources:
http://personalpages.to.infn.it/~mignone/Numerical_Algorithms/gnuplot.pdf
https://weinman.cs.grinnell.edu/courses/CSC213/2008F/labs/10-pingpong-regression.pdf
Plotting Sine Function:
gnuplot> plot sin(x)
Re-plotting on Top of Existing Plot:
gnuplot> replot (cos(x))**2 # ** means exponential (not ^)
Other Basic Commands (# indicates comments):
gnuplot> set key autotitle columnhead # Automatically use the first line (column headers) of the data file as titles
gnuplot> set xrange [0:2*pi] # Limit the x-axis range from 0 to 2*pi
gnuplot> set ylabel "f(x)" # Set the label on the y-axis (same as “set xlabel”)
gnuplot> set title "My Plot" # Set the plot title
gnuplot> set log y # Set logarithmic scale on the y-axis (same as “set log x”)
gnuplot> unset log y # Disable log scale on the y-axis
gnuplot> set key bottom left # Position the legend in the bottom left part of the plot
gnuplot> set xlabel font ",18" # Change font size for the x-axis label (same as “set ylabel”)
gnuplot> set tic font ",18" # Change the major (labelled) tics font size on all axes
gnuplot> set samples 2500 # Set the number of points used to draw a function
Plotting Data from a File:
gnuplot> plot "file.dat" using 1:2 with lines smooth bezier # Use column 1 for x and column 2 for y with a smooth bezier line
If a data file has more than one dataset, they must be separated by a pair of empty lines (two empty lines). The first set is the 0th dataset (0 index), the second set is the 1st dataset (1 index), etc. You can plot them like this:
gnuplot> plot "file.dat" using 1:2 index 0
gnuplot> replot "file.dat" using 1:2 index 1
To keep all commands in a file and load it, you can use:
gnuplot> load "filename.gp"
Example of a .gp File:
reset # Force all graph-related options to default values
fname = "myfile.dat" # File name
set key autotitle columnhead
set autoscale xfixmin # Axis range automatically scaled to include the range
set autoscale xfixmax # of data to be plotted
set tics font ",18"
set xlabel "x" font ",18"
set ylabel "y" font ",18"
set lmargin at screen 0.1 # Set size of left margin
set rmargin at screen 0.82 # Set size of right margin
set bmargin at screen 0.12 # Set size of bottom margin
set tmargin at screen 0.95 # Set size of top margin
plot fname using 1:3
Regression
# Define a linear function for regression
gnuplot> f(x) = m*x + b
# Perform a linear fit using data from 'filename', with the first column as x and the second column as y, solving for m and b
gnuplot> fit f(x) 'filename' using 1:2 via m, b
# Plot the fitted line with the title 'Line Fit'
gnuplot> plot f(x) title 'Line Fit'
# Plot the data points from 'filename' (using the first column as x and the second column as y) with the title 'Carrier Pigeon Delivery'
# and overlay the fitted model line with the title 'Model Fit'
gnuplot> plot 'filename' using 1:2 title 'Carrier Pigeon Delivery' with points, f(x) title 'Model Fit'
Saving a image file
gnuplot> set terminal png
gnuplot> set output ’file.png'
gnuplot> set terminal png
gnuplot> plot sin(x)
Reply