Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
software:gnuplot [2010/08/18 08:04]
cyril gunzip
software:gnuplot [2024/04/19 09:54] (current)
cyril autotitle
Line 8: Line 8:
  
 <code gnuplot> <code gnuplot>
-load "plot_data.gnuplot"+load "plot_data.gp"
 </code> </code>
  
 You can also directly load it when starting gnuplot, in shell: You can also directly load it when starting gnuplot, in shell:
-<code> +<code gnuplot
-gnuplot -persist plot_data.gnuplot+gnuplot -persist plot_data.gp
 </code> </code>
 +If the file ends with "pause -1", you can omit the "-persist" argument.
 +
 ===== set ===== ===== set =====
-  * Window size +  * Window size.<code gnuplot>
-<code>+
 set term wxt size 1024,768 set term wxt size 1024,768
 set term x11 size 1024,768 set term x11 size 1024,768
 </code> </code>
  
-  * Axes ranges +  * Axes ranges.<code gnuplot>
-<code>+
 set xrange [0:1000] set xrange [0:1000]
 set yrange [-100:100] set yrange [-100:100]
Line 31: Line 31:
 </code> </code>
  
-  * Axes tics +  * Axes tics.<code gnuplot>
-<code>+
 set ytics nomirror 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 set y2tics auto
 +set ytics 0.5
 +set ytics -3.5,0.5,3.5
 +set xtics ("0.5" 0.5, "1.2" 1.2, "2.3" 2.3)
 </code> </code>
  
-  * Axes tics labels +  * Axes tics labels.<code gnuplot>
-<code>+
 set format x "%f" set format x "%f"
 set format y "%e" set format y "%e"
 +</code> Can be used to remove tics labels:<code gnuplot>
 +set format x ""
 +</code>For time formats:<code gnuplot>
 +set ydata time
 +set timefmt "%M:%S" # input format in data
 +set format y "%s"   # outpout format in labels
 </code> </code>
  
-  * Grid +  * Grid.<code gnuplot>
-<code>+
 set grid set grid
 </code> </code>
  
-  * Axes labels +  * Axes labels.<code gnuplot>
-<code>+
 set xlabel "" set xlabel ""
 set ylabel "" set ylabel ""
Line 58: Line 61:
 </code> </code>
  
-  * Graph title +  * Log scale.<code gnuplot> 
-<code>+set logscale x 2 
 +set logscale y 10 
 +</code> 
 + 
 +  * Graph title.<code gnuplot>
 set title "" set title ""
 </code> </code>
  
-  * Line options +  * Legend position.<code gnuplot> 
-<code>+# available: left, right, top, bottom, outside, and below 
 +set key left bottom 
 +set key 100,100 
 +</code> 
 + 
 +  * Automatic legend.<code gnuplot> 
 +set key autotitle columnheader 
 +</code> 
 + 
 +  * Line options.<code gnuplot>
 set pointsize 0.33333333 set pointsize 0.33333333
 </code> </code>
  
-  * Multiplot +  * Size ratio (-1 to have same scale on x and y axis).<code gnuplot> 
-<code>+set size ratio -1   # for plot 
 +set view equal xyz  # for splot 
 +</code> 
 + 
 +  * Multiplot.<code gnuplot> 
 +set multiplot layout 1,2 title "global title" 
 +plot <...> 
 +plot <...> 
 +unset multiplot 
 +</code><code gnuplot>
 set multiplot set multiplot
 set size 0.5,1.0 set size 0.5,1.0
Line 76: Line 101:
 set size 0.5,1.0 set size 0.5,1.0
 set origin 0.5,0 set origin 0.5,0
-plot <...> 
-unset multiplot 
-</code> 
- 
-<code> 
-set multiplot layout 1,2 title "global title" 
-plot <...> 
 plot <...> plot <...>
 unset multiplot unset multiplot
Line 88: Line 106:
  
  
-  * New plot window +  * New plot window.<code gnuplot>
-<code>+
 plot <...> plot <...>
 set term wxt 1 set term wxt 1
Line 97: Line 114:
 </code> </code>
  
-  * Data separator (space by default) +  * Data separator (space by default).<code gnuplot>
-<code>+
 set datafile separator "," set datafile separator ","
 </code> </code>
  
