Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active February 26, 2025 18:57
Show Gist options
  • Select an option

  • Save drmalex07/306da0091d5e4a5c8e39f8c7bf644061 to your computer and use it in GitHub Desktop.

Select an option

Save drmalex07/306da0091d5e4a5c8e39f8c7bf644061 to your computer and use it in GitHub Desktop.
Some gnuplot examples. #gnuplot

README - gnuplot examples

Non-interactive use

Pass command-line args

To run a gnuplot script non-interactively (possibly passing positional arguments from the command line) use the -c flag of gnuplot. For example:

gnuplot -c plot-something.gnuplot "Measurements on A" measurements.csv

Inside plot-something.gnuplot script, we can refer to command-line arguments as:

TITLE = ARG1
DATAFILE = ARG2
...

Plot data from STDIN

As in previous example, but now datafile has the special name <cat. Note that not reading from a regular file, has the limitation that we cannot have multiple plots (as data is read once from pipe).

For example:

cat measurements.csv | gnuplot -c plot-something.gnuplot "Measurements on A"

Inside plot-something.gnuplot we use something like:

...
TITLE = ARG1
plot '<cat' using ($1):($2) with lines title TITLE

Basic plots

Assume the following defaults:

DATAFILE="1.csv"
set grid
set datafile separator ";"
set terminal png font "sans-serif,10"
set output "1.png"

Plot a simple line:

plot DATAFILE using 1:2 with lines title "Something"

Plot a line with filled area:

set style fill transparent solid 0.33
plot DATAFILE using 1:2 with filledcurves x1 title "Another thing"

To plot time series, we must give read format (to read the input timestamp) and a write format for labels placed along x tics:

# the X axis contains time data
set xdata time

# the format to read x datapoints from input
# see http://gnuplot.sourceforge.net/docs_4.2/node274.html
set timefmt "%s"

# the format to write datapoints at x axis
# see http://gnuplot.sourceforge.net/docs_4.2/node185.html
set xdata time
set format x "%b %d\n%H:%M"

# place a X tick every 2h
set xtic 7200

plot DATAFILE using 1:2 with lines title "Series #1", using 1:3 with lines title "Series #2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment