# Gnuplot-Hacks-2.0-Script # Best practices[tm] with gnuplot # Hagen Paul Pfeifer - # Location of this script: ####################################################### # # README # # o Process this file via 'gnuplot gnuplot-hacks.gpi' # # o Dependencies of this script are gnuplot version >= 4.2 # and ruby to generate some input data on the fly # # o At the end of this document you will find some # helpfully tips # # o If you want to increase the collected knowledge: # send the new version and I will upload it ####################################################### # # DATA GENERATION # # Generate three columns - x, e(x) and a f(3), # in each case with a little bit random noise ! ruby -e 'File.open("/tmp/gnuplot.dat", "w") do |f| 500.times do |i| f.puts "#{i} #{Math::log(i + 1) + rand(0.05)} #{3 + rand(0.2)}" end end' ####################################################### # # GNUPLOT PROCESSING # # print gnuplot version ! gnuplot --version ############# # 1. # Start with a simple eps plot set term postscript eps enhanced color set title "Gnuplot Hacks 2.0 Script" set output "1.eps" plot \ "/tmp/gnuplot.dat" using 1:2 title "data 1", \ "/tmp/gnuplot.dat" using 1:3 title "data 2" !epstopdf --outfile=1.pdf 1.eps !rm -rf 1.eps ##################################### # # Try to adapt the figure for scientific publication # ############# # 2. # w/o colors - black/white set term postscript eps enhanced monochrome set output "2.eps" replot !epstopdf --outfile=2.pdf 2.eps !rm -rf 2.eps ############## # 3. # w/o colors - grayscale # Nice and increased font (in publications the figures are # normally small -> exonerate the reader and increase the fontsize set term postscript eps enhanced color "Times" 14 set output "3.eps" set style line 1 linetype 1 linecolor rgb "#000000" lw 1 set style line 2 linetype 2 linecolor rgb "#222222" lw 1 plot \ "/tmp/gnuplot.dat" using 1:2 title "data 1" with points ls 1, \ "/tmp/gnuplot.dat" using 1:3 title "data 2" with points ls 2 !epstopdf --outfile=3.pdf 3.eps !rm -rf 3.eps ############## # 4. # Set tics # Change the agenda style # Use postscript font features (symbole) set title "Gnuplot Hacks^{2} Script" set style line 99 linetype 1 linecolor rgb "#999999" lw 2 set key left top set key box linestyle 99 set key spacing 1.2 set grid xtics ytics mytics set output "4.eps" replot !epstopdf --outfile=4.pdf 4.eps !rm -rf 4.eps ############## # 5. # Use lines instead of points set output "5.eps" set style line 1 linetype 1 linecolor rgb "#000000" lw 1 set style line 2 linetype 1 linecolor rgb "#000000" lw 1 plot \ "/tmp/gnuplot.dat" using 1:2 title "data 1" with lines ls 1, \ "/tmp/gnuplot.dat" using 1:3 title "data 2" with lines ls 2 !epstopdf --outfile=5.pdf 5.eps !rm -rf 5.eps ############## # 6. # Data smoothing -> csplines curves # Gnuplot offers a simple algorithm to smooth data, # normaly you must execute a separate algorithm that smooth # data. Example algorithms are: # o Exponentially smoothed weighted # o Weighted moving averages # o Simple moving averages set output "6.eps" set style line 1 linetype 1 linecolor rgb "#000000" lw 1 set style line 2 linetype 1 linecolor rgb "#000000" lw 1 plot \ "/tmp/gnuplot.dat" using 1:2 title "data 1" smooth csplines ls 1, \ "/tmp/gnuplot.dat" using 1:3 title "data 2" smooth csplines ls 2 !epstopdf --outfile=6.pdf 6.eps !rm -rf 6.eps ############## # 7. # Data smoothing -> bezier curves # "shadow" data set output "7.eps" set style line 1 linetype 1 linecolor rgb "#000000" lw 2 set style line 2 linetype 1 linecolor rgb "#000000" lw 2 set style line 3 linetype 1 linecolor rgb "#cccccc" lw 1 set style line 4 linetype 2 linecolor rgb "#aaaaaa" lw 1 plot \ "/tmp/gnuplot.dat" using 1:2 notitle with points ls 3, \ "/tmp/gnuplot.dat" using 1:3 notitle with points ls 4, \ "/tmp/gnuplot.dat" using 1:2 title "data 1" smooth bezier ls 1, \ "/tmp/gnuplot.dat" using 1:3 title "data 2" smooth bezier ls 2 !epstopdf --outfile=7.pdf 7.eps !rm -rf 7.eps ############## # 8. # Data smoothing -> bezier curves # "shadow" data set style line 1 linetype 1 linecolor rgb "#c400cf" lw 4 set style line 2 linetype 1 linecolor rgb "#3e6694" lw 4 set output "8.eps" plot \ "/tmp/gnuplot.dat" using 1:2 title "data 1" smooth bezier ls 1, \ "/tmp/gnuplot.dat" using 1:3 title "data 2" smooth bezier ls 2 !epstopdf --outfile=8.pdf 8.eps !rm -rf 8.eps ############## # 9. # Log scale set logscale y set style line 1 linetype 1 linecolor rgb "#000000" lw 2 set style line 2 linetype 2 linecolor rgb "#000000" lw 2 set output "9.eps" plot \ "/tmp/gnuplot.dat" using 1:2 title "data 1" smooth bezier ls 1, \ "/tmp/gnuplot.dat" using 1:3 title "data 2" smooth bezier ls 2 !epstopdf --outfile=9.pdf 9.eps !rm -rf 9.eps ############## # 10. # Aspect ratio unset logscale set size ratio 0.5 set style line 1 linetype 1 linecolor rgb "#000000" lw 2 set style line 2 linetype 2 linecolor rgb "#000000" lw 2 set output "10.eps" plot \ "/tmp/gnuplot.dat" using 1:2 title "data 1" smooth bezier ls 1, \ "/tmp/gnuplot.dat" using 1:3 title "data 2" smooth bezier ls 2 !epstopdf --outfile=10.pdf 10.eps !rm -rf 10.eps ############## # 11. # Presentation Modus # Colors and widther lines # Label set style line 1 linetype 1 linecolor rgb "#c400cf" lw 4 set style line 2 linetype 1 linecolor rgb "#3e6694" lw 4 set style line 3 linetype 1 linecolor rgb "#391740" lw 3 set output "11.eps" set label "A miracle occure here" at 150, 4.8 set arrow from 140,5.4 to 140,3.6 ls 3 plot \ "/tmp/gnuplot.dat" using 1:2 title "data 1" smooth bezier ls 1, \ "/tmp/gnuplot.dat" using 1:3 title "data 2" smooth bezier ls 2 !epstopdf --outfile=11.pdf 11.eps !rm -rf 11.eps ############## # 12. # Multipot reset set grid xtics ytics set terminal postscript enhanced eps color solid set output "12.eps" set key spacing 1 set style data l set multiplot set origin 0.0,0.3 set size 1.0,0.7 set style line 1 linetype 1 linecolor rgb "#c400cf" lw 1 set style line 2 linetype 2 linecolor rgb "#3e6694" lw 1 set style line 3 linetype 1 linecolor rgb "#c400cf" lw 2 set style line 4 linetype 2 linecolor rgb "#3e6694" lw 2 set ylabel "raw data" set format x "" unset xtics unset xlabel set y2label " " plot \ "/tmp/gnuplot.dat" using 1:2 title "data 1" with points ls 1, \ "/tmp/gnuplot.dat" using 1:3 title "data 2" with points ls 2 ### lower graph set origin 0.0,0.0 set size 1.0,0.3 set ylabel "normalized" set ytics nomirror set y2range [*:*] set xlabel "time [s]" set xtics nomirror set format x plot "/tmp/gnuplot.dat" using 1:2 t "data 1" smooth bezier ls 3,\ "/tmp/gnuplot.dat" using 1:3 t "data 2" smooth bezier ls 4 unset multiplot !epstopdf --outfile=12.pdf 12.eps !rm -rf 12.eps # now we are diving into new spheres - reset all reset ############## # 13. # 3D Intro set terminal postscript enhanced eps color set output "13.eps" set yrange [-2:2] set pm3d splot exp(-x*x)*exp(-y*y) !epstopdf --outfile=13.pdf 13.eps !rm -rf 13.eps ############## # 14. # Monochrome set terminal postscript eps enhanced monochrome set output "14.eps" replot !epstopdf --outfile=14.pdf 14.eps !rm -rf 14.eps ############## # 15. # pm3d map set terminal postscript eps enhanced color set output "15.eps" set pm3d map replot !epstopdf --outfile=15.pdf 15.eps !rm -rf 15.eps ############## # 16. # another color combi set terminal postscript eps enhanced color set output "16.eps" set pm3d map set size square set palette rgbformulae 22,13,-31 replot !epstopdf --outfile=16.pdf 16.eps !rm -rf 16.eps ############################### # # Tips: # # o Name you gnuplot file with a ".gpi" suffix # vim and other editors will recognize files as GnuPlot Input # and adapt the syntax highlighting # # o ! - executes a shell: e.g. "!date". Sometimes you will pre-process # some data. Maybe this is the perfect place for that. Another example # are the convertion from eps to pdf. # # o Don not use png/gif as the output terminal # - gnuplot data is vector data - no pixel based data -> use vector output # - eps can be scaled without information loss # - Font output is cuter then for the png format # - If you want png/gif (for website etc.) use convert(1). # The following 4 lines my Makefile target: # %.png: %.eps # @echo "conversion in png format" # @convert -density 300 $< $*.png # @echo "end" # - For presentation png is sometimes a recommendation - if the graph is # really "complicated", a lot of data the rendering can be slow on older # machines. Furthermore: exact details can't be recognize via a beamer in # a range of ten meters. Convert therefore your eps to png with the upper # command (convert). # For an presentation: use nice colors and adapt the line width -> "lw 4" # or something. # - Convert the eps to pdf with ps2pdf (there are some problems, depending # on your installation, maybe you must convert the ps to eps one more time # and use epstopdf or other programms. There are from time to time some # bounding box problems (check that there are no silly border around the # generated graph). # epstopdf --outfile=figure.pdf figure.eps # - Easy integration into LaTeX (\includegraphics{figure.pdf} # # o Terminals # - gnuplot has support for several type of terminals. For example: # svg - W3C Scalable Vector Graphics. You may edit path afterwards # when you use svg. On the other hand, the fonts aren't that cute. # So I prefer eps as the one and only format # # o Visualize Dynamic Data (animated gif) # - my hint: skip animated gifs - use flv (flash) # 1. generate all figures as eps # 2. convert to png # 3. use mencoder to convert png files into a flv movie # mencoder mf://\*.png -mf w=360:h=240:fps=12:type=png -of lavf -ffourcc FLV1 -ovc # lavc -lavcopts vcodec=flv -o outputfile.avi # 4. grap a flv player for your homepage (try JW FLV PLAYER - # http://www.jeroenwijering.com/?item=JW_FLV_Player) # 5. embeed the player and movie files within html on you page # # # # # o Links: # - http://t16web.lanl.gov/Kawano/gnuplot/ # - http://www.ibm.com/developerworks/library/l-gnuplot/ # - http://sparky.rice.edu/~hartigan/gnuplot.html # - http://www.helsinki.fi/~jalaaman/gnuplot/index.html