Gnuplot
Basics
set datafile commentschars '#' set datafile missing '#' set datafile separator "\t"
Simple Plot
data = 'data.dat' plot data #or plot sin(x)
Plot multiple lines
plot\ "1.dat" title "Eins"\ ,"2.dat" title "Zwei"\ ,"3.dat" title "Drei"\
png output
set terminal png giant size 1024,768 set output "plot.png" plot sin(x) unset output
Multiplot / 2 stacked plots
set multiplot layout 2,1 columnsfirst plot sin(x) plot cos(x) unset multiplot
Plot different columns of same file using 2 y-axes
data = 'data.dat' plot\ data using 1:3 title "Drei" axes x1y2 \ ,data using 1:2 title "Zwei"
better:
plot\ data using (column("X-Values")):(column("Drei")) title "Drei" axes x1y2 \ ,data using (column("X-Values")):(column("Zwei")) title "Zwei"
Common Mistakes / Errors
Integer division:
use 10.0 / 3 instead of 10/3
Plot Region of Function
plot \ (x<=0.0)||(x>=0.5)?1/0:sin(x) \ , (x<=0.5)||(x>=1.0)?1/0:cos(x)
Filter Data to Plot
plot data \ using (column("Date")):(column("Cases_Last_Week_Doubling_Time")>0 ? column("Cases_Last_Week_Doubling_Time") : 1/0) \ title "Verdopplungszeit" axis x1y2 with lines ls 5 linecolor rgb "sea-green"
String Functions
get file base name of data file and add .png for outfile
data = "myDatafile.dat" set output data[0:strlen(data)-4] . ".png"
Layout
Terminals
set terminal postscript enhanced eps color solid # solid!!! set terminal png enhanced giant size 1024,768
For PostScript and MP you should
set border back
as otherwise the frame is drawn twice (can be found using inkscape)
My Favorite: Terminal MP (Metapost) (needs Latex and UbuntuPackage texlive-metapost)
#term = "png" term = "mp" if (term eq "png" ) set terminal png enhanced giant size 1024,768 if (term eq "mp" ) set terminal mp color solid latex psnfss prologues 2 magnification 1.00 outfile="plot123" set output outfile . "." . term plot sin(x) unset output if (term eq "mp" ) \ system ("mpost -tex=latex '".outfile.".mp'") ; system ("mv " . outfile . ".0 " . outfile .".mps") \ ; system ("convert -density 300 ".outfile.".mps ".outfile.".png") \ ; system ("rm -f ".outfile.".mpx") ; system ("rm -f ".outfile.".log") ; system ("rm -f ".outfile.".mp")
General
set title "Plot Title" # set default plot style set style function points # define linestyles set style line 1 lt 1 lw 2 # Legend position set key top right # or set key off # advanced: set key bottom left box width -3 reverse Left samplen 2 #invert title "Sinus" # default: set key on right top vertical Right noreverse noinvert samplen 4 spacing 1.25 title "" nobox
Axes
set xlabel "Time (s)" set ylabel "Current (A)" set grid xtics ytics # set tics set xtics mirror set ytics mirror # add some tics set xtics add (0.003, 0.03, 0.3) set xtics add ("Pi" 3.14159) # Disable autorange of axes set xrange [1:*] set yrange [2.8E-08:3.0E-08] set logscale y # set format of number to 1e-03 etc set format y "%1.e" # 10^x set format y "10^{%L}" # "%" sign: set format y "%g%%" # % sign in Terminal MP (Latex) set format y "%g".'\%%' # NullAchsen einfügen set zeroaxis # or set xzeroaxis ls -1 lw 2
Date/Time Axis
set timefmt '%Y-%m-%d' # %d.%m.%Y %H:%M set format x '%d.%m' set xdata time set xrange ["2020-03-01":]
using offset in time axis
plot data u (column("Date")):(column("Deaths_Last_Week_Per_Million")) t "Tote" axes x1y2 with lines ls 2, \ data u (timecolumn(2)-14*24*3600):(column("Deaths_Last_Week_Per_Million")) t "Tote verschoben um 14 Tage" axes x1y2 with lines ls 3 unset output
Linked axes
Variant 1 Linked tics on top axes e.g. bottom=nanometer/top=eV [1]
set x2label "Temp (C)" set x2tics (\ "25" 1000/(c0C+25) \ , "50" 1000/(c0C+50) \ , "75" 1000/(c0C+75) \ , "100" 1000/(c0C+100) \ , "125" 1000/(c0C+125) \ )
Important: you have to set plot range for x and x2!!!
plot [xmin:xmax] [*:*] [xmin:xmax]
Links: [2]
Variant 2: easier
plot sin(x) set y2range[0:GPVAL_Y_MAX/1.60934] # km -> mile set y2tics replot
Add Text to Plot
set label "text" at 3.3e-3,90e-3 plot ... set label 1 sprintf("Ea = %3.5g",-b*ckb/ce) at 3.3e-3,90e-3 plot ... unset label 1 # position can be given via differnet systems: first (=x axes), second (=x2 axes), graph (=pos in graph area 0.0..1.0) , screen, character # see help set coordinates set label "text" at graph 0.25, graph 0.75
Colors
set color of axes label or tics to that of line XYZ
set ylabel "Label" textcolor lt 1 set ytics textcolor lt 1
Draw Line/Arrow
set arrow nohead from 0,0 rto 10,1 set arrow from fitLow, graph 0 to fitLow, graph 1 nohead lc -1 lw 2 plot ... unset arrow
Note: set arrow must be placed before the plot command
line at y=0 in date plot
set arrow nohead from graph 0, first 0 to graph 1, first 0
Draw circle, rectangle, ellipse, or polygon
set object 1 circle at xwert , ywert behind
Adjusting Margins/Distances
Axis label closer to tics (unit = characters, other units possible as well)
set xlabel offset 0.0,0.5 set ylabel offset 1.5,0.0 set x2label offset 0.0,-0.5 set y2label offset -1.5,0.0
margins: (l,r,t,bmargin)
set lmargin 3 # unit chars set lmargin at screen 0.1 # useful for multiplots
Fonts
in Linux run
export GDFONTPATH=/usr/share/fonts/truetype/msttcorefonts
than you can use all the fonts in that dir. For example
set terminal png giant font "Arial,24"
Latex Fonts
From [3], needs Linux package "cm-super")
set term postscript fontfile '/usr/share/texmf/fonts/type1/public/cm-super/sfrm1000.pfb' "SFRM1000"
or after copying the file
set term postscript fontfile '/MessSoftware/sfrm1000.pfb' "SFRM1000"
Greek Symbols in Latex
To get a greek letter, you have to use the postscript terminal, with the option enhanced. Then, to add the alpha greek letter, you have to type {/Symbol a}. Analogously for other letters, changing the "a" by other, like "D" for Delta and so on... [4]
Modify Default Line Types/Styles
set style increment user # important!!! switch between linetypes (default) and userdefined linestyles set style line 1 linewidth 2 pointsize 2 linecolor rgb "blue" set style line 2 linewidth 2 pointsize 2 linetype 7 set style line 3 linewidth 2 pointsize 2
Fitting
b=5 f(x) = x**b fit f(x) "data.dat" using 1:2 via b plot "data.dat" title "data" notitle, f(x) title "fit" # to weight the data (here by y^(-2)) use sth like the following: # fit f(x) "data.dat" using 1:2:(1./$2**2) via b f05(x)=a05*exp(b05*x)+0 fit [0.003:0.006] f05(x) "V6_05.dat" using (1./(($3/2+$4/2)+273.15)):($2*5380716) via a05,b05 a=1; b=1 f(x)=a*exp(b*x) fitLow =0.0030 fitHigh=0.0055 fit [fitLow:fitHigh] f(x) "data.dat" via a,b print a,b set output "png/fit.png" plot \ "data.dat" title "5V" with points pointsize 0.2\ , (x<=fitLow)||(x>=fitHigh)?1/0:f(x) notitle with lines \ unset output
Interpolate
smooth csplines
Advanced Fitting
FIT_LIMIT = 1e-8 # or even smaller
f(x) = a *exp(-(x-b )*(x-b )/c /c ) g(x) = A(a)*exp(-(x-B(b))*(x-B(b))/C(c)/C(c)) # trick: use atan as restriction function A(x) = (2.0-1.1)/pi*(atan(x)+pi/2)+1.1 # Restrict a to the range of [1.1:2.0] B(x) = (0.9-0.1)/pi*(atan(x)+pi/2)+0.1 # Restrict b to the range of [0.1:0.9] C(x) = (1.5-0.5)/pi*(atan(x)+pi/2)+0.5 # Restrict c to the range of [0.5:1.5] a =0.0 ; b =0.5 ; c =0.9 ; fit f(x) 'data.dat' via a, b, c aa=1.5 ; bb=0.5 ; cc=0.9 ; fit g(x) 'data.dat' via aa, bb, cc plot "data.dat" , f(x) , g(x) print A(aa), B(bb), C(cc) # do NOT use aa, bb, cc as they are not what you want!!!
Stats
In GP Ver>4.6 the new function stats is quite convenient.
stats data STATS_records # total number of in-range data records STATS_min # minimum value of in-range data points STATS_max # maximum value of in-range data points STATS_mean # ... STATS_median STATS_stddev # standard deviation STATS_pos_min_y # x coordinate of a point with minimum y value STATS_pos_max_y # x coordinate of a point with maximum y value
Finding Min and Max for X and Y
In GP Ver 4.2 (and above) these tricks, stolen from [5] are handy
data = 'stats2.dat' # Retrieve statistical properties via dummy plot plot data u 1:2 min_y = GPVAL_DATA_Y_MIN max_y = GPVAL_DATA_Y_MAX min_x = GPVAL_DATA_X_MIN max_x = GPVAL_DATA_X_MAX # X Pos of min_y and max_y plot data u ($2 == min_y ? $2 : 1/0):1 min_y_x_pos = GPVAL_DATA_Y_MIN plot data u ($2 == max_y ? $2 : 1/0):1 max_y_x_pos = GPVAL_DATA_Y_MAX # Mean via fit + Std Deviation f(x) = mean_y fit f(x) data u 1:2 via mean_y stddev_y = sqrt(FIT_WSSR / (FIT_NDF + 1 ))
Histogram
data="data.dat" stats data # retrieve STATS_ variables ... rows = STATS_records # num values mu = STATS_mean_y sigma = STATS_stddev_y # standard deviation # histogram parameters width=0.5 # box width (units of x!) max = 0.0+ceil(mu+3*sigma) # plot range max min = 0.0+floor(mu-3*sigma) # plot range min # gaussean function gauss(x) = 1. / (sigma * sqrt(2 * pi)) * exp(-(x - mu)**2 / (2 * sigma**2)) # histogram function hist(x) = width * floor(x / width) + width / 2.0 # some settings set key off set tics out set xrange [min:max] ; set x2range [min:max] set yrange [0:] set y2range [0:] set grid x2tics ytics # set xtics min, (max - min)/4.0, max set x2tics ("-3s" mu-3*sigma, "-2s" mu-2*sigma, "-1s" mu-1*sigma, "µ" mu, "1s" mu+1*sigma, "2s" mu+2*sigma, "3s" mu+3*sigma) set boxwidth width set style fill solid 0.5 set tics out nomirror set xlabel "Value" set ylabel "rel. Density" set format x "%g" set format y "%g %%" plot \ data using (hist($1)):(100.0 / rows) smooth freq w boxes lc rgb"red" notitle \ , gauss(x) * sigma * sqrt(2*pi) axes x1y2 notitle lc rgb"blue" lw 2 unset output
Historam with logscale
GnuPlot 5.0 has a bug combining logscale and smooth frequency, workaround:
unset logscale y set table 'hist_ temp.dat' plot \ data using (hist($2)):(1.0) smooth frequency unset table plot 'hist_temp.dat' using 1:2
improved version, after finding a second bug, and using memory instead of file for table
unset logscale y set table $histtabledata #'hist_temp.dat' plot \ data using (hist($2)):(1.0) smooth frequency w points unset table plot $histtabledata using 1:($2 == 0 || strcol(3) eq "u" ? 1/0 : $2) w boxes lc rgb"red" notitle # 1:0 -> $2 == 0 || strcol(3) eq "u" ? 1/0 : $2
Misc
Bar/Box Chart Plot
set style fill solid 0.5 border 0 set boxwidth 0.75 relative plot data u 3:xticlabels(1) with boxes # y = data column 3 # x label = data column 1
Stacked Boxes via histogram rowstacked
# settings for boxes set style data histograms set style histogram rowstacked set boxwidth 1 relative set style fill solid 1.0 border -1 plot data using 3:xticlabels(1)
Errorbars
plot data u ($1):($5):(0.1*$1):(0.1*$5) w xyerrorbars ps 0 plot data u ($1):($5):(0.1*$1) w xerrorbars plot data u ($1):($5):(0.1*$5) w yerrorbars
Dashed lines
In terminal mp (which I prefere) use terminal option dashed and redefine the linestyles using lt 1 (solid) as template
set style line 1 lt 1 linecolor rgb "red" set style line 2 lt 1 linecolor rgb "blue" set style line 3 lt 1 linecolor rgb "magenta"
Furthermore define how your dashed (e.g. lt 6 as template) line shall look like and define e.g. ls 30 to it
set style line 30 lt 6 lw 1 linecolor rgb "dark-green"
Now you can plot data and line by
plot data u 1:5 ls 2 set arrow from 0.55, graph 0 to 0.55, graph 1 nohead ls 30 back
Number or points to plot for analytic function
set samples 200 # default = 100
pause
you can use
pause mouse
to let gnuplot stop continuing until you click the mouse
extract a "cell" from data file
# fetch data_file from last row, 2nd column of data (cut assumes tab separating by default) value = system("tail -1 " . data_file . " | cut -f2)
Find number of data points
Better use Gnuplot way, see Stats above
Version 1: count lines
set table "temptable.dat" plot [fitLow0:fitHigh0] data using 1:3 unset table pointsInRange0 = 0.0 + system (sprintf("grep -P ' i' temptable.dat | wc -l")) system ("rm temptable.dat") ; system ("del temptable.dat")
Version 2: count lines where first value is in range min .. max
numPointsInRange (datafile, min, max, col) = 0 + system ("perl -e '\ $c = 0; \ while ( <> ) { \ $x = ( split /\t/ )[ 0 ]; \ $y = ( split /\t/ )[ ".sprintf("%d",col - 1)."]; \ if ( $x =~ m/^[0-9\.]+$/ \ and $y =~ m/^[0-9\.]+$/ \ and $y > 0 \ and $x >= ".sprintf("%.1f", min)." \ and $x <= ".sprintf("%.1f", max)." ) \ {$c++;} \ } print $c; \ ' < ". datafile) print numPointsInRange (data, date_min, fitHigh0, 12)
Find min / max value
Better use Gnuplot way, see Stats above
using Perl
max=`! perl -e '$max=-1e38; while (<>) {@t=split; $max=$t[1] if $t[1]>$max}; print $max' < 22.5.dat` min=`! perl -e '$min=+1e38; while (<>) {@t=split; $min=$t[1] if $t[1]<$min}; print $min' < 22.5.dat` print min,max
Version 2
s = '$col=2; $f='."'".data."'".'; use warnings; \ open (FH, $f); @a = grep {not m/^#/ and not m/^\s*$/ } <FH>; close FH; \ @t=split /\t/ , shift @a ; $x=$t[$col-1]; print $x ' first=0.0+system("perl -e \"".s."\"") print first s = '$col=2; $f='."'".data."'".'; use warnings; \ open (FH, $f); @a = grep {not m/^#/ and not m/^\s*$/ } <FH>; close FH; \ @t=split /\t/ , pop @a ; $x=$t[$col-1]; print $x ' last=0.0+system("perl -e \"".s."\"") print last s = '$col=2; $f='."'".data."'".'; use warnings; $x=1e38;\ open (FH, $f); @a = grep {not m/^#/ and not m/^\s*$/ } <FH>; close FH; \ while ($l=shift @a) { @t=split /\t/,$l ; next if $#t<$col-1 ; $x=$t[$col-1] if $t[$col-1]<$x;} ; print $x ' min=0.0+system("perl -e \"".s."\"") print min s = '$col=2; $f='."'".data."'".'; use warnings; $x=-1e38;\ open (FH, $f); @a = grep {not m/^#/ and not m/^\s*$/ } <FH>; close FH; \ while ($l=shift @a) { @t=split /\t/,$l ; next if $#t<$col-1 ; $x=$t[$col-1] if $t[$col-1]>$x;} ; print $x ' max=0.0+system("perl -e \"".s."\"") print max
Version 2a - Linux ( excape $)
s = '\$col=2; \$f='."'".d."'".'; use warnings; \ open (FH, \$f); @a = grep {not m/^#/ and not m/^\s*$/ } <FH>; close FH; \ @t=split /\t/ , shift @a ; \$x=\$t[\$col-1]; print \$x '
Version 3 (when using Perl to gen Gnuplot script)
my $colX=3; my $colY=2; open (FH, $f); my @a = grep {not m/^#/ and not m/^\s*$/} <FH>; close FH; my @b=split /\t/ , pop @a ; my $lastX=$b[$colX-1]; my $lastY=$b[$colY-1]; $cont .= "set object 1 circle at $lastX , ($lastY*1E9) behind\n";
Values of Tics in Logscale
of only 3 minor tics are visible they are at 0.2 ; 0.5 ; 0.8
Variables
a=123 print "Variable a=",a undef a
Export (fit-)Variables
a=12 # not so good... system(sprintf("echo A=%d >testausgabe.txt",a)) set print "../data/cases-de-gnuplot-fit.csv" append print a unset print
Schnippsel
c0C = 273.15 ckb = 1.38e-23 ce = 1.602e-19 ckbeV = ckb/ce
set x2tics (\ "25" (c0C+25) \ , "50" (c0C+50) \ , "75" (c0C+75) \ , "100" (c0C+100) \ , "125" (c0C+125) \ )
WARNING: you have to set x2 AND x ranges to the same! (so no autorange :-()
Multiplot V3 : 2x2
#!/usr/bin/gnuplot term="eps" color="sw" color="color" outtype="png-trans" outtype="png-solid" outtype="pdf" if (color eq "sw") set terminal postscript monochrome if (color eq "color") set terminal postscript color set terminal postscript eps enhanced clip dashed size 15cm,15cm font ",22pt" #fontfile add "uopr8a.pfa" fontfile add "uopri8a.pfa" fontfile add "uopb8a.pfa" font "URWClassico-Reg,22pt" # 10x7 # fontsize 14 = default set encoding utf8 set border back set style data points set style function lines set style increment user # LineWitdh + Symbols set style line 1 lw 4 ps 3 pointtype 8 #= dreieck oben set style line 2 lw 4 ps 3 pointtype 10 #= dreieck unten if (color eq "sw") set style line 1 lt 1 # voll if (color eq "sw") set style line 2 lt 5 # Strich-Punkt if (color eq "color") set style line 1 lt 1 linecolor rgb "red" if (color eq "color") set style line 2 lt 1 linecolor rgb "blue" # grid set style line 40 lt 1 lw 2 linecolor rgb "grey70" set grid ls 40 set ytics .4 set y2tics .4 set yrange [-1.2:+1.4] set y2range [-1.2:+1.4] set mytics 4 # minor ytics set my2tics 4 set samples 300 # text will be inserted later on set label 2 "" left front font ",20" at graph 0.05, graph 0.90 marL=0.12 marT=0.04 plotX=(1.0-marL-0.005)/2 plotY=plotX/1.618 # Golden Ratio = a:b = 1.618 set key box height 0.1 width -2 reverse noinvert Left opaque samplen 1 font ",18" set key t l at screen marL + 0.85*plotX, screen 1.0-marT-1.25*plotY set key off set output "plot" . ".gp" . term set multiplot # (a) set label 2 "(a) n=1" set lmargin at screen marL set rmargin at screen marL + 1*plotX set tmargin at screen 1.0-marT set bmargin at screen 1.0-marT-1*plotY unset xlabel; set xtics format "" unset ylabel; set ytics format "" unset x2label; set x2tics format "" unset y2label; set y2tics format "" set ytics format "%g" # y Tics on set key on plot sin(x) t "sin(n x)", cos(x) t "cos(n x)" # (b) set label 2 "(b) n=2" set lmargin at screen marL + 1*plotX set rmargin at screen marL + 2*plotX set tmargin at screen 1.0-marT set bmargin at screen 1.0-marT-plotY unset xlabel; set xtics format "" unset ylabel; set ytics format "" unset x2label; set x2tics format "" unset y2label; set y2tics format "" set key off plot sin(2*x), cos(2*x) # (c) set label 2 "(c) n=3" set lmargin at screen marL set rmargin at screen marL + 1*plotX set tmargin at screen 1.0-marT-1*plotY set bmargin at screen 1.0-marT-2*plotY unset xlabel; set xtics format "" unset ylabel; set ytics format "" unset x2label; set x2tics format "" unset y2label; set y2tics format "" set ytics add ("" 1.2) # prevent overwriting of label of (a) # Axis Labels set xlabel "X-LABEL" offset 13.5 , 0 set ylabel "Y-LABEL" offset 0 , 5 set xtics format "%g" # x Tics on set ytics format "%g" # y Tics on plot sin(3*x), cos(3*x) unset xlabel ; unset ylabel # (d) set label 2 "(d) n=4" set lmargin at screen marL + 1*plotX set rmargin at screen marL + 2*plotX set tmargin at screen 1.0-marT-1*plotY set bmargin at screen 1.0-marT-2*plotY unset xlabel; set xtics format "" unset ylabel; set ytics format "" unset x2label; set x2tics format "" unset y2label; set y2tics format "" set xtics format "%g" # x Tics on set xtics add ("" -10) # prevent overwriting of label of (c) set key on plot sin(4*x) t "sin(n x)", cos(4*x) t "cos(n x)" unset multiplot unset output # trim eps s = 'epstool --copy --quiet --bbox "plot.gpeps" "plot.eps"' ; system (s) ; system "rm plot.gpeps" # eps -> pdf if (outtype eq "pdf") s = 'epstopdf --embed "plot.eps"' ; system (s) #embed=embed fonts # eps -> png if (outtype eq "png-solid") s = 'convert -density 150 -trim +repage "plot.gpeps" -background white -flatten +matte -type Palette "plot.png"' ; system (s) if (outtype eq "png-trans") s = 'convert -density 150 -trim +repage "plot.gpeps" -transparent white "plot.png"' ; system (s) # remove eps system "rm plot.eps"
Multiplot using macros and Xmargin at screen
#!/usr/bin/gnuplot # Demonstration of a common use scenario of the multiplot environment. AUTHOR: Hagen Wierstorf # http://www.gnuplotting.org/multiplot-placing-graphs-next-to-each-other/ # Functions (1/0 means not defined) a = 0.9 f(x) = abs(x)<2*pi ? a*sin(x) : 1/0 g(x) = abs(x)<2*pi ? a*sin(x+pi/2) : 1/0 h(x) = abs(x)<2*pi ? a*sin(x+pi) : 1/0 k(x) = abs(x)<2*pi ? a*sin(x+3.0/2*pi) : 1/0 unset key #set tics scale 0.5 set ytics 1 set xrange [-2.5*pi:2.5*pi] set yrange [-1.5:1.5] # MACROS set macros # Enable the use of macros # x- and ytics for each row resp. column NOXTICS = "set xtics ( -2*pi, -pi, 0, pi, 2*pi); \ unset xlabel" XTICS = "set xtics ('-2Pi' -2*pi, '-Pi' -pi,'0' 0, 'Pi' pi, '2Pi' 2*pi);\ set xlabel 'x'" NOYTICS = "set format y ; unset ylabel" YTICS = "set format y '%.0f'; set ylabel 'y'" # Margins for each row resp. column TMARGIN = "set tmargin at screen 0.98; set bmargin at screen 0.58" BMARGIN = "set tmargin at screen 0.58; set bmargin at screen 0.18" LMARGIN = "set lmargin at screen 0.13; set rmargin at screen 0.56" RMARGIN = "set lmargin at screen 0.56; set rmargin at screen 0.99" # Placement of the a,b,c,d labels in the graphs POS = "at graph 0.92,0.9 font ',18'" ### Start multiplot (2x2 layout) set multiplot layout 2,2 rowsfirst # --- GRAPH a @TMARGIN; @LMARGIN @NOXTICS; @YTICS set label 1 'a' @POS plot f(x) with lines ls 1 # --- GRAPH b @TMARGIN; @RMARGIN @NOXTICS; @NOYTICS set label 1 'b' @POS plot g(x) with lines ls 1 # --- GRAPH c @BMARGIN; @LMARGIN @XTICS; @YTICS set label 1 'c' @POS plot h(x) with lines ls 1 # --- GRAPH d @BMARGIN; @RMARGIN @XTICS; @NOYTICS set label 1 'd' @POS plot k(x) with lines ls 1 unset multiplot
Insets
# plot 2(inset) set size 0.2,0.4 set origin 0.2,0.5 clear # !!! clears the plot area from grid lines etc # draw frame set object 1 rect from screen 0.2, screen 0.5 to screen 0.5, screen 0.9 back behind set object 1 rect fc rgb "white" fillstyle solid 1.0 set lmargin 6; set bmargin 1.5; set rmargin 0;set tmargin 0 unset key unset xlabel ; unset ylabel ; unset y2label
My Plot Template
allowing to choose between Terminal PNG or MetaPost
Main file plot.gp
#!/usr/bin/gnuplot load "/MessSoftware/A_gnuplot-header.gp" term = "png" term = "mp" plotfolder = "png_XYZ" data1 = "data/Eins.dat" data2 = "data/Zwei.dat" set style data linespoints set style increment user set style line 1 linetype 1 lw 2 ps 2 set style line 2 linetype 2 lw 2 ps 2 if (term eq "mp" ) set terminal mp color solid latex psnfss prologues 2 if (term eq "png" ) set terminal png enhanced giant size 1024,768 data = data1 load "plot_c_vs_d1.gp" # in here is the plot function data = data2 load "plot_c_vs_d1.gp" # again... if (term eq "mp" ) s = "perl convert.pl " . plotfolder ; system (s)
plot file plot1.gp, executed for each data set
f(x) = m*x+b fit f(x) data using (log($1)):(log($2)) via m,b fe(x) = x**m * exp(b) set label 1 sprintf("m=%.2f",m) at graph 0.05, graph 0.75 set output plotfolder . "/" . "." . term plot \ data u ($1):($2) notitle \ , fe(x) title "fit" w lines lw 1 unset output ; unset label 1
perl script convert.pl for converting .mp to png and .mps
#!/usr/bin/perl -w use warnings;use strict; my $del_mps = 1; my $dpi = 222.841; # -> 28cm in ooimpress my $plotdir = "png"; $plotdir = shift if @ARGV; chdir $plotdir or die $!; my @a = <*.mp>; foreach my $f (@a) { print "-->$f\n"; $_ = $f; $f =~ s/ /_/g; `mv "$_" "$f"` unless ($_ eq $f); my $fname = $f; $fname =~ s/\.[^\.]*?$//; # remove ext print system("mpost -tex=latex '$fname.mp'"); print "\n"; my @b = grep {m/\.[\d]+$/} grep {-f $_} <$fname.*>; $f = $b[0]; print `mv "$f" "$fname.mps"`; print `convert -density $dpi "$fname.mps" "$fname.png"`; print `rm "$fname.mps"` if $del_mps; unlink ( split " ", "$fname.eps $fname.mp $fname.mpx $fname.log"); print "\n"; } $_ = "mpxerr.tex"; unlink $_ if -f $_;
Perl: plotAll.pl
Perl script for generating a plotAll.gp file for all files matching */*.log. In plotAll.gp a file named plot1.gp is loaded for each file, which contains the gnuplot commands.
use strict; use warnings; #use v5.10; # say #use Data::Dumper; #my $pathToGnuplot = '"C:\Program Files (x86)\gnuplot\bin\gnuplot.exe"'; my $gpoutfile = "plotAll.gp"; my $gpplot1 = "plot1.gp"; my @Files = <*/*.log>; #print Dumper @Files; open (my $fh, ">", $gpoutfile) or die $!; print $fh "print \"Gnuplot Start\"\n"; foreach my $file (@Files){ print $fh "data = \"$file\" ; load \"$gpplot1\" \n"; } close $fh; #$_ = "$pathToGnuplot $gpoutfile"; $_ = "$gpoutfile"; system($_); unlink $gpoutfile;
Filled Curves
set style fill solid 1.0 noborder plot [:] [*:*] \ ,f1(x) with filledcurve y1=0 lc rgb "gray80" \ f2(x) with filledcurve y1=0 lc rgb "gray60" \ ,f1(x) ls 2 \ ,f2(x) ls 3 # first the areas, than the lines in the right linecolor
Filled Area between 2 Curves
f1(x) = sin(x) f2(x) = x+1 set style fill solid 0.4 border plot \ '+' using 1:(f1($1)):(f2($1)) with filledcurve # '+' allows treatment of functions as data
Color-map of 3d data
set palette defined (0 "white", 1 "blue", 2000 "red") set logscale cb set view map splot data u 2:1:3 with image