-  * Clear window +  * Plot type (histogram)<code gnuplot> 
-<code>+set style data histogram 
 +set style histogram errorbars gap 0 lw 2 
 +set style fill solid border -1 
 +</code> 
 + 
 +  * Clear window.<code gnuplot>
 clear clear
 </code> </code>
  
 ===== plot ===== ===== plot =====
 +==== Plot functions ====
 +
 +FIXME
  
 ==== Plot data from a file ==== ==== Plot data from a file ====
  
-  * With only one or two columns in the file. +  * **plot** With only one or two columns in the file.<code gnuplot>
-<code>+
 plot "data.dat" plot "data.dat"
 </code> </code>
  
-  * Specifying columns to use. If only one is specified, entry number (line number) is used for x. +  * **using/u** Specifying columns to use. If only one is specified, entry number (line number) is used for x.<code gnuplot>
-<code>+
 plot "data.dat" using 4 plot "data.dat" using 4
 plot "data.dat" using 4:5 plot "data.dat" using 4:5
 </code> </code>
  
-  * Specifying lines to use (every 2 lines from line 50 to line 1050) +  * **every** Specifying lines to use (every 2 lines from line 50 to line 1050).<code gnuplot>
-<code>+
 plot "data.dat" using 4:5 every 2::50::1050 plot "data.dat" using 4:5 every 2::50::1050
 </code> </code>
  
-  * Several curves in the same plot +  * **separator** Set the field separator:<code gnuplot> 
-<code>+set datafile separator "," 
 +</code> 
 + 
 +  * **,** Several curves in the same plot.<code gnuplot>
 plot "data1.dat" using 4:5, "data2.dat" using 6:7 plot "data1.dat" using 4:5, "data2.dat" using 6:7
 +</code><code gnuplot>
 +plot \
 +  "data1.dat" using 4:5, \
 +  "data2.dat" using 6:7
 </code> </code>
  
-  * Functions titles +  * **title/t** Functions titles.<code gnuplot
-<code> +plot "data1.dat" using 4:5 title "my curve" 
-plot "data1.dat" using 4:5 "my curve"+</code> 
 +    * Use a variable in title.<code gnuplot> 
 +a=2 
 +plot "data1.dat" using 4:5 title sprintf("curve for a=%.5g",a)
 </code> </code>
  
-  * Colors +  * **rgb** Colors.<code gnuplot>
-<code>+
 show palette colornames show palette colornames
 plot "data1.dat" using 4:5 lt rgb "red" plot "data1.dat" using 4:5 lt rgb "red"
Line 144: Line 174:
 </code> </code>
  
