| Home > | My UNIX Info > |
to GNU Plot > |
|
|
|
The problem: You want to create a diagram presenting the consumption of
carrots
in
the departments of EEE and CS. Input data: Here are the data stored in a file called carrots.dat:
First column - the year, the second column - values of the
consumption
of carrots in the dept of EEE and the last one in the CS. Columns are
separated
by the TABs (or spaces). |
|
Plotting:
Start GNU Plot just by typing "gnuplot":
| benny:/scratch/archive/acz/www:2466>
gnuplot
G N U P L O T |
As you may have noticed the terminal is set to x11, what means that all the drawing commands will output the results on a window. Please remember this as by changing the terminal type, later, you can output your plots in different formats.
Let's start with the simple plot:
| gnuplot> plot "carrots.dat" |
Now you should see a new window with the points for the dept of EEE. This is because, by default plot command uses the first two columns of your data file.
With this command:
| gnuplot> plot "carrots.dat" using 1:3 |
you can display the data for the other department. But you want them
together, connected with lines:
| gnuplot> plot "carrots.dat" using 1:2 with linespoints, "carrots.dat" using 1:3 with linespoints |
Almost perfect. But still you do not like "carrots.dat" on your
diagram
and you would prefer more meaningfull names:
| gnuplot> plot "carrots.dat" using 1:2 t "Dept of EEE" with linespoints, "carrots.dat" using 1:3 t "Dept of CS" with linespoints |
Let's assume that you also want to specify the year range from 1985
till 2005, and fix the carrot consumption range:
| gnuplot> plot [1985:2005]
"carrots.dat"
using 1:2 t "Dept of EEE" with linespoints, "carrots.dat" using 1:3 t
"Dept
of CS" with linespoints gnuplot> plot [1985:2005] [0:200] "carrots.dat" using 1:2 t "Dept of EEE" with linespoints, "carrots.dat" using 1:3 t "Dept of CS" with linespoints |
Let's assume that you also prefer to have the legend below the
diagram:
| gnuplot> set key below |
(you have to redraw it to see the difference - use up and down arrows to access previous/next commands)
Finally you want to add the title and the axle labels:
| gnuplot> set title "The
comparison
of the consumption of carrots\nby the departments of EEE and CS in
years
1990-2000" gnuplot> set xlabel "Year" gnuplot> set ylabel "Consumption\nof carrots [kg]" |
Please note that I used \n
to
force the new line characters.
(again you have to redraw to see any changes)
OK, let's assume that now you are happy about your plot. It should look like this:

Now you want to obtain a high quality file which you can insert into
your document.
There are many formats supported but I will describe only how to
generate
files in Encapsulated PostScript file format (EPS, .eps).
As I mentioned before you can do this by changing the terminal:
| gnuplot> set terminal postscript eps color |
Also you can specify the name of the output file:
| gnuplot> set output "carrots.eps" |
And plot again, that is type plot
<whatever
you want> or use arrows to find the previous plot line and
execute
it. Nothing should change on your plot window but a new file
"carrots.eps"
should appear in your directory.
If you plot again the result will again be written to the same file,
hence remember to change the output variable for the next plot.
If you want again see your plots in the plot window simply change
the
terminal to x11:
| set terminal x11 |
Now, from another terminal window (so you do not have to exit GNU
plot)
you can try to view the new file with ghostview:
| benny:/scratch/archive/acz/www:2468> ghostview carrots.eps |
Does it look fine ? Hopefully it does.
If you do not need GNU plot anymore:
| gnuplot> quit |
Now you can insert your images to your LaTeX documents or M$ Word or M$ PowerPoint or Star Office. If you use UNIX-like systems (Solaris, Linux, etc.) you should have no problems with handling PostScript or Encapsulated PostScript files. Unfortunately M$ software has very poor support for PostScript. If you have a PostScript printer you should have no problems with printing from M$ software. If not, you may have to print from the ghostview or Acrobat reader.
If you really cannot print your final document in M$ environment,
install
any PostScript printer driver and print to a file. Now convert that
file
into PDF format with e.g. ghostview (choose the device pdfwrite) or
Acrobat
Distiller. Now you should have a PDF version of your document (e.g.
carrot_report2005.pdf)
which you can view or print from an Acrobat Reader on any printer or
e-mail
to someone.
Usefull Hints:
Use a text editor (e.g. nedit) as a scratch pad. Store there all the commands you need to produce plots you want. You can select a few lines with gnuplot commands and paste them in gnuplot window, e.g. by pressing the middle mouse button.
For the example above you can have a text file with:
| set terminal postscript eps
color set output "carrots.eps" set key below set title "The comparison of the consumption of carrots\nby the departments of EEE and CS in years 1990-2000" set xlabel "Year" set ylabel "Consumption\nof carrots [kg]" plot [1985:2005] [0:200] "carrots.dat" using 1:2 t "Dept of EEE" with linespoints, "carrots.dat" using 1:3 t "Dept of CS" with linespoints |
and you select all these lines (even now from your browser window) and paste in gnuplot window.
If you, for example, want to use the carrot data from this page you
can, in a terminal window, type:
| cat > carrot.dat |
select the "carrot.dat" data lines in your web browser, press the
middle
mouse button inside the terminal window with cat command waiting for
any
data. When the data appears simply press Control + D (not C nor Z!) to
close the file.
Last word:
On this page I have described only a tiny subset of GNU plot commands. If you type help or help <topic> (e.g. help plotting) you will get much more information.
That's it.