Posts mit dem Label gnuplot werden angezeigt. Alle Posts anzeigen
Posts mit dem Label gnuplot werden angezeigt. Alle Posts anzeigen

Mittwoch, 4. März 2015

plot an image transparently with gnuplot

The standard way to plot a image in gnuplot is the following:
plot x
plot 'test.png' binary filetype=png with rgbimage 



with several filetypes, like jpg, png and so on. But if you want to have this image transparent you need to choose rgbalpha instead of rgbimage and use u 1:2:3:(alpha-value) with the alpha-value ranging between 0:255 as follows:

plot x
replot 'test.png' u 1:2:3:($4*0.5) binary filetype=png with rgbalpha 


Samstag, 17. Januar 2015

plot color of data point with z-value

To plot a x,y,z data file with the z-value being the color value of the x,y data point use "with linespoints palette" as following:

plot 'test.dat' u 1:2:3 with linespoints palette 

An example is shown below:

Donnerstag, 15. Januar 2015

gnuplot use figure with alpha channel

Sometimes it is desirable to plot data on top of a figure or a figure on top of a graph. In the latter case the figure has to be transparent so that the plotted data is still shown. This can be achieved via the "with rgbalpha" option as shown in the following:
plot "partly_transparent_figure.png" binary filetype=png  with rgbalpha

Donnerstag, 23. Oktober 2014

gnuplot loop over all blocks in datafile

If you have a data file for gnuplot with indexable blocks (two blank lines between data blocks) e.g.:

0 1
1 2

0 1.5
1 2.5


0 2.0
2 3.0

and want to plot each plot individually but with the same plot command, you can simply use the gnuplot 4.6 stats function to get the number of blocks and loop over the blocks in a combined command:

#!/usr/bin/gnuplot

file="data.dat"
stats file

plot for [i=0:STATS_blocks-2] file index i w l ls i+1
PS: if you only have one blank line between data blocks you need to use the every command:
#!/usr/bin/gnuplot

file="data.dat"
stats file

plot for [i=0:STATS_blank] file every :::i::i w l ls i+1

Mittwoch, 22. Oktober 2014

histogram with gnuplot

if you want to plot a histogram with gnuplot, just use the raw data in "data.dat" and plot the data via the following command:

the data is transformed into a histogram via the bin function and then plotted via the plot with boxes command.

  #!/usr/bin/gnuplot

file="data.dat"
binwidth=1.0
bin(x,width)=width*floor(x/width)
set style fill solid 0.5

plot file u (bin($1,binwidth)):(1.0) smooth freq with boxes ls 1 t 'total'


the result looks something like that:

Montag, 28. April 2014

make gnuplot produce a latex standalone document

gnuplot has a view very good terminals including the epslatex-terminal the only backdraw which i had recently was that the terminal produces either:
- a latex file which can be included in the latex-document
or
- a latex-document which produces a pdf with the minimal-documentclass
but it does not produce a document which can be used via includestandalone.

But now I found a way to do it.

1.  create example with epslatex terminal


set terminal epslatex
set output "test.tex"
plot sin(x)
set output

2. Add preamble for standalone figure and add \end{document}

Add at the beginning of test.tex:
 \documentclass{standalone}
\usepackage{graphicx}
\begin{document}

Add at the end of test.tex
\end{document}

PS: I also wrote a gnuplot script doing all of this:

epslatexterm(name, xsize, ysize, gnuplotcommand) = sprintf('\
name="%s";\
file=name.".tex";\
set terminal epslatex size %s, %s;\
%s;\
set output name.".tex";  \
replot; \
unset output;\
eval bash(''echo \" \\\\begin{document}            \" |cat - ''.file.'' > tmp    '');\
eval bash(''echo \" \\\\usepackage{graphicx}       \" |cat - tmp      > tmp2   '');\
eval bash(''echo \" \\\\documentclass{standalone}  \" |cat - tmp2     > tmp    '');\
eval bash(''echo \" \\\\end{document}              \" |cat tmp -      > tmp2   '');\
eval bash("sed ''s/".name."/".name."-inc/g'' tmp2 > ".file  );\
eval bash(''mv ''.name.''.eps ''.name.''-inc.eps'');\
eval bash(''rm tmp tmp2 -f'');\
set terminal wxt; \
',name,xsize,ysize,gnuplotcommand)

Mittwoch, 11. Dezember 2013

Gnuplot set linecolor from palette

In gnuplot you can set the color of a line in a plot by using:
 linecolor "COLOR" 

For example via, (linecolor is abbreviated via lc):
 plot sin(x) lc "blue"