-  * Axes : x1y1 (bottom-left)x2y2 (top-right), x1y2, x2y1 +  * **palette** Colors as value.<code gnuplot> 
-<code> +set palette defined (0 "black"1 "red"
-plot "data1.dat" using 4:5 axes x1y1, "data2.dat" using 6:7 axes x1y2+plot "data1.dat" using 1:2:0 w p palette
 </code> </code>
-If you want to display different tics on y2 axis, do before the plot command: + 
-<code>+  * **axes** Axes : x1y1 (bottom-left), x2y2 (top-right), x1y2, x2y1.<code gnuplot> 
 +plot "data1.dat" using 4:5 axes x1y1, "data2.dat" using 6:7 axes x1y2 
 +</code>If you want to display different tics on y2 axis, do before the plot command:<code gnuplot>
 set ytics nomirror set ytics nomirror
 set y2tics -3.5,0.5,3.5 set y2tics -3.5,0.5,3.5
Line 156: Line 188:
 </code> </code>
  
-  * Line type +  * **with** Line type.<code gnuplot>
-<code>+
 # line # line
 plot "data1.dat" using 4:5 with lines plot "data1.dat" using 4:5 with lines
Line 177: Line 208:
 </code> </code>
  
-  * Arithmetics on columns +  * **($1)** Arithmetics on columns data.<code gnuplot>
-<code>+
 plot "data1.dat" using 4:($5/2+14) plot "data1.dat" using 4:($5/2+14)
 # where $n means column n # where $n means column n
 </code> </code>
  
-You can use very complicated formulas, define functions and do computations before the plot commandFor instance to rotate and translate a trajectory before to display it+  * **(column())** Arithmetics on columns indices.<code gnuplot> 
-<code>+plot i=1, "data1.dat" using 1:(column(i+1)) 
 +</code> 
 + 
 +  * **fun()=** Define variables and functions, use predefined functions (''help functions'').<code gnuplot>
 theta=7.72 * (2*pi/360) theta=7.72 * (2*pi/360)
 dx=6.7866 dx=6.7866
Line 194: Line 227:
 </code> </code>
  
-  * Using cumulated values of a column +  * **word()** Define array variables.<code gnuplot> 
-<code>+colors="red blue green" 
 +print word(colors,3) 
 +</code> 
 + 
 +  * **(?:)** Conditions.<code gnuplot> 
 +max(x,y)=(x>y?y:x) 
 +</code>Ignore line under condition:<code gnuplot> 
 +use(x)=(x>0?x:0/0) 
 +</code> 
 + 
 +  * **for** Plot iteration.<code gnuplot> 
 +plot for [file in "run1.dat run2.dat run3.dat"] file using 1:2 
 +</code><code gnuplot> 
 +plot for [i=1:3] run.dat using i t sprintf("curve %s", columnheader(i)) 
 +</code><code gnuplot> 
 +file(n) = sprintf("run_%d.dat",n) 
 +plot for [i=1:3] file(i) using 1:2 
 +</code> 
 + 
 +  * **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:<code gnuplot> 
 +prev_x=0 
 +tmp=0 
 +sum_prev(x)=(tmp=prev_x, prev_x=x, x+tmp) 
 +</code> 
 +    * Using cumulated values of a column.<code gnuplot> 
 +sum=0 
 +cumulated(x)=(sum=sum+x, sum) 
 +plot "idSlamDala.dat" using (cumulated($2)):
 +</code><code gnuplot> 
 +plot sum=0, "idSlamDala.dat" using (sum=sum+$2,sum):
 +</code> 
 +    * Subtract the initial value of a column.<code gnuplot> 
 +t0=0 
 +deltat(t)=((t0<=1)?(t0=t,0):(t-t0)) 
 +plot "idSlamDala.dat" using (deltat($1)):
 +</code> 
 +    * Using difference of two consecutive lines of the same column.<code gnuplot> 
 +plot prev=-9999, "idSlamDala.dat" using 1:(dt=(prev==-9999?0/0:$2-prev), prev=$2, dt)  
 +</code> 
 +    * Making an angle continuous.<code gnuplot> 
 +k=0 
 +prev_a=0 
 +continuous(a)=((prev_a>pi/2&&a<-pi/2 ? k=k+1 : (prev_a<-pi/2&&a>pi/2 ? k=k-1 : k)), prev_a=a, a+k*2*pi) 
 +</code> 
 +    * You may also use ''awk'' to have a more explicit program, but it won't automatically ignore comment lines:<code gnuplot>
 plot "< awk '{sum=sum+$2; print sum,$6}' idSlamDala.dat" using 1:2 plot "< awk '{sum=sum+$2; print sum,$6}' idSlamDala.dat" using 1:2
-# sum of column 2+plot "< awk 'NR==1 { prev=$} NR>1 { dt=$2-prev; prev=$2; print $1,dt; }' idSlamDala.dat" using 1:2 
 +</code> 
 +    * Bins mean + eval.<code gnuplot> 
 +myplot(x,t) = sprintf("plot \"< awk '\ 
 +    NR==1 { i=0; s=0; for(j=0;j<$ARGV_NMEAN;j++) tab[j]=0; } \ 
 +    NR>=1 { s=s-tab[i]; tab[i]=\$%d; s=s+tab[i]; i=(i>=($ARGV_NMEAN-1)?0:i+1); } \ 
 +    NR>$ARGV_NMEAN { if (i==0) print s/$ARGV_NMEAN; }' $ARGV_FILE\"
 +    using 1 t \"%s\"", x, t) 
 +eval myplot(2, "ax"
 +</code> 
 +    * Sliding mean + continous angle + gunzip.<code gnuplot> 
 +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>pi/2 && a<-pi/2 ? k+1 : (prev_a<-pi/2 && a>pi/2 ? k-1 : k)); prev_a=a; a=a+k*2*pi; t=\$1; taba[i]=a; tabt[i]=t; i=(i>=($ARGV_NMEAN-1)?0:i+1); } \ 
 +    NR>$ARGV_NMEAN { bias=(a-taba[i])/((t-tabt[i])/3600); print t,bias; }' "\ 
 +    using (deltat(\$1)/3600):(\$2*180/pi) with points pt 0 lt rgb "red"  title "bias"
 </code> </code>
  
