Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
software:gnuplot [2009/03/17 16:43] cyril |
software:gnuplot [2024/04/19 09:54] (current) cyril autotitle |
||
---|---|---|---|
Line 8: | Line 8: | ||
<code gnuplot> | <code gnuplot> | ||
- | load " | + | 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 ===== | ===== set ===== | ||
+ | * Window size.< | ||
+ | set term wxt 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 22: | Line 31: | ||
</ | </ | ||
- | * Axes tics | + | * Axes tics.< |
- | < | + | |
set ytics nomirror | set ytics nomirror | ||
- | set y2tics -3.5, | ||
set y2tics auto | set y2tics auto | ||
+ | set ytics 0.5 | ||
+ | set ytics -3.5, | ||
+ | set xtics (" | ||
</ | </ | ||
- | * Axes labels | + | * Axes tics labels.<code gnuplot> |
- | < | + | 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 xlabel "" | ||
set ylabel "" | set ylabel "" | ||
Line 37: | Line 61: | ||
</ | </ | ||
- | * Graph title | + | |
- | < | + | set logscale x 2 |
+ | set logscale y 10 | ||
+ | </ | ||
+ | |||
+ | | ||
set title "" | set title "" | ||
</ | </ | ||
+ | * Legend position.< | ||
+ | # available: left, right, top, bottom, outside, and below | ||
+ | set key left bottom | ||
+ | set key 100,100 | ||
+ | </ | ||
- | * Multiplot | + | |
- | < | + | 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 | ||
+ | </ | ||
+ | |||
+ | | ||
+ | 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 56: | Line 106: | ||
- | * New plot window | + | * New plot window.< |
- | < | + | |
plot <...> | plot <...> | ||
set term wxt 1 | set term wxt 1 | ||
Line 65: | Line 114: | ||
</ | </ | ||
- | * Clear window | + | |
- | < | + | 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 | 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 " | ||
</ | </ | ||
- | * Several curves in the same plot | + | |
- | < | + | plot " |
+ | </ | ||
+ | |||
+ | * **separator** Set the field separator:< | ||
+ | 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 102: | 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 114: | Line 188: | ||
</ | </ | ||
- | * Line type | + | |
- | < | + | |
# line | # line | ||
plot " | plot " | ||
+ | # line with custom style | ||
+ | plot " | ||
# line with custom line width | # line with custom line width | ||
plot " | plot " | ||
Line 124: | Line 199: | ||
# points of custom type | # points of custom type | ||
plot " | plot " | ||
+ | # lines AND points | ||
+ | plot " | ||
+ | # labels in addition to points | ||
+ | plot " | ||
+ | |||
# to find out point types and line width available | # to find out point types and line width available | ||
test | test | ||
</ | </ | ||
- | * Arithmetics on columns | + | |
- | < | + | |
plot " | plot " | ||
# where $n means column n | # where $n means column n | ||
</ | </ | ||
- | * Using cumulated values of a column | + | |
- | < | + | plot i=1, " |
+ | </ | ||
+ | |||
+ | * **fun()=** Define variables and functions, use predefined functions ('' | ||
+ | theta=7.72 * (2*pi/ | ||
+ | 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, | ||
+ | </ | ||
+ | | ||
+ | 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)/ | ||
</ | </ | ||
- | * Selecting lines | + | * **paste** Plotting one curve with values from different files. Not possible with gnuplot, but '' |
- | < | + | plot "< |
- | plot "< | + | |
</ | </ | ||
- | ===== Other possibilities | + | * **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; | ||
+ | </ | ||
+ | # | ||
+ | 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 " | ||
+ | </ | ||
+ | # | ||
+ | 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 | ||
+ | </ | ||
- | * other outputs, eg to generate eps files |