Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
software:gnuplot [2011/02/07 18:32] cyril code gnuplot |
software:gnuplot [2024/04/19 09:54] (current) cyril autotitle |
||
---|---|---|---|
Line 33: | Line 33: | ||
* Axes tics.< | * Axes tics.< | ||
set ytics nomirror | set ytics nomirror | ||
- | set y2tics -3.5, | ||
- | set ytics (" | ||
set y2tics auto | set y2tics auto | ||
+ | set ytics 0.5 | ||
+ | set ytics -3.5, | ||
+ | set xtics (" | ||
</ | </ | ||
Line 41: | Line 42: | ||
set format x " | set format x " | ||
set format y " | set format y " | ||
+ | </ | ||
+ | set format x "" | ||
+ | </ | ||
+ | set ydata time | ||
+ | set timefmt " | ||
+ | set format y " | ||
</ | </ | ||
Line 52: | Line 59: | ||
set x2label "" | set x2label "" | ||
set y2label "" | set y2label "" | ||
+ | </ | ||
+ | |||
+ | * Log scale.< | ||
+ | set logscale x 2 | ||
+ | set logscale y 10 | ||
</ | </ | ||
Line 62: | Line 74: | ||
set key left bottom | set key left bottom | ||
set key 100,100 | set key 100,100 | ||
+ | </ | ||
+ | |||
+ | * Automatic legend.< | ||
+ | set key autotitle columnheader | ||
</ | </ | ||
* Line options.< | * Line options.< | ||
set pointsize 0.33333333 | 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.< | * Multiplot.< | ||
+ | set multiplot layout 1,2 title " | ||
+ | plot <...> | ||
+ | plot <...> | ||
+ | unset multiplot | ||
+ | </ | ||
set multiplot | set multiplot | ||
set size 0.5,1.0 | set size 0.5,1.0 | ||
Line 75: | 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 | ||
- | </ | ||
- | set multiplot layout 1,2 title " | ||
- | plot <...> | ||
plot <...> | plot <...> | ||
unset multiplot | unset multiplot | ||
Line 95: | Line 116: | ||
* Data separator (space by default).< | * Data separator (space by default).< | ||
set datafile separator "," | set datafile separator "," | ||
+ | </ | ||
+ | |||
+ | * Plot type (histogram)< | ||
+ | set style data histogram | ||
+ | set style histogram errorbars gap 0 lw 2 | ||
+ | set style fill solid border -1 | ||
</ | </ | ||
Line 121: | Line 148: | ||
</ | </ | ||
- | * **,** and **,\** Several curves in the same plot.< | + | * **separator** Set the field separator:< |
+ | set datafile separator "," | ||
+ | </ | ||
+ | |||
+ | * **,** Several curves in the same plot.< | ||
plot " | plot " | ||
+ | </ | ||
+ | plot \ | ||
+ | " | ||
+ | " | ||
</ | </ | ||
* **title/t** Functions titles.< | * **title/t** Functions titles.< | ||
plot " | plot " | ||
+ | </ | ||
+ | * Use a variable in title.< | ||
+ | a=2 | ||
+ | plot " | ||
</ | </ | ||
- | * **lt rgb** Colors.< | + | * **rgb** Colors.< |
show palette colornames | show palette colornames | ||
plot " | plot " | ||
plot " | plot " | ||
+ | </ | ||
+ | |||
+ | * **palette** Colors as value.< | ||
+ | set palette defined (0 " | ||
+ | plot " | ||
</ | </ | ||
Line 144: | Line 188: | ||
</ | </ | ||
- | * **lines, | + | * **with** Line type.< |
# line | # line | ||
plot " | plot " | ||
Line 164: | Line 208: | ||
</ | </ | ||
- | * **($1)** Arithmetics on columns.< | + | * **($1)** Arithmetics on columns |
plot " | plot " | ||
# where $n means column n | # where $n means column n | ||
+ | </ | ||
+ | |||
+ | * **(column())** Arithmetics on columns indices.< | ||
+ | plot i=1, " | ||
</ | </ | ||
Line 179: | Line 227: | ||
</ | </ | ||
- | * **(?:)**Tests.< | + | |
+ | colors=" | ||
+ | print word(colors, | ||
+ | </ | ||
+ | |||
+ | | ||
max(x, | max(x, | ||
+ | </ | ||
+ | use(x)=(x> | ||
</ | </ | ||
- | * **for** Plot repetition.<code gnuplot> | + | * **for** Plot iteration.<code gnuplot> |
plot for [file in " | plot for [file in " | ||
- | </ | + | </ |
+ | plot for [i=1:3] run.dat using i t sprintf(" | ||
+ | </ | ||
file(n) = sprintf(" | file(n) = sprintf(" | ||
plot for [i=1:3] file(i) using 1:2 | plot for [i=1:3] file(i) using 1:2 | ||
</ | </ | ||
- | * **awk** Assign variables in functions and make operations with different lines. | + | * **fun()=(var=** Make operations with different lines using variables assignment in functions. |
- | * Using cumulated values of a column.< | + | * Make sequential operations, the final result is the value of the last operation:< |
- | plot "< | + | prev_x=0 |
+ | tmp=0 | ||
+ | sum_prev(x)=(tmp=prev_x, prev_x=x, x+tmp) | ||
+ | </ | ||
+ | * Using cumulated values of a column.< | ||
+ | sum=0 | ||
+ | cumulated(x)=(sum=sum+x, | ||
+ | plot "idSlamDala.dat" | ||
+ | </ | ||
+ | plot sum=0, " | ||
+ | </ | ||
+ | * Subtract the initial value of a column.< | ||
+ | t0=0 | ||
+ | deltat(t)=((t0< | ||
+ | plot "idSlamDala.dat" | ||
</ | </ | ||
* Using difference of two consecutive lines of the same column.< | * 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" | + | 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** | + | * **paste** |
- | * Plotting one curve with values from different files.<code gnuplot> | + | |
plot "< paste file1.dat file2.dat" | plot "< paste file1.dat file2.dat" | ||
</ | </ | ||
Line 205: | Line 299: | ||
* **gunzip** Use file compressed with gzip. Not possible with gnuplot, but '' | * **gunzip** Use file compressed with gzip. Not possible with gnuplot, but '' | ||
plot "< gunzip -c file.dat" | plot "< gunzip -c file.dat" | ||
+ | plot "< zcat file.dat" | ||
+ | plot "< tar -Oxjf file.dat.tar.bz2" | ||
</ | </ | ||
* or anything else with shell commands... | * or anything else with shell commands... | ||
- | * **sh** Passing arguments to a gnuplot script.< | + | |
+ | f(x) = a*x**2 + b*x + c | ||
+ | fit f(x) " | ||
+ | plot " | ||
+ | </ | ||
+ | |||
+ | | ||
+ | * (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 | #!/bin/sh | ||
- | file=$1 | + | ARGV1=$1 |
- | gnuplot <<\EOF | + | gnuplot << EOF |
- | plot "$file" using 1:(\$2*3) | + | plot "$ARGV1" using 1:(\$2*3) |
EOF | EOF | ||
- | </ | + | </ |
- | plot "`echo $GNUPLOT_PARAM1`" | + | ./plot.gp run.dat |
- | </code> | + | </ |
+ | #!/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<<EOF | ||
+ | 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 | ||
+ | </ | ||
Line 234: | Line 387: | ||
!epstopdf --outfile=plot.pdf plot.eps | !epstopdf --outfile=plot.pdf plot.eps | ||
quit | quit | ||
- | </ | + | </ |
- | plot [...] with lines linestyle | + | plot [...] with lines ls 1 lw 2 lt rgb "red" |
+ | </ | ||
+ | set grid lc rgb "grey" | ||
</ | </ | ||
Line 244: | Line 399: | ||
# your plotting code (can be included using the load command) | # 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 | quit | ||
</ | </ | ||
- | |||
* SVG:< | * SVG:< | ||
set terminal svg rounded size 450,360 | set terminal svg rounded size 450,360 | ||
Line 256: | Line 418: | ||
!gzip -S z plot.svg | !gzip -S z plot.svg | ||
quit | 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 | ||
</ | </ | ||