Pages

Wednesday, February 1, 2012

2D Basics: functions and data plotting

When you start to get interested with a concrete software (let me say some fancy words here... you bastard!) you don't give a shit about all the details but yet, all the pages you search are full of... say details you don't give a damn about.

What I'm saying its that "I better prefer to explore than decode". I don't want to study. I want to get to the point I'm searching.

To plot a function you just define it and it will show to you. Go to the console, write gnuplot and do

[...]
f(x,a,b) = a*x**2 + b 
plot f(x,10,30) title 'the simplest plot'
[...]

you will get this


to change the format style (color, line type: doted, dashed, lines-points) do

[...]
plot f(x,10,30) with linespoints pointsize 0.5 pointtype 2 linecolor rgb "black" title 'f(x,a,b) with lines and points'
[...]


Change the parameters as you wish to get another stuff.

  • Tables

Now we will generate a table of $x$-$y$ data and plot it as it was a data file we had for something else


[...]
## Using functions to generate data : tables
set output 'f(x,a,b).table'
set table
plot f(x,10,30)
unset table
reset # If you don't reset you will print the results on 'f(x,a,b).table'

## Ploting data from a file

plot 'f(x,a,b).table' using 1:2 t 'Plotting from a file'
[...]

You get the same you got but as it was data


  • Parametric plots
You need to make a grid (there are another ways, but that will be shared later). A grid is just a table of $x$-$y$ points. Your computer always discretizes the space in order to evaluate the function you with to it. In 2D the grid is just a straight line. So we do, 

[...]
set xrange [0:2*pi] # Yes, pi is defined within gnuplot, awesome ha? No less that one should expect!
set yrange [-1:1] 

set output 'xy-grid.table' # This is the output file to use later
set table
plot x 
unset table
reset # If you don't reset you will print the results on 'xy-grid.table'

plot 'xy-grid.table' using 1:2 title "The grid is a line if you are not a curved space dealer"
[...]

Which looks like

Now we use the file "xy-grid.table" to plot whatever function we want using explicit parameterization
of the axes.We will use the axes

[...]
set size square # This sets the scale of 'x' the same as 'y' 1 to 1 proportion

plot 'xy-grid.table' using ( cos($2) ):( sin($2) ) title 'this was a fucking line!!', \
     'xy-grid.table' using ( cos($2)/5 ):( sin($2)/5 ) title 'this was another fucking line!!'
[...]
I plotted two "different" parametrizations, so you can see how its done.

That's all!
Bye

Monday, January 30, 2012

Real-Time Animation: bash & gnuplot

If you are using gnuplot to "plot" (I know, that is a surprise) data after the ill-computer calculations but still don't want to print a file and then: open gnuplot  or run a script (i'm getting bored even writing this) you can use bash to do it. Easy. Say your program prints $x$-$y$ points to the console each time you call it

~$ ./program < initial_data.dat
1 2 
3 4
7 5
8 6

then you make bash do this

#!/bin/bash
for((i=0;i<1;i++))
do 
echo "plot '-' u 1:2" 
echo "./program < initial_data.dat" | bash
echo "e"
done | gnuplot -persist
echo "Finished!"


Example. I don't really have a program to show you this and, honestly, I don't want to do it, so I will use bash directly to plot something with gnuplot. Let's try to create an animated gif on gnuplot for a sinus varying its wavelength.

#!/bin/bash
for((i=0;i<1;i++))
do 
echo "set term gif animate delay 40 enhanced;"
echo "set output \"sin_out.gif\";"

for ((k=0;k<11; k++)) do
echo "plot sin($k*x)" 
done

done | gnuplot -persist
echo "Finished!"

PD. The reason why I used a loop gnuplot need to be fed "|" with all the script at once. Byeee

Thursday, January 26, 2012

This is a pool of gnuplot stuff

Hi there.

I love to use gnuplot for everything. But I didn't figure that out by myself. So I want to put all I've learned together so it can help someone as it helped to me.