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
Last revision Both sides next revision
software:gnuplot [2011/07/14 14:50]
cyril [File outputs]
software:gnuplot [2024/04/19 09:52]
cyril columnheader
Line 44: Line 44:
 </code> Can be used to remove tics labels:<code gnuplot> </code> Can be used to remove tics labels:<code gnuplot>
 set format x "" 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>
  
Line 55: Line 59:
 set x2label "" set x2label ""
 set y2label "" set y2label ""
 +</code>
 +
 +  * Log scale.<code gnuplot>
 +set logscale x 2
 +set logscale y 10
 </code> </code>
  
Line 103: Line 112:
   * Data separator (space by default).<code gnuplot>   * Data separator (space by default).<code gnuplot>
 set datafile separator "," set datafile separator ","
 +</code>
 +
 +  * Plot type (histogram)<code gnuplot>
 +set style data histogram
 +set style histogram errorbars gap 0 lw 2
 +set style fill solid border -1
 </code> </code>
  
Line 127: Line 142:
   * **every** Specifying lines to use (every 2 lines from line 50 to line 1050).<code gnuplot>   * **every** Specifying lines to use (every 2 lines from line 50 to line 1050).<code gnuplot>
 plot "data.dat" using 4:5 every 2::50::1050 plot "data.dat" using 4:5 every 2::50::1050
 +</code>
 +
 +  * **separator** Set the field separator:<code gnuplot>
 +set datafile separator ","
 </code> </code>
  
Line 149: Line 168:
 plot "data1.dat" using 4:5 lt rgb "red" plot "data1.dat" using 4:5 lt rgb "red"
 plot "data1.dat" using 4:5 lt rgb "#FF0000" plot "data1.dat" using 4:5 lt rgb "#FF0000"
 +</code>
 +
 +  * **palette** Colors as value.<code gnuplot>
 +set palette defined (0 "black", 1 "red")
 +plot "data1.dat" using 1:2:0 w p palette
 </code> </code>
  
Line 180: Line 204:
 </code> </code>
  
-  * **($1)** Arithmetics on columns.<code gnuplot>+  * **($1)** Arithmetics on columns data.<code gnuplot>
 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>
 +
 +  * **(column())** Arithmetics on columns indices.<code gnuplot>
 +plot i=1, "data1.dat" using 1:(column(i+1))
 </code> </code>
  
Line 193: Line 221:
  
 plot "gps.dat" using (rotation_x($3,$4)):(rotation_y($3,$4)) plot "gps.dat" using (rotation_x($3,$4)):(rotation_y($3,$4))
 +</code>
 +
 +  * **word()** Define array variables.<code gnuplot>
 +colors="red blue green"
 +print word(colors,3)
 </code> </code>
  
Line 203: Line 236:
   * **for** Plot iteration.<code gnuplot>   * **for** Plot iteration.<code gnuplot>
 plot for [file in "run1.dat run2.dat run3.dat"] file using 1:2 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> </code><code gnuplot>
 file(n) = sprintf("run_%d.dat",n) file(n) = sprintf("run_%d.dat",n)
Line 238: Line 273:
 plot "< awk 'NR==1 { prev=$2 } NR>1 { dt=$2-prev; prev=$2; print $1,dt; }' idSlamDala.dat" using 1:2 plot "< awk 'NR==1 { prev=$2 } NR>1 { dt=$2-prev; prev=$2; print $1,dt; }' idSlamDala.dat" using 1:2
 </code> </code>
-    * Sliding mean.<code gnuplot>+    * 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 '\ plot "< gunzip -c $ARGV_FILE | awk '\
     NR==1 { k=0; prev_a=0; i=0; pi=3.14159265358979 } \     NR==1 { k=0; prev_a=0; i=0; pi=3.14159265358979 } \
Line 253: Line 296:
 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 "< zcat file.dat" using 1:($4-$2)
 +plot "< tar -Oxjf file.dat.tar.bz2" using 1:($4-$2)
 </code> </code>
  
   * or anything else with shell commands...   * 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.   * **sh** Passing arguments to a gnuplot script.
-    * 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>+    * (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 #!/bin/sh
 ARGV1=$1 ARGV1=$1
Line 266: Line 316:
 </code><code sh> </code><code sh>
 ./plot.gp run.dat ./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> </code>
     * Writing a generic launcher script with environment variables (but difficulty to deal with relative paths):<code sh|gplaunch>     * Writing a generic launcher script with environment variables (but difficulty to deal with relative paths):<code sh|gplaunch>
Line 304: Line 383:
 !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 gnuplot> +</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>
  
Line 333: Line 414:
 !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> </code>
  
software/gnuplot.txt · Last modified: 2024/04/19 09:54 by cyril
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0