Gnuplot
Was ist das?
Gnuplot ist ein Programm zum Plotten von mathematischen Funktionen oder Daten.
Es ist Freeware und kann von der Seite www.gnuplot.info
runtergeladen werden.
Anders als viele andere Programme, die das gleiche tun, ist Gnuplot ohne eine GUI ausgestattet-
alles funktioniert über Kommandozeileneingaben oder Skriptdateien.
Das mag für den ein- oder anderen abschreckend sein, ist aber eigentlich ein klarer Vorteil.
Kommandozeilen basiert Programme wie Gnuplot können leicht aus automatisch ablaufenden Shell-Skripten
oder sonstigen Programmen über Pipes aufgerufen werden.
Außerdem speichert man i.d.R. die Befehle an Gnuplot in einer von Gnuplot lesbaren Textdatei ab, so dass
man diese immer wieder verwenden kann. Auf diese Weise erreicht man einen einheitlichen Look aller Grafiken.
Es gibt viele gute Einfühungen im Netz. Leider gestaltet sich die Suche nach einigen Spezialfunktionen nicht immer
ganz so einfach. Daher gibt es hier eine (unvollständige) Übersicht über "Wie macht man ..." in Gnuplot.
function plotting
2D-functions
Plotting simple y=f(x)-style functions:
plot exp(-x**2)
|
|
3D-functions
Plotting simple z=f(x,y)-style functions:
set hidden
set isosamples 50,50
splot sin(sqrt(x**2+y**2))/sqrt(x**2+y**2)
|
|
data plotting
plotting a simple dataset over the index
Here we have a data file containing three time series. We now want to plot the first series over the index.
As my dataset stores a 3d time series of the roessler equation, it is called "roessler".
plot "roessler" u 0:1
"u 0:1" stands for "using 0:1" which tells gnuplot to plot the 1st column in the data file over the index (column 0)
|
|
plotting a simple dataset over another
Here we have the same datafile, but now I want to plot the x- and y-coordinate of the time series
simultanously by interpreting them as geometrical points in a 2d space.
plot "roessler" u 1:2
|
|
plotting 3d datasets
Now we want to do the same as in the previous example, but in 3d.
splot "roessler" w l
"w l" stands for "with lines", so points are not drawn with a "x" as in the previous pictures. Instead all points get
connected with a line. Alternatively you can use "w lp" or "with linespoints", which does both.
|
|
plotting a height profile
If you have some "matrixlike" data, maybe a hight profile, you want to plot a landscape like plot.
This can be achieved with the keyword matrix. Additionally, if we want the plot to look nice, we use the
pm3d plot style. This provides nice 3d surfaces and color mappings.
splot "correlation.txt" matrix w pm3d
|
|
plotting a map
Now we want the same image as in the previous example, but without the mountains.
set pm3d map
set xrange [0:52]
set yrange [0:52]
splot "correlation.txt" matrix w pm3d
The color of each rectange is computed by the mean of all four datapoints at the corners. If you want something
else (e.g. color the rectange in respect only to the lower right corner), you can change it by typing
set pm3d corners2color c1
Other possible values are c2, c3, c4, mean (default), median, geomean.
|
|
data fitting
simple fitting
Now, we have a dataset (maybe some measurement) and a model. We want to fit the parameters of our model,
such that it fits the real data. Our model is given by the equation f(x) = bxa
f(x) = b*x**a
fit f(x) "square" u 0:1 via a,b
plot "square" u 0:1, f(x)
While fitting, gnuplot prints out some text. At the end, it tells us the results:
Final set of parameters Asymptotic Standard Error
======================= ==========================
a = 1.72797 +/- 0.032 (1.852%)
b = 0.036066 +/- 0.005087 (14.1%)
Of course you can do that with as many parameters as you wish and you can do fitting in 3d too.
Sometimes, if you are trying to fit a much more complicated function with more parameters, it might happen that
gnuplot does not manage it to find the result you have in mind. Gnuplot uses an iterating algorithm to find the best fit,
so if gnuplot's fit does not satisfy you, try to give appropriate initial values for the parameters.
You can do that by just typing a=1.234 etc. after you defined your function. Try to start the fitting again and the result
should be much better. If not, you maybe have a function in which small changes of some of the parameters have a big effect.
In this case, you could try to add a multiplier as 0.001 to the parameter. (e.g. 0.001*a instead of a)
|
|
saving you plots on disc
setting terminal and output
Of course, sometimes you want to have your plot saved on disc. Gnuplot supports several graphics formats for
pixel based graphics and vector based formats.
When you type set term, gnuplot will provide a list of all possible formats it supports. The most "terminals"
take some extra parameters (e.g. size, color or b/w output etc...)
For saving graphics on disc, I use either png for pixel based graphics, or eps (Postscript) for vector based graphics.
Type set term png size 1024,768 to get a png with size 1024x768.
Type set term postscript eps to get a eps
At last, tell gnuplot where to save the image.
set output "output.xxx"
Then, you will need to plot your figure either directly by using the plot/splot call, or by
replot, which will reproduce the last plot.
If you do not specify an output file, gnuplot will print it's output on standard out (the console or terminal).
This can be useful in some cases. (e.g. you want to make a cgi script, which generates some plots online on a website)
|
making your plot look nice
enhanced typesetting
In one of the first examples, you might have noticed the ugly way gnuplot displays the
formulas (e.g. sin(sqrt(x**2+y**2))/sqrt(x**2+y**2) ).
In some gnuplot terminals, you can use an enhanced mode, to enable sub and super scripting.
Try set term postscript eps enh to enable enhanced mode on eps terminal. (Of course, you can try it on the
x11 or win or all other terminals aswell, but not every terminal supports enhanced mode)
|
|