Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
software:gnuplot [2013/07/09 18:49] cyril [File outputs] |
software:gnuplot [2024/04/19 09:54] (current) cyril autotitle |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== GnuPlot ====== | ||
| + | |||
| + | Enter gnuplot environment with ' | ||
| + | |||
| + | ===== load ===== | ||
| + | |||
| + | To load a command file, which contains other commands for the gnuplot interactive shell. Lines starting with '#' | ||
| + | |||
| + | <code gnuplot> | ||
| + | load " | ||
| + | </ | ||
| + | |||
| + | You can also directly load it when starting gnuplot, in shell: | ||
| + | <code gnuplot> | ||
| + | gnuplot -persist plot_data.gp | ||
| + | </ | ||
| + | If the file ends with "pause -1", you can omit the " | ||
| + | |||
| + | ===== set ===== | ||
| + | * Window size.< | ||
| + | set term wxt size 1024,768 | ||
| + | set term x11 size 1024,768 | ||
| + | </ | ||
| + | |||
| + | * Axes ranges.< | ||
| + | set xrange [0:1000] | ||
| + | set yrange [-100:100] | ||
| + | set x2range [0:100] | ||
| + | set y2range [-10:10] | ||
| + | set autoscale | ||
| + | </ | ||
| + | |||
| + | * Axes tics.< | ||
| + | set ytics nomirror | ||
| + | set y2tics auto | ||
| + | set ytics 0.5 | ||
| + | set ytics -3.5, | ||
| + | set xtics (" | ||
| + | </ | ||
| + | |||
| + | * Axes tics labels.< | ||
| + | set format x " | ||
| + | set format y " | ||
| + | </ | ||
| + | set format x "" | ||
| + | </ | ||
| + | set ydata time | ||
| + | set timefmt " | ||
| + | set format y " | ||
| + | </ | ||
| + | |||
| + | * Grid.< | ||
| + | set grid | ||
| + | </ | ||
| + | |||
| + | * Axes labels.< | ||
| + | set xlabel "" | ||
| + | set ylabel "" | ||
| + | set x2label "" | ||
| + | set y2label "" | ||
| + | </ | ||
| + | |||
| + | * Log scale.< | ||
| + | set logscale x 2 | ||
| + | set logscale y 10 | ||
| + | </ | ||
| + | |||
| + | * Graph title.< | ||
| + | set title "" | ||
| + | </ | ||
| + | |||
| + | * Legend position.< | ||
| + | # available: left, right, top, bottom, outside, and below | ||
| + | set key left bottom | ||
| + | set key 100,100 | ||
| + | </ | ||
| + | |||
| + | * Automatic legend.< | ||
| + | set key autotitle columnheader | ||
| + | </ | ||
| + | |||
| + | * Line options.< | ||
| + | set pointsize 0.33333333 | ||
| + | </ | ||
| + | |||
| + | * Size ratio (-1 to have same scale on x and y axis).< | ||
| + | set size ratio -1 # for plot | ||
| + | set view equal xyz # for splot | ||
| + | </ | ||
| + | |||
| + | * Multiplot.< | ||
| + | set multiplot layout 1,2 title " | ||
| + | plot <...> | ||
| + | plot <...> | ||
| + | unset multiplot | ||
| + | </ | ||
| + | set multiplot | ||
| + | set size 0.5,1.0 | ||
| + | set origin 0,0 | ||
| + | plot <...> | ||
| + | set size 0.5,1.0 | ||
| + | set origin 0.5,0 | ||
| + | plot <...> | ||
| + | unset multiplot | ||
| + | </ | ||
| + | |||
| + | |||
| + | * New plot window.< | ||
| + | plot <...> | ||
| + | set term wxt 1 | ||
| + | plot <...> | ||
| + | set term wxt 2 | ||
| + | plot <...> | ||
| + | </ | ||
| + | |||
| + | * Data separator (space by default).< | ||
| + | set datafile separator "," | ||
| + | </ | ||
| + | |||
| + | * Plot type (histogram)< | ||
| + | set style data histogram | ||
| + | set style histogram errorbars gap 0 lw 2 | ||
| + | set style fill solid border -1 | ||
| + | </ | ||
| + | |||
| + | * Clear window.< | ||
| + | clear | ||
| + | </ | ||
| + | |||
| + | ===== plot ===== | ||
| + | ==== Plot functions ==== | ||
| + | |||
| + | FIXME | ||
| + | |||
| + | ==== Plot data from a file ==== | ||
| + | |||
| + | * **plot** With only one or two columns in the file.< | ||
| + | plot " | ||
| + | </ | ||
| + | |||
| + | * **using/u** Specifying columns to use. If only one is specified, entry number (line number) is used for x.<code gnuplot> | ||
| + | plot " | ||
| + | plot " | ||
| + | </ | ||
| + | |||
| + | * **every** Specifying lines to use (every 2 lines from line 50 to line 1050).< | ||
| + | plot " | ||
| + | </ | ||
| + | |||
| + | * **separator** Set the field separator:< | ||
| + | set datafile separator "," | ||
| + | </ | ||
| + | |||
| + | * **,** Several curves in the same plot.< | ||
| + | plot " | ||
| + | </ | ||
| + | plot \ | ||
| + | " | ||
| + | " | ||
| + | </ | ||
| + | |||
| + | * **title/t** Functions titles.< | ||
| + | plot " | ||
| + | </ | ||
| + | * Use a variable in title.< | ||
| + | a=2 | ||
| + | plot " | ||
| + | </ | ||
| + | |||
| + | * **rgb** Colors.< | ||
| + | show palette colornames | ||
| + | plot " | ||
| + | plot " | ||
| + | </ | ||
| + | |||
| + | * **palette** Colors as value.< | ||
| + | set palette defined (0 " | ||
| + | plot " | ||
| + | </ | ||
| + | |||
| + | * **axes** Axes : x1y1 (bottom-left), | ||
| + | plot " | ||
| + | </ | ||
| + | set ytics nomirror | ||
| + | set y2tics -3.5, | ||
| + | # or | ||
| + | set y2tics auto | ||
| + | </ | ||
| + | |||
| + | * **with** Line type.< | ||
| + | # line | ||
| + | plot " | ||
| + | # line with custom style | ||
| + | plot " | ||
| + | # line with custom line width | ||
| + | plot " | ||
| + | # points | ||
| + | plot " | ||
| + | # points of custom type | ||
| + | plot " | ||
| + | # lines AND points | ||
| + | plot " | ||
| + | # labels in addition to points | ||
| + | plot " | ||
| + | |||
| + | # to find out point types and line width available | ||
| + | test | ||
| + | </ | ||
| + | |||
| + | * **($1)** Arithmetics on columns data.< | ||
| + | plot " | ||
| + | # where $n means column n | ||
| + | </ | ||
| + | |||
| + | * **(column())** Arithmetics on columns indices.< | ||
| + | plot i=1, " | ||
| + | </ | ||
| + | |||
| + | * **fun()=** Define variables and functions, use predefined functions ('' | ||
| + | theta=7.72 * (2*pi/360) | ||
| + | dx=6.7866 | ||
| + | dy=10.1704 | ||
| + | rotation_x(x, | ||
| + | rotation_y(x, | ||
| + | |||
| + | plot " | ||
| + | </ | ||
| + | |||
| + | * **word()** Define array variables.< | ||
| + | colors=" | ||
| + | print word(colors, | ||
| + | </ | ||
| + | |||
| + | * **(?:)** Conditions.< | ||
| + | max(x, | ||
| + | </ | ||
| + | use(x)=(x> | ||
| + | </ | ||
| + | |||
| + | * **for** Plot iteration.< | ||
| + | plot for [file in " | ||
| + | </ | ||
| + | plot for [i=1:3] run.dat using i t sprintf(" | ||
| + | </ | ||
| + | file(n) = sprintf(" | ||
| + | plot for [i=1:3] file(i) using 1:2 | ||
| + | </ | ||
| + | |||
| + | * **fun()=(var=** Make operations with different lines using variables assignment in functions. | ||
| + | * Make sequential operations, the final result is the value of the last operation:< | ||
| + | prev_x=0 | ||
| + | tmp=0 | ||
| + | sum_prev(x)=(tmp=prev_x, | ||
| + | </ | ||
| + | * Using cumulated values of a column.< | ||
| + | sum=0 | ||
| + | cumulated(x)=(sum=sum+x, | ||
| + | plot " | ||
| + | </ | ||
| + | plot sum=0, " | ||
| + | </ | ||
| + | * Subtract the initial value of a column.< | ||
| + | t0=0 | ||
| + | deltat(t)=((t0< | ||
| + | plot " | ||
| + | </ | ||
| + | * Using difference of two consecutive lines of the same column.< | ||
| + | plot prev=-9999, " | ||
| + | </ | ||
| + | * Making an angle continuous.< | ||
| + | k=0 | ||
| + | prev_a=0 | ||
| + | continuous(a)=((prev_a> | ||
| + | </ | ||
| + | * You may also use '' | ||
| + | plot "< awk ' | ||
| + | plot "< awk 'NR==1 { prev=$2 } NR>1 { dt=$2-prev; prev=$2; print $1,dt; }' idSlamDala.dat" | ||
| + | </ | ||
| + | * Bins mean + eval.< | ||
| + | myplot(x,t) = sprintf(" | ||
| + | NR==1 { i=0; s=0; for(j=0; | ||
| + | NR>=1 { s=s-tab[i]; tab[i]=\$%d; | ||
| + | NR> | ||
| + | using 1 t \" | ||
| + | eval myplot(2, " | ||
| + | </ | ||
| + | * Sliding mean + continous angle + gunzip.< | ||
| + | plot "< gunzip -c $ARGV_FILE | awk '\ | ||
| + | NR==1 { k=0; prev_a=0; i=0; pi=3.14159265358979 } \ | ||
| + | NR>=1 { a=\$2; k=(prev_a> | ||
| + | NR> | ||
| + | using (deltat(\$1)/ | ||
| + | </ | ||
| + | |||
| + | * **paste** Plotting one curve with values from different files. Not possible with gnuplot, but '' | ||
| + | plot "< paste file1.dat file2.dat" | ||
| + | </ | ||
| + | |||
| + | * **gunzip** Use file compressed with gzip. Not possible with gnuplot, but '' | ||
| + | plot "< gunzip -c file.dat" | ||
| + | plot "< zcat file.dat" | ||
| + | plot "< tar -Oxjf file.dat.tar.bz2" | ||
| + | </ | ||
| + | |||
| + | * or anything else with shell commands... | ||
| + | |||
| + | * **fit** a function to data:< | ||
| + | f(x) = a*x**2 + b*x + c | ||
| + | fit f(x) " | ||
| + | plot " | ||
| + | </ | ||
| + | |||
| + | * **sh** Passing arguments to a gnuplot script. | ||
| + | * (preferred method) Putting the gnuplot commands in a shell script (be careful, if you forget a backslash in front of a gnuplot dollar the error message can seem mysterious):< | ||
| + | #!/bin/sh | ||
| + | ARGV1=$1 | ||
| + | gnuplot << EOF | ||
| + | plot " | ||
| + | EOF | ||
| + | </ | ||
| + | ./plot.gp run.dat | ||
| + | </ | ||
| + | #!/bin/sh | ||
| + | ###### | ||
| + | script_header=`cat<< | ||
| + | set term wxt size 1024,768 | ||
| + | set grid | ||
| + | plot \ | ||
| + | EOF | ||
| + | ` | ||
| + | script=$script_header | ||
| + | ###### | ||
| + | for host in $*; do | ||
| + | script_loop=`cat<< | ||
| + | " | ||
| + | EOF | ||
| + | ` | ||
| + | script=" | ||
| + | done | ||
| + | ###### | ||
| + | script_footer=`cat<< | ||
| + | |||
| + | pause -1 | ||
| + | EOF | ||
| + | ` | ||
| + | script=" | ||
| + | |||
| + | gnuplot<< | ||
| + | $(echo " | ||
| + | EOF | ||
| + | </ | ||
| + | * Writing a generic launcher script with environment variables (but difficulty to deal with relative paths):< | ||
| + | #!/bin/sh | ||
| + | export ARGV1=$2; export ARGV2=$3; export ARGV3=$4; export ARGV4=$5; | ||
| + | cat $1 | gnuplot | ||
| + | export -n ARGV1; export -n ARGV2; export -n ARGV3; export -n ARGV4; | ||
| + | </ | ||
| + | #!gplaunch | ||
| + | plot "`echo $ARGV1`" | ||
| + | </ | ||
| + | gplaunch plot.gp run.dat | ||
| + | ./plot.gp run.dat # if gplaunch is installed on the system | ||
| + | </ | ||
| + | * Writing a generic launcher script with '' | ||
| + | #!/bin/sh | ||
| + | cat $1 | sed " | ||
| + | </ | ||
| + | #!gplaunch | ||
| + | plot " | ||
| + | </ | ||
| + | gplaunch plot.gp run.dat | ||
| + | ./plot.gp run.dat # if gplaunch is installed on the system | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== 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 " | ||
| + | set output ' | ||
| + | |||
| + | # your plotting code (can be included using the load command) | ||
| + | |||
| + | set output | ||
| + | !epstopdf --outfile=plot.pdf plot.eps | ||
| + | quit | ||
| + | </ | ||
| + | plot [...] with lines ls 1 lw 2 lt rgb " | ||
| + | </ | ||
| + | set grid lc rgb " | ||
| + | </ | ||
| + | |||
| + | * PNG:< | ||
| + | set terminal png size 450,360 small | ||
| + | set output ' | ||
| + | |||
| + | # your plotting code (can be included using the load command) | ||
| + | |||
| + | quit | ||
| + | </ | ||
| + | set output | ||
| + | !convert -density 600x600 -background white -flatten plot.eps plot.png | ||
| + | quit | ||
| + | </ | ||
| + | set output | ||
| + | !convert -density 600x600 -background white -flatten plot.eps ppm:- | convert - plot.png | ||
| + | quit | ||
| + | </ | ||
| + | * SVG:< | ||
| + | set terminal svg rounded size 450,360 | ||
| + | set output ' | ||
| + | |||
| + | # your plotting code (can be included using the load command) | ||
| + | |||
| + | set output | ||
| + | !gzip -S z plot.svg | ||
| + | quit | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | * Conditional output:< | ||
| + | #!/bin/sh | ||
| + | |||
| + | TITLE=plot | ||
| + | if [[ $1 == " | ||
| + | GTERM=' | ||
| + | OUTPUT=' | ||
| + | GRIDCOLOR=' | ||
| + | shift | ||
| + | elif [[ $1 == " | ||
| + | GTERM=' | ||
| + | OUTPUT='' | ||
| + | GRIDCOLOR='' | ||
| + | shift | ||
| + | else | ||
| + | GTERM=' | ||
| + | OUTPUT='' | ||
| + | GRIDCOLOR='' | ||
| + | fi | ||
| + | |||
| + | gnuplot -persist << EOF | ||
| + | |||
| + | $GTERM | ||
| + | set grid $GRIDCOLOR | ||
| + | |||
| + | # your plotting code (can be included using the load command) | ||
| + | |||
| + | set output | ||
| + | $OUTPUT | ||
| + | | ||
| + | EOF | ||
| + | </ | ||
| + | |||
| + | ===== Interactivity ===== | ||
| + | |||
| + | * Keyboard bindings< | ||
| + | bind " | ||
| + | </ | ||
| + | |||
| + | * Progressive display (but the plot is not interactive anymore)< | ||
| + | plot " | ||
| + | while(1) { replot ; pause 0.2 } | ||
| + | </ | ||
| + | |||
| + | * Pausing and resuming the progressive display for interactivity (only works with x11 terminal if started in interactive gnuplot shell... for wxt terminal the event processing loop -- wxt_gui.cpp: | ||
| + | set terminal x11 | ||
| + | bind " | ||
| + | plot " | ||
| + | update=1 | ||
| + | while(update) { replot ; pause 0.2 } | ||
| + | </ | ||
| + | |||
| + | ===== Example ===== | ||
| + | |||
| + | This is a typical example that uses the most useful features to plot data from a file. It is easy to copy and adapt for your own use. | ||
| + | |||
| + | <code gnuplot> | ||
| + | set term wxt size 1024,768 | ||
| + | |||
| + | # file to plot: | ||
| + | file=" | ||
| + | |||
| + | # to have smaller crosses: | ||
| + | set pointsize 0.3333333333333 | ||
| + | |||
| + | # to have the same scale along x and y axis: | ||
| + | #set size ratio -1 | ||
| + | |||
| + | # to display a grid: | ||
| + | #set grid | ||
| + | |||
| + | # to have a different right y axis: | ||
| + | set y2tics auto | ||
| + | set ytics nomirror | ||
| + | |||
| + | # titles | ||
| + | set title " | ||
| + | set xlabel "Time (s)" | ||
| + | set ylabel " | ||
| + | set y2label "Angle (deg)" | ||
| + | |||
| + | # to define some functions: | ||
| + | norm3(x, | ||
| + | pitch(x, | ||
| + | roll(x,y,z) = atan2(y, z)*180/pi | ||
| + | |||
| + | # to define some variables: | ||
| + | t0=1281469302 | ||
| + | |||
| + | # now plot: | ||
| + | plot \ | ||
| + | file using ($1-t0):15 with points pt 1 lt rgb " | ||
| + | file using ($1-t0):16 with points pt 1 lt rgb " | ||
| + | file using ($1-t0):17 with points pt 1 lt rgb " | ||
| + | file using ($1-t0): | ||
| + | file using ($1-t0): | ||
| + | file using ($1-t0): | ||
| + | |||
| + | |||
| + | # prevent window from closing at the end | ||
| + | pause -1 | ||
| + | </ | ||