-  * Plotting one curve with values from different files +  * **paste** Plotting one curve with values from different files. Not possible with gnuplot, but ''paste'' can help.<code gnuplot>
-<code>+
 plot "< paste file1.dat file2.dat" using 1:($4-$2) plot "< paste file1.dat file2.dat" using 1:($4-$2)
 </code> </code>
  
-  * Using directly a data file compressed with gzip +  * **gunzip** Use file compressed with gzip. Not possible with gnuplot, but ''gunzip'' can help.<code gnuplot>
-<code>+
 plot "< gunzip -c file.dat" using 1:($4-$2) plot "< gunzip -c file.dat" using 1:($4-$2)
 +plot "< zcat file.dat" using 1:($4-$2)
 +plot "< tar -Oxjf file.dat.tar.bz2" using 1:($4-$2)
 +</code>
 +
 +  * or anything else with shell commands...
 +
 +  * **fit** a function to data:<code gnuplot>
 +f(x) = a*x**2 + b*x + c
 +fit f(x) "file.dat" u 1:2 via a,b,c
 +plot "file.dat" u 1:2, f(x)
 +</code>
 +
 +  * **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):<code gnuplot|plot.gp>
 +#!/bin/sh
 +ARGV1=$1
 +gnuplot << EOF
 +plot "$ARGV1" using 1:(\$2*3)
 +EOF
 +</code><code sh>
 +./plot.gp run.dat
 +</code>You can also build a more complicated plot file with multiple steps (for multiplot or multiple curves):<code gnuplot>
 +#!/bin/sh
 +######
 +script_header=`cat<<EOF
 +set term wxt size 1024,768
 +set grid
 +plot \
 +EOF
 +`
 +script=$script_header
 +######
 +for host in $*; do
 +script_loop=`cat<<EOF
 +    "file_$host.log" u 4:5 w l lt rgb "blue" t "Position $host", \
 +EOF
 +`
 +script="$script$script_loop"
 +done
 +######
 +script_footer=`cat<<EOF
 +
 +pause -1
 +EOF
 +`
 +script="$script$script_footer"
 +
 +gnuplot<<EOF
 +$(echo "$script")
 +EOF
 +</code>
 +    * Writing a generic launcher script with environment variables (but difficulty to deal with relative paths):<code sh|gplaunch>
 +#!/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;
 +</code><code gnuplot|plot.gp>
 +#!gplaunch
 +plot "`echo $ARGV1`" using 1:($2*3)
 +</code><code sh>
 +gplaunch plot.gp run.dat
 +./plot.gp run.dat # if gplaunch is installed on the system
 +</code>
 +    * Writing a generic launcher script with ''sed'' (but difficulty to deal with relative paths):<code sh|gplaunch>
 +#!/bin/sh
 +cat $1 | sed "s/\$ARGV1/$2/g" | sed "s/\$ARGV2/$3/g" | sed "s/\$ARGV3/$4/g" | sed "s/\$ARGV4/$5/g" | gnuplot
 +</code><code gnuplot|plot.gp>
 +#!gplaunch
 +plot "$ARGV1" using 1:($2*3)
 +</code><code sh>
 +gplaunch plot.gp run.dat
 +./plot.gp run.dat # if gplaunch is installed on the system
 </code> </code>
  
-etc with shell commands... 
  
 ===== File outputs ===== ===== File outputs =====
Line 216: Line 378:
 In general be sure that you don't have another "set term" command in your plotting code. In general be sure that you don't have another "set term" command in your plotting code.
  
-  * EPS and PDF:<code>+  * EPS and PDF:<code gnuplot>
 set terminal postscript eps color "Times-Roman" 16 set terminal postscript eps color "Times-Roman" 16
 set output 'plot.eps' set output 'plot.eps'
  
-you plotting code (can be included using the load command)+your plotting code (can be included using the load command)
  
 set output set output
 !epstopdf --outfile=plot.pdf plot.eps !epstopdf --outfile=plot.pdf plot.eps
 quit quit
-</code>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:<code> +</code>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 either add the "solid" option to the terminal command, or add the "ls 1" command:<code gnuplot
-plot [...] with lines linestyle 1 lt rgb "red"+plot [...] with lines ls lw 2 lt rgb "red
 +</code>Also the grid is black by default, if you want to set it to grey:<code gnuplot> 
 +set grid lc rgb "grey"
 </code> </code>
  
