This is an old revision of the document!


GnuPlot

Enter gnuplot environment with 'gnuplot' command. In the interactive shell you can use the following commands.

load

To load a command file, which contains other commands for the gnuplot interactive shell. Lines starting with '#' are ignored.

load "plot_data.gp"

You can also directly load it when starting gnuplot, in shell:

gnuplot -persist plot_data.gp

If the file ends with “pause -1”, you can omit the “-persist” argument.

set

  • Axes tics.
    set ytics nomirror
    set y2tics -3.5,0.5,3.5
    set ytics ("0.5" 0.5, "1.2" 1.2, "2.3" 2.3)
    set y2tics auto
  • Graph title.
    set title ""
  • Legend position.
    # available: left, right, top, bottom, outside, and below
    set key left bottom
    set key 100,100
  • New plot window.
    plot <...>
    set term wxt 1
    plot <...>
    set term wxt 2
    plot <...>
  • Data separator (space by default).
    set datafile separator ","
  • Clear window.
    clear

plot

Plot functions

FIXME

Plot data from a file

  • plot With only one or two columns in the file.
    plot "data.dat"
  • using/u Specifying columns to use. If only one is specified, entry number (line number) is used for x.
    plot "data.dat" using 4
    plot "data.dat" using 4:5
  • every Specifying lines to use (every 2 lines from line 50 to line 1050).
    plot "data.dat" using 4:5 every 2::50::1050
  • , Several curves in the same plot.
    plot "data1.dat" using 4:5, "data2.dat" using 6:7
    plot \
      "data1.dat" using 4:5, \
      "data2.dat" using 6:7
  • title/t Functions titles.
    plot "data1.dat" using 4:5 title "my curve"
  • rgb Colors.
    show palette colornames
    plot "data1.dat" using 4:5 lt rgb "red"
    plot "data1.dat" using 4:5 lt rgb "#FF0000"
  • axes Axes : x1y1 (bottom-left), x2y2 (top-right), x1y2, x2y1.
    plot "data1.dat" using 4:5 axes x1y1, "data2.dat" using 6:7 axes x1y2

    If you want to display different tics on y2 axis, do before the plot command:

    set ytics nomirror
    set y2tics -3.5,0.5,3.5
    # or
    set y2tics auto
  • with Line type.
    # line
    plot "data1.dat" using 4:5 with lines
    # line with custom style
    plot "data1.dat" using 4:5 with lines linestyle 1
    # line with custom line width
    plot "data1.dat" using 4:5 with lines lw 4
    # points
    plot "data1.dat" using 4:5 with points
    # points of custom type
    plot "data1.dat" using 4:5 with points pt 19
    # lines AND points
    plot "data1.dat" using 4:5 with linespoints pt 1
    # labels in addition to points
    plot "data1.dat" using 4:5:1 with labels offset 0.7,0.7
     
    # to find out point types and line width available
    test
  • ($1) Arithmetics on columns.
    plot "data1.dat" using 4:($5/2+14)
    # where $n means column n
  • fun()= Define variables and functions, use predefined functions (help functions).
    theta=7.72 * (2*pi/360)
    dx=6.7866
    dy=10.1704
    rotation_x(x,y)=(x-dx)*cos(theta)-(y-dy)*sin(theta)
    rotation_y(x,y)=(x-dx)*sin(theta)+(y-dy)*cos(theta)
     
    plot "gps.dat" using (rotation_x($3,$4)):(rotation_y($3,$4))
  • (?:)Tests.
    max(x,y)=(x>y?y:x)
  • for Plot iteration.
    plot for [file in "run1.dat run2.dat run3.dat"] file using 1:2
    file(n) = sprintf("run_%d.dat",n)
    plot for [i=1:3] file(i) using 1:2
  • awk Assign variables in functions and make operations with different lines. Not possible with gnuplot, but awk can help.
    • Using cumulated values of a column.
      plot "< awk '{sum=sum+$2; print sum,$6}' idSlamDala.dat" using 1:2
    • Using difference of two consecutive lines of the same column.
      plot "< awk 'NR==1 { t=$2 } NR>1 { dt=t-$2; t=$2; print $1,dt; }' idSlamDala.dat" using 1:2
  • paste Make operations with different files. Not possible with gnuplot, but paste can help.
    • Plotting one curve with values from different files.
      plot "< paste file1.dat file2.dat" using 1:($4-$2)
  • gunzip Use file compressed with gzip. Not possible with gnuplot, but gunzip can help.
    plot "< gunzip -c file.dat" using 1:($4-$2)
  • or anything else with shell commands…
  • sh Passing arguments to a gnuplot script.
    #!/bin/sh
    file=$1
    gnuplot <<\EOF
    plot "$file" using 1:(\$2*3)
    EOF

    Or with environment variables set before executing the script:

    plot "`echo $GNUPLOT_PARAM1`" using 1:($2*3)

File outputs

In general be sure that you don't have another “set term” command in your plotting code.

  • EPS and PDF:
    set terminal postscript eps color "Times-Roman" 16
    set output 'plot.eps'
     
    # your plotting code (can be included using the load command)
     
    set output
    !epstopdf --outfile=plot.pdf plot.eps
    quit

    By default, when exporting to ps or pdf, gnuplot changes the lines style with dashes so that they can be recognized when printed in black and white. If you want to keep your lines plain, you can add the “linestyle 1” command:

    plot [...] with lines linestyle 1 lt rgb "red"
  • PNG:
    set terminal png size 450,360 small
    set output 'plot.png'
     
    # your plotting code (can be included using the load command)
     
    quit
  • SVG:
    set terminal svg rounded size 450,360
    set output 'plot.svg'
     
    # your plotting code (can be included using the load command)
     
    set output
    !gzip -S z plot.svg
    quit
software/gnuplot.1297105492.txt.gz · Last modified: 2013/09/19 16:43 (external edit)
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0