But there are several others, including setting the linecolor from a palette, so according to the blog entry here and here. You set up a new color macro scheme and use it as a palette. This color palette you can then use to define the color of a line by using either the color of a colorbox (cb) value or a fractional value ranging from 0 to 1:
set palette @redgreenblue
plot sin(x) lc palette cb 1 
replot sin(x) lc palette frac 0.5 


More information can be found in gnuplot via:
help linecolor

Gnuplot user-defined color schemes

As already explained in this blog entry, this is the way to define color macros:
set macros
redgreenblue_small = "defined (\
                   0  0.0 0.0 0.5, \
                   1  0.0 0.0 1.0, \
                   2  0.0 0.5 1.0, \
                   3  0.0 1.0 1.0, \
                   4  0.5 1.0 0.5, \
                   5  1.0 1.0 0.0, \
                   6  1.0 0.5 0.0, \
                   7  1.0 0.0 0.0, \
                   8  0.75 0.0 0.0, \
                   9  0.5 0.0 0.0, \
                   10  0.25 0.0 0.0, \
                   11  0.0 0.0 0.0 )"

To use them in gnuplot you simply type:
set palette @redgreenblue


Montag, 9. Dezember 2013

Gnuplot write in a variable from bash

If you want to read in a variable, which is in a file for example or you simply have a script to calculate it into gnuplot. Simply use the following command:

 variable="`BASH COMMAND`"

instead of BASH COMMAND you can use any bash command you like and even load variables or get data from files, e.g.:

 variable="`echo $VARIABLE`"

Dienstag, 3. Dezember 2013

Gnuplot multiplot - problems with replot

All those who use gnuplot multiplot probably know about the problems using replot in the multiplot environment. When using replot the lines which were plotted first and get replotted and appear darker or broader and if the xranges and yranges do not fit perfectly the plots get shifted.


For all that a small
 clear
statement in between the plot and replot might help.


For this i will show a full example in the following and two images of the two results with the png-terminal:
set terminal png
set output "test.png"
set multiplot

plot sin(x)
clear
replot cos(x)

unset multiplot 


without clearwith clear

Donnerstag, 28. November 2013

gnuplot array

How does it work to have an array in gnuplot?

The simplest way to create an array is like this:
 array="1 2 3 4 5"

to address a element you simple use the word command (for the zeroth element e.g.):
 word(array, 0)

In a real exercise you could use it e.g. to loop over files:
files="file1 file2 file3"
plot for [i=0:2] word(files,i) u 1:2 ti word(files,i)

gnuplot loop over datablocks

How to plot several datablocks with separate linestyles in one file?

In the following three datablocks are shown:

0.0 1.0
1.0 2.0
2.0 3.0

0.5 1.5
1.5 2.5
2.5 3.5

0.75 1.75
1.75 2.75
2.75 3.75

The first one e.g. can be addressed in gnuplot via:
plot 'datafile' every :::0::0

But to address all consecutively you need a loop the simplest way would be this loop over i=0,1,2:
plot for [i=0:2] 'datafile' every :::i::i w l ls i

There is also the possibility to use the do for loop with curly brackets {}:
 do for [i=0:3] {
 plot "datafile" ls i
} 

Donnerstag, 7. November 2013

gnuplot: redirecting print

Sometimes it comes in handy to redirect the output of the print command, especially if you fit something and want to reuse the data later. This can be done with gnuplot via:
 set print "FILENAME"  

A complete example:
set print "test.dat"
print "test"
set print  

Dienstag, 13. August 2013

Contour plot gnuplot

Contour plot

To create a contour plot like this two script files are needed. First of all a contour.gnu gnuplot file which plots the data and then contour.sh bash file which edits a temporary output file.

Steps in gnuplot:

  1. Write data into temporary file
  2. Write contour lines into temporary file
  3. Edit temporary file with contour.sh
  4. plot the edited data