-  * PNG:<code>+  * PNG:<code gnuplot>
 set terminal png size 450,360 small set terminal png size 450,360 small
 set output 'plot.png' set output 'plot.png'
  
-you plotting code (can be included using the load command)+your plotting code (can be included using the load command)
  
 +quit
 +</code>If you need dashes but want raster PNG output (eg because the eps would be too large), you can use the eps terminal and convert the file to png at the end with imagemagick:<code gnuplot>
 +set output
 +!convert -density 600x600 -background white -flatten plot.eps plot.png
 +quit
 +</code>When converting eps to png, convert creates a png with non visible alpha data even if flatten, and some viewers like ''evince'' fail to antialias them. So you have to convert it twice:<code gnuplot>
 +set output
 +!convert -density 600x600 -background white -flatten plot.eps ppm:- | convert - plot.png
 quit quit
 </code> </code>
- +  * SVG:<code gnuplot>
-  * SVG:<code>+
 set terminal svg rounded size 450,360 set terminal svg rounded size 450,360
 set output 'plot.svg' set output 'plot.svg'
  
-you plotting code (can be included using the load command)+your plotting code (can be included using the load command)
  
 set output set output
 !gzip -S z plot.svg !gzip -S z plot.svg
 quit quit
 +</code>
 +
 +
 +
 +  * Conditional output:<code gnuplot>
 +#!/bin/sh
 +
 +TITLE=plot
 +if [[ $1 == "pdf" ]]; then
 +    GTERM='set terminal postscript eps color "Times-Roman" 16 solid size 5,2.9 ; set output "$TITLE.eps"'
 +    OUTPUT='!epstopdf --outfile=$TITLE.pdf $TITLE.eps'
 +    GRIDCOLOR='lc rgb "grey"'
 +    shift
 +elif [[ $1 == "png" ]]; then
 +    GTERM='set terminal pngcairo font "Times,32" size 1830,1780 ; set output "$TITLE.png"'
 +    OUTPUT=''
 +    GRIDCOLOR=''
 +    shift
 +else
 +    GTERM='set term wxt size 1024,768'
 +    OUTPUT=''
 +    GRIDCOLOR=''
 +fi
 +
 +gnuplot -persist << EOF
 +
 +$GTERM
 +set grid $GRIDCOLOR
 +
 +# your plotting code (can be included using the load command)
 +
 +set output
 +    $OUTPUT
 +    
 +EOF
 +</code>
 +
 +===== Interactivity =====
 +
 +  * Keyboard bindings<code gnuplot>
 +bind "a" "plot x*x"
 +</code>
 +
 +  * Progressive display (but the plot is not interactive anymore)<code gnuplot>
 +plot "file.dat" u 1
 +while(1) { replot ; pause 0.2 }
 +</code>
 +
 +  * 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:wxt_waitforinput function -- is not called during the update loop, but ctrl-c in the shell stops the update loop and allows plot interactivity afterwards, contrary to x11 term...).<code gnuplot>
 +set terminal x11
 +bind "c" "update=1-update ; while(update) { replot ; pause 0.1 }"
 +plot "file.dat" u 1
 +update=1
 +while(update) { replot ; pause 0.2 }
 +</code>
 +
 +===== 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="rtslam.log"
 +
 +# 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 "Accelerometer biases and g estimation"
 +set xlabel "Time (s)"
 +set ylabel "Acceleration (m/s2)"
 +set y2label "Angle (deg)"
 +
 +# to define some functions:
 +norm3(x,y,z)=sqrt(x*x+y*y+z*z)
 +pitch(x,y,z) = atan2(-x, z)*180/pi
 +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 "red"       title "Bias AX", \
 +    file using ($1-t0):16 with points pt 1 lt rgb "dark-red"  title "Bias AY", \
 +    file using ($1-t0):17 with points pt 1 lt rgb "orange"    title "Bias AZ", \
 +    file using ($1-t0):(norm3($21,$22,$23)/100)           with points pt 1 lt rgb "blue"       title "Norm g / 100", \
 +    file using ($1-t0):(pitch($21,$22,$23))     axes x1y2 with points pt 1 lt rgb "green"      title "Pitch g", \
 +    file using ($1-t0):(roll($21,$22,$23))      axes x1y2 with points pt 1 lt rgb "dark-green" title "Roll g"
 +
 +
 +# prevent window from closing at the end
 +pause -1
 </code> </code>
  
software/gnuplot.1282118681.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