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/01/18 04:08] 127.0.0.1 external edit | software:gnuplot [2024/04/19 09:54] (current) cyril autotitle | ||
|---|---|---|---|
| Line 12: | Line 12: | ||
| You can also directly load it when starting gnuplot, in shell: | You can also directly load it when starting gnuplot, in shell: | ||
| - | < | + | < | 
| gnuplot -persist plot_data.gp | gnuplot -persist plot_data.gp | ||
| </ | </ | ||
| Line 18: | Line 18: | ||
| ===== set ===== | ===== set ===== | ||
| - | * Window size | + | * Window size.< | 
| - | < | + | |
| set term wxt size 1024,768 | set term wxt size 1024,768 | ||
| set term x11 size 1024,768 | set term x11 size 1024,768 | ||
| </ | </ | ||
| - | * Axes ranges | + | * Axes ranges.< | 
| - | < | + | |
| set xrange [0:1000] | set xrange [0:1000] | ||
| set yrange [-100:100] | set yrange [-100:100] | ||
| Line 33: | Line 31: | ||
| </ | </ | ||
| - | * 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 (" | ||
| </ | </ | ||
| - | * Axes tics labels | + | * Axes tics labels.< | 
| - | < | + | |
| set format x " | set format x " | ||
| set format y " | set format y " | ||
| + | </ | ||
| + | set format x "" | ||
| + | </ | ||
| + | set ydata time | ||
| + | set timefmt " | ||
| + | set format y " | ||
| </ | </ | ||
| - | * Grid | + | * Grid.< | 
| - | < | + | |
| set grid | set grid | ||
| </ | </ | ||
| - | * Axes labels | + | * Axes labels.< | 
| - | < | + | |
| set xlabel "" | set xlabel "" | ||
| set ylabel "" | set ylabel "" | ||
| Line 60: | Line 61: | ||
| </ | </ | ||
| - | * Graph title | + |  | 
| - | < | + | set logscale x 2 | 
| + | set logscale y 10 | ||
| + | </ | ||
| + | |||
| + |  | ||
| set title "" | set title "" | ||
| </ | </ | ||
| - | * Legend position | + | * Legend position.< | 
| - | < | + | |
| # available: left, right, top, bottom, outside, and below | # available: left, right, top, bottom, outside, and below | ||
| set key left bottom | set key left bottom | ||
| Line 72: | Line 76: | ||
| </ | </ | ||
| - | * Line options | + |  | 
| - | < | + | set key autotitle columnheader | 
| + | </ | ||
| + | |||
| + |  | ||
| set pointsize 0.33333333 | set pointsize 0.33333333 | ||
| </ | </ | ||
| - | * Multiplot | + |  | 
| - | < | + | set size ratio -1 # for plot | 
| + | set view equal xyz # for splot | ||
| + | </ | ||
| + | |||
| + |  | ||
| + | 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 85: | 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 97: | Line 106: | ||
| - | * New plot window | + | * New plot window.< | 
| - | < | + | |
| plot <...> | plot <...> | ||
| set term wxt 1 | set term wxt 1 | ||
| Line 106: | Line 114: | ||
| </ | </ | ||
| - | * Data separator (space by default) | + | * Data separator (space by default).< | 
| - | < | + | |
| set datafile separator "," | set datafile separator "," | ||
| </ | </ | ||
| - | * Clear window | + |  | 
| - | < | + | set style data histogram | 
| + | set style histogram errorbars gap 0 lw 2 | ||
| + | set style fill solid border -1 | ||
| + | </ | ||
| + | |||
| + |  | ||
| clear | clear | ||
| </ | </ | ||
| ===== 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 " | plot " | ||
| </ | </ | ||
| - | * Specifying columns to use. If only one is specified, entry number (line number) is used for x. | + |  | 
| - | < | + | |
| plot " | plot " | ||
| plot " | plot " | ||
| </ | </ | ||
| - | * Specifying lines to use (every 2 lines from line 50 to line 1050) | + |  | 
| - | < | + | |
| plot " | plot " | ||
| </ | </ | ||
| - | * Several curves in the same plot | + |  | 
| - | < | + | set datafile separator "," | 
| + | </ | ||
| + | |||
| + | * **,** Several curves in the same plot.< | ||
| plot " | plot " | ||
| + | </ | ||
| + | plot \ | ||
| + | " | ||
| + | " | ||
| </ | </ | ||
| - | * Functions titles | + |  | 
| - | < | + | plot " | 
| - | plot " | + | </ | 
| + | * Use a variable in title.< | ||
| + | a=2 | ||
| + | plot " | ||
| </ | </ | ||
| - | * Colors | + |  | 
| - | < | + | |
| show palette colornames | show palette colornames | ||
| plot " | plot " | ||
| Line 153: | Line 174: | ||
| </ | </ | ||
| - | * Axes : x1y1 (bottom-left), x2y2 (top-right), x1y2, x2y1 | + | * **palette** Colors as value.< | 
| - | < | + | set palette defined | 
| - | plot " | + | plot " | 
| </ | </ | ||
| - | If you want to display different tics on y2 axis, do before the plot command: | + | |
| - | < | + | * **axes** Axes : x1y1 (bottom-left), | 
| + | plot " | ||
| + | </ | ||
| set ytics nomirror | set ytics nomirror | ||
| set y2tics -3.5, | set y2tics -3.5, | ||
| Line 165: | Line 188: | ||
| </ | </ | ||
| - | * Line type | + |  | 
| - | < | + | |
| # line | # line | ||
| plot " | plot " | ||
| Line 186: | Line 208: | ||
| </ | </ | ||
| - | * Arithmetics on columns | + |  | 
| - | < | + | |
| plot " | plot " | ||
| # where $n means column n | # where $n means column n | ||
| </ | </ | ||
| - | You can use very complicated formulas, define functions and do computations before the plot command. For instance to rotate and translate a trajectory before to display it: | + | * **(column())** Arithmetics on columns indices.< | 
| - | < | + | plot i=1, "data1.dat" using 1:(column(i+1)) | 
| + | </ | ||
| + | |||
| + | * **fun()=** Define variables and functions, use predefined functions ('' | ||
| theta=7.72 * (2*pi/360) | theta=7.72 * (2*pi/360) | ||
| dx=6.7866 | dx=6.7866 | ||
| Line 203: | Line 227: | ||
| </ | </ | ||
| - | * Using cumulated values of a column | + |  | 
| - | < | + | 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, | ||
| + | </ | ||
| + |  | ||
| + | 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 ' | ||
| - | # sum of column | + | 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)/ | ||
| </ | </ | ||
| - | * Plotting one curve with values from different files | + |  | 
| - | < | + | |
| plot "< paste file1.dat file2.dat" | plot "< paste file1.dat file2.dat" | ||
| </ | </ | ||
| - | * Using directly a data file compressed with gzip | + | * **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... | ||
| + | |||
| + | * **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 | ||
| </ | </ | ||
| - | etc with shell commands... | ||
| ===== File outputs ===== | ===== File outputs ===== | ||
| Line 225: | 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:< | + | * EPS and PDF:< | 
| set terminal postscript eps color " | set terminal postscript eps color " | ||
| set output ' | set output ' | ||
| - | # 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 | ||
| - | </ | + | </ | 
| - | plot [...] with lines linestyle | + | plot [...] with lines ls 1 lw 2 lt rgb "red" | 
| + | </ | ||
| + | set grid lc rgb "grey" | ||
| </ | </ | ||
| - | * PNG:< | + | * PNG:< | 
| set terminal png size 450,360 small | set terminal png size 450,360 small | ||
| set output ' | set output ' | ||
| - | # you 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 | ||
| </ | </ | ||
| - | + |  | |
| - |  | + | |
| set terminal svg rounded size 450,360 | set terminal svg rounded size 450,360 | ||
| set output ' | set output ' | ||
| - | # 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 | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | * 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 | ||
| </ | </ | ||