Contour.sh


 #!/bin/bash  
 gawk -v d=$2 -v w=$3 -v os=$4 'function abs(x) { return (x>=0?x:-x) }  
   {  
       if($0~/# Contour/) nr=0  
       if(nr==int(os+w/2) && d==0) {i++; a[i]=$1; b[i]=$2; c[i]=$3;}  
       if(abs(nr-os-w/2)>w/2 && d==1) print $0  
       nr++  
   }  
   END {  if(d==0) {  
           for(j=1;j<=i;j++)  
           printf "set label %d \"%g\" at %g, %g centre front\n", j, c[j], a[j], b[j]  
       }  
   }' $1  

d: which option (0: set labels, 1: give data back where no label is)
w: width
os: offset

Contour.gnu

 
 # Step 1
 set xrange [-5:0]  
 set yrange [2:5]  
 set isosample 150, 150  
 set table 'test.dat'  
 splot sin(1.3*x)*cos(.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x)  
 unset table  
 # Step 2
 set cont base  
 set cntrparam level incremental -3, 0.5, 3  
 unset surf  
 set table 'cont.dat'  
 splot sin(1.3*x)*cos(0.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x)  
 unset table  
 reset
 # Step 3 and 4
 set xrange [-5:0]  
 set yrange [2:5]  
 unset key  
 set palette rgbformulae 33,13,10  
 load '<./contour.sh cont.dat 0 20 0'  
 plot 'test.dat' w ima, '<./contour.sh cont.dat 1 20 0' w l lt -1 lw 1.5  

Step 1:

Surface data of the function "sin(1.3*x)*cos(.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x)" is plotted into "test.dat" in the x- and y-range given.

Step 2:

By using "set cont base" only the contour data of the function is written to the file "cont.dat"

Step 3:

By loading the contour.sh script the contour data in cont.dat is read and the labels for gnuplot are created.

Step 4:

The plot command plots first the data as an image and adds to that the modified contour data (modified by contour.sh)


adapted from http://gnuplot-tricks.blogspot.com/2009/07/maps-contour-plots-with-labels.html

Mittwoch, 31. Juli 2013

gnuplot tikz terminal

 Just now i found out about the tikz terminal in gnuplot 4.6 and just tried it out. It works very nicely, but I think I have to put a little bit of effort into it if its worth to change from pdflatex terminal to lua tikz.
But here is how it works:

Usage

 set terminal lua tikz standalone
 set output "test.tex"  
 plot sin(x)   

standalone/nostandalone/fulldoc

Compiling

 > pdflatex test.tex
immediate compiling only works as far as I saw for standalone and fulldoc

UPDATE COMING SOON

gnuplot if else

gnuplot 4.4 and lower:

 if (<condition>) <command-line> [; else if (<condition>) ...; else ...]
commands are just written after if condition

gnuplot 4.6

 if (<condition>) { <command>; <command>  
  <commands>  
  <commands>  
  } else {  
  <commands>   
  }  
 Commands enclosed in curly brackets.

string comparison:

test eq "test" 

And with this framework you can start building your logical setups in gnuplot, have fun.

gnuplot: key magic

All info about key: www.gnuplot.info

positioning key

 set key top left
top/bottom/centerleft/right

justification of the text

 set key Left
Left or Right (Default)

small tricks

 set key reverse            ## reverse text and sample
 set key samplen <length>   ## change length of samplen
 set key spacing <spacing>  ## set spacing between key lines

Dienstag, 30. Juli 2013

Changing textsize epslatex terminal or latexterm subroutine

 latexterm subroutine

Example file to change the fontsize in my latexterm subroutine:


The way it works:
1. Loading the gnuplot setup file
2. plotting a random function
3. evaluating the latexterm subroutine while renewing the normalsize in latex to another fontsize

 load "gnuplot-setup" 
 plot sin(x) 
 eval latexterm("test","6cm","4cm","",'\n\\renewcommand\{\\normalsize\}\{\\fontsize\{24.88\}\{30\}\\selectfont\}') 

epslatex terminal


In general you can also use it to change the textsize with the epslatex terminal for that you just have to add an option to the epslatex terminal like:

 set terminal epslatex header "\\renewcommand\{\\normalsize\}\{\\fontsize\{24.88\}\{30\}\\selectfont\}"

gnuplot setup file

So here I am gonna show you my gnuplot setup file, which I load into every gnuplot script, I will put up a standard gnuplot script at some point.
This script can be loaded into gnuplot via:

load "/folder/of/your/gnuplot/script/script.gnu"

 

First part: Linestyles

 #--- LINESTYLES------------------------------------------------------- 
 set style line 1 lt 1 lc rgb "black"  pt 2 lw 2.0 
 set style line 2 lt 1 lc rgb "red"   pt 4 lw 2.0 
 set style line 3 lt 1 lc rgb "blue"  pt 6 lw 2.0 
 set style line 4 lt 1 lc rgb "#44AA44" pt 8 lw 2.0 
 set style line 5 lt 1 lc rgb "#cc7722" pt 10 lw 2.0 
 set style line 6 lt 1 lc rgb "#BBBBBB" pt 12 lw 2.0 

The first paragraph is just to set up a few linestyles in gnuplot which look good, so I set up 6 good looking linestyles, which can be used by typing:
 plot sin(x) ls 1 

Second part: Commands

bash("")

 #--- bash("command") ------------------------------------------------- 
 # -allows to use bash commands in gnuplot 
 bash(cmd) = sprintf('set print "| /bin/bash"; print "%s"; set print', cmd) 

which gives you an command to use bash easily in gnuplot:
 eval bash("echo test")


latexterm("","","","")

 #--- latexterm("name", "xsize", "ysize","command","latexcommand")--\  
 ----------------------------  
 # -sets terminal to epslatex and creates output file with xsize and\  
  ysize  
 latexterm(name, xsize, ysize, command,latexcommand) = sprintf('\  
 name="%s";\  
 xsize="%s";\  
 ysize="%s";\  
 command="%s";\  
 latexcommand="%s";\  
 set terminal epslatex size xsize, ysize color colortext standalone \  
 header "\\usepackage[T1]{fontenc}\n\\usepackage{mathptmx}\n\\usepackage{helvet} ".latexcommand command ; \  
 set output name.".tex"; \  
 replot; \  
 unset output;\  
 eval bash("latex ".name.".tex"); \  
 eval bash("dvips ".name.".dvi"); \  
 eval bash("ps2pdfbb ".name.".ps"); \  
 eval bash("rm -rf ".name.".aux ".name.".dvi ".name.".log ".name.".ps ".name.".tex ".name."-inc.eps"); \  
 set terminal wxt; \  
 ',name,xsize,ysize,command,latexcommand)   

which gives you an terminal which automatically creates an latex file and converts it into pdf (you need a ps2pdfbb script --> i will upload it later, but it can be also found online; the script corrects the wrong bounding box created by gnuplot).
 eval latexterm("test","6cm","4cm","","")


fitcube("","")

 #--- fitcube("plotinfo", "n")----------------------------------------- 
 # -fits a cubic function to the data plotinfo with the name "n" 
 fitcube(plotinfo,n) = sprintf('\ 
 %s(x)=a%s*x**3+b%s*x**2+c%s*x+d%s;\ 
 fit %s(x) %s via a%s,b%s,c%s,d%s;\ 
 print "fitted function is saved in %s(x): \ 
 %s(x)=",a%s,"x**3 + ",b%s,"x**2 + ",c%s,"*x + ",d%s;\ 
 eval bash("rm -f fit.log");\ 
 x1%s =(-2*b%s-sqrt(4*b%s**2-12*a%s*c%s))/(6*a%s);\ 
 x2%s =(-2*b%s+sqrt(4*b%s**2-12*a%s*c%s))/(6*a%s);\ 
 y1%s =%s(x1%s);\ 
 y2%s =%s(x2%s);\ 
 print "extreme points at (",x1%s ,"/",y1%s ,") and (",x2%s ,"/",y2%s ,") \ 
 and are saved in (x1%s /y1%s ) and (x2%s /y2%s )";\ 
 ',n,n,n,n,n,n,plotinfo,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n 
 ,n,n,n,n,n,n,n) 
Fits a cubic function to the function you say it to. It will save the maxima and minima into x1${functionname} y1${functionname} and x2${functionname} y2${functionname}. It allows a continues use of fitcube and storage of all needed parameters, if different names for the function are chosen.

Example to fit a dataset in file "data" with column 1 and 2 into the function named function:

  eval fitcube('"data" u 1:2',"function")

Third part: Macros

set macros 
 redgreenblue_small = "defined (\ 
           0 0.0 0.0 0.5, \ 
           1 0.0 0.0 1.0, \ 
           2 0.0 0.5 1.0, \ 
           3 0.0 1.0 1.0, \ 
           4 0.5 1.0 0.5, \ 
           5 1.0 1.0 0.0, \ 
           6 1.0 0.5 0.0, \ 
           7 1.0 0.0 0.0, \ 
           8 0.75 0.0 0.0, \ 
           9 0.5 0.0 0.0, \ 
           10 0.25 0.0 0.0, \ 
           11 0.0 0.0 0.0 )" 
 redgreenblue = "defined (\ 
           0 0.0 0.0 0.5, \ 
           1 0.0 0.0 1.0, \ 
           2 0.0 0.5 1.0, \ 
           3 0.0 1.0 1.0, \ 
           4 0.5 1.0 0.5, \ 
           5 1.0 1.0 0.0, \ 
           6 1.0 0.5 0.0, \ 
           7 1.0 0.0 0.0, \ 
           8 0.5 0.0 0.0)" 

Macros for 3d colormaps. If will give a tutorial on that in a later blogentry.