In this example we plot a sin wave,
| |
The first example just plots one sinusoid.
Run it using load 'gnu.wave' from within gnuplot. |
[an error occurred while processing this directive] |
The next example plots two sinusoids and
their sum.
load 'gnu.wave2' |
[an error occurred while processing this directive] |
The next example shows how to make a movie
by using load 'gnu.wave4' to recursively carry out
functions that increment t and replot.
You should run this command -
load 'gnu.wave3' |
[an error occurred while processing this directive] |
- noting that that file loads this one, which
then loads itself again...
It loads itself forever, so you will need to hit Ctrl-C to stop it. [Some window-managers have the annoying feature that by default whenever a fresh plot happens in gnuplot, the window manager gives focus to the gnuplot window; this default behaviour can be disabled. I like to have focus follow the mouse.] |
[an error occurred while processing this directive] |
The next example rectifies the problem of the never-ending movie by
loading gnu.wave6 which contains an if
before its load command.
Run this example using
load 'gnu.wave5' |
[an error occurred while processing this directive] |
This file, gnu.wave6, is used by all the remaining examples. |
[an error occurred while processing this directive] |
You'll probably find that the previous example, gnu.wave5, which increased the xrange of the plot, produced a pretty lousy-looking plot, which didn't look like pictures of sinusoids any more. The problem was that the sinusoid has too many wiggles compared with the default number of samples. We fix this problem by cranking up the number of samples. This file, gnu.wave7, shows how. |
[an error occurred while processing this directive] |
In these files, gnu.wave8 and gnu.wave9, we play around with the value of w2 a little, seeing the effect of changing the dispersion relation. |
[an error occurred while processing this directive] [an error occurred while processing this directive] |
This file, gnu.wave10, goes the whole hog, adding up five sinusoids. Physicists may find it instructive to play with the definition of the parameter dwdk. |
[an error occurred while processing this directive] |
gnuplot has a function called fit
which can fit parameterized functions to data files
by twiddling whichever parameters you choose.
The objective function that is minimized by the fitting process is the sum of squares of residuals. [The residuals are the differences between the functions and the data points.] This is the standard objective function, widely used in data-fitting, but do be careful to think whether it is an appropriate objective function. It's appropriate, for example, if you wish to model the residuals as coming independently and identically distributed from a gaussian distribution. fit is a pretty neat function. Its syntax is similar to the plot syntax. | |
This first file, gnu.fit1, simply plots two data files,
one of which contains half-hourly temperature data, and one daily
averages of temperatures. It also plots a function
plot '2006/Year' u ($0/48.0):($2) w l 6 ,\ '2006/YearTd' u ($1-0.5):($2) w l 8,\ f(x)
|
[an error occurred while processing this directive] [an error occurred while processing this directive] |
The second file, gnu.fit2, plots the data files and f(x)
as before, then runs the function
fit f(x) '2006/YearTd' u ($1-0.5):2 via A,o,Bso as to fit the function f(x) to the average daily data. Note that the syntax of the "u" (using) part is the same as used in the plot. The words via A,o,B instruct the fit function to vary those three parameters so as to optimize the fit of the function f(x) to the specified data. If all is well, this fit function will come up with a good setting of the parameters, report them with error bars and a correlation matrix, the stop; the next command in the gnuplot file replots the function so you can see how it fits. |
[an error occurred while processing this directive] |
gnu.fit3 fits a more complicated model to the half-hourly data - the temperature is modelled as the sum of two sinusoids, one with period one day, and one (as before) with period one year. Here the plot of the function is a fancy 'multiplot' with three parts. To avoid writing everything twice, the details of the plot command are popped into another file, gnu.showTempg, which is loaded whenever we want to make the three-part plot. |
[an error occurred while processing this directive] |
[an error occurred while processing this directive] |
|
gnu.fit4 shows off fit's capabilities by fitting a more complicated model, in which the daily sinusoid has an amplitude that itself varies sinusoidally with a period of one year. To find out more about fit, use ?fit |
[an error occurred while processing this directive] |
If you have a load of points in datafiles and
want to make a movie, there are two ways
with gnuplot.
plot 'datafile' every 10 using 3:5which plots every 10th line in 'datafile', showing column 3 on the x axis and 5 on the y axis. Use ?every to find out the fancier things you can do with every Note that gnuplot calls the top line of a file line zero, not line one. [an error occurred while processing this directive] The example shown here [run it using load 'gnuC02' ] shows a spaceman and his hammer orbitting the earth. The spaceman is in circular orbit. The spaceman has just given his hammer a kick directly away from the earth. |
[an error occurred while processing this directive] [an error occurred while processing this directive] |
There are several ways to make histograms.
For example, you could pop your data into a spreadsheet
and use its built-in histogram function.
However there is a sneaky way to do the histogram entirely
within gnuplot.
To adapt this trick to your problem, change the datafile 'dat' to your data file; change the $1 to point to the correct column number (eg, $2 for column 2); choose the bin_width you want. This example assumes the data is in a file called dat. |
[an error occurred while processing this directive] |