Feeds:
Posts
Comments

Archive for the ‘gnuplot’ Category

you may also get the error

You can’t change the terminal in multiplot mode

the following code WILL produce the error:

set multiplot layout 1,2
set term postscript eps enhanced
set out 'a.eps'
plot sin(x)
plot cos(x)

this is an annoyance but just set the term and out variables before you set the multiplot command to get rid of the error message.

set term postscript eps enhanced
set out 'a.eps'
set multiplot layout 1,2 # set 'multiplot' after 'term' and 'out'
plot sin(x)
plot cos(x)
Advertisements

Read Full Post »

do not read the startup file ~/.gnuplot

gnuplot reads the ~/.gnuplot file on startup. one can define useful macros or variables in this file so that they are readily available to the user. today i wanted to start gnuplot and bypass the definitions contained in the start up file. one obvious way it to just rename the start up file but that seemed like a bit of a hassle so this is what i came up with after reading the help section for startup

$HOME='' gnuplot

that is just set the HOME environment variable to an empty string before invoking gnuplot. This works because gnuplot looks for the file .gnuplot in the directory defined by the environment variable HOME which is unset just before invoking gnuplot. please note that unsetting HOME this way is only temporary and lasts as long as the gnuplot session. to find out more about startup use the following within a gnuplot session

gnuplot> help startup

$ gnuplot –version
gnuplot 4.6 patchlevel 1

$ bash –version
GNU bash, version 4.2.42(2)-release (i686-pc-linux-gnu)

Read Full Post »

there have been some situations where i needed a custom grid on a 2-dimensional plot in gnuplot instead of a regularly placed grid. in order for me to set up a customized grid i have to first set up a customised xtics and/or ytics. gnuplot does provide a way of providing user preferred tics by manually typing them in at the gnuplot command prompt:

gnuplot> set xtics (1,2,3,4)

however that might get too tiresome for your digits. naturally one would want to employ the powerful text processors and stream editors that GNU/Linux provides. this is how i went about it:

assume that the data file(data.out, say) is as follows:

# some comments and blank lines

#      #x1	#x2 	  #x3

1      1.1	3.9	  4.5
2      2.7	6.1	  5.6
3      3.9	2.3	  3.4
       
4      3.5	4.3	  34.0
5      12.1	3.4	  15.9

i want to set the xtics to the values in column 2 and ytics to the values in column 3 say. first i conjure up the following shell script (ttf.sh, say):

#!/usr/bin/env bash

# set tic levels from file at the gnuplot prompt
# examples:
# gnuplot> `./tff.sh data.dat 1 xtics`
# gnuplot> `./tff.sh data.dat 2 ytics`

# dataFile from which tic levels will be read
dataFile=$1

# which column to use
column=$2

# xtics OR ytics 
whichTics=$3

sed -e '/^#/d' $dataFile | \
    sed -e '/^$/d' | \
    cut -f $column | \
    tr '\n' ',' | \
    sed -e "s/^/set $whichTics (/" | \
    sed -e 's/,$/)\n/'

and then in gnuplot carry out the following sequence of commands:

gnuplot> `./tff.sh data.out 2 xtics`
gnuplot> `./tff.sh data.out 3 ytics`
gnuplot> plot 'data.out' u 1:2 w p pt 13 ps 2

to produce the following plot:

it should be noted that some of the grid lines are hidden due to alignment with borders.

the shell script needs 3 inputs

  1. datafile name
  2. which column to pull from the datafile
  3. which tics to set the values to

then the whole command is sandwiched between couple of backtics which bring in the command substitution magic. one could very well do the following in bash:

$ ./tff.sh data.out 2 xtics > tmp

and then in gnuplot

gnuplot> load 'tmp'

but i prefer the backtics.

now the explanation of the wonderful sed magic. first we do a little bit of file cleaning/processing like removing comments and blank lines. these operations may not necessarily apply to your case or might need additional operations which is not that difficult a job. anyways on to the explanations now…

sed -e '/^#/d' $dataFile

simply deletes all lines starting with the # symbol. of course if the comment character is different one should change the code appropriately. ^ stands for the beginning of all lines and ^# means all lines starting with a # symbol. the `d’ command instructs sed to delete those lines. we then pipe (|) the output of this command to the next command

 
sed -e '/^$/d' 

which simply deletes all blank lines. since ^ represents the beginning of all lines and $ represents the end of all lines, ^$ then represents those lines with nothing in between ^ and $ i.e. blank lines. as always the `d’ command instructs sed to delete this line.

cut -f $column

next we cut out the required column from the datafile using the cut command. we assume here that the field separater is a TAB character. if some other field separater is being used that can be specified with the -d switch. do a `man cut` to find out more. now comes the all important part of the entire exercise, what we want is to transform the vertical stack of values:

1
2
3

to a horizontal comma separated list of values:

1,2,3,

notice the comma after 3.
we achieve this by using the tr command which we instruct to transform all the newline characters (`\n’) to the comma character (`,’). we do pick up an unwanted comma at the very end of the list which we get rid of and transformed to a right bracket ) like so:

sed -e 's/,$/)\n/'

this time we are using the subsitute command `s’ and asking sed to replace the last comma by a ). as you already know $ represents the end of the line so (,$) represents the last comma we substitute those with a right bracket ) and a newline character `\n’.
of course we had to prepend the “set xtics (” to the beginning of the list and that bit is done by the following:

sed -e "s/^/set $whichTics (/"

notice the double quotes instead of the single quotes. we use double quotes so that $whichTics is automatically expanded to the user supplied tics name.

Read Full Post »

gnuplot annoyance

i hate myself for writing that post title… the joy of free/libre software should muffle any feeling of annoyance or hatred towards a software, so i apologize in advance. anyways…

i wonder why the following does not work in gnuplot


plot "a.dat" u 1:2 w p, [0:1] 1-sqrt(x)

while the following does


plot [0:1] 1-sqrt(x), "a.dat" u 1:2 w p

why should the order be important?

i m sure it has a perfectly logical explanation… which i have yet to discover!

Read Full Post »

gnuplot compile options

how do i know with what compile options gnuplot was compiled on my gnu/linux system ?

if you are looking for an answer to that question then you can get an answer by typing the following at the gnuplot command prompt

gnuplot> show version long

Read Full Post »

gnuplot help

normally i used the following to querry gnuplot for any help

gnuplot> help plot

i learn’t a new method today … say you want to figure out what isosamples mean, then write the following at the gnuplot prompt

gnuplot> ?isosamples

if you want to know some thing about terminal then its

gnuplot> ?terminal

Read Full Post »

Say in a datafile you have 10 points that you want to plot but on the plot itself there is no easy way to figure out the serial position of the data point in the file by just looking at the plot. of course one can estimate the coordinates of the datum and then compare against the values in the data file. but i find that a bit cumbersome so this is the solution i tend to use a lot.

contents of data.dat

1 3
5 9
4 6
2 9
3 8
2 1
1 9
9 9
2 2
5 5

gnuplot> plot “data.dat” u 1:2 w p pt 4 ps 2

produces the plot as shown below

plot with no labels

gnuplot> plot “data.dat” u 1:2 w p pt 4 ps 2 ( thanks Swagat !!! )

gnuplot> plot “data.dat” u 1:2: w p pt 4 ps 2, ” ” using 1:2:(sprintf(“%d”,column(0))) with labels

however produces the following which makes my life a whole lot easier

plot with labels

a big thanks to the gnuplot google group and its helpful posters

http://www.gnuplot.info/docs/node253.html

Read Full Post »

Older Posts »