Basic use of Gnuplot



Gnuplot is very easy to use, but it can easily get complicated if you do not know the commands.
Once some of the basic commands are mastered, you can then use the power of gnuplot to make a graph of pretty much anything.
You can even animate the graph, and/or have 3d animations.

The following example is very easy to follow along with, anything much harder is just that much easier to get lost in....

Creating the data file:

Start with a text file containing the data that you want to plot.
Each line in the file represents one point (x-y coordinate pair) that will be drawn on the graph.
The numbers on each line in the file can be separated by any form of white space (tabs or spaces).

The file called 'data' contains the following data:  (you can change this to whatever it is you want to graph)

10      1
20      1.30
30      1.48
40      1.60
50      1.70
60      1.78
70      1.85
80      1.90
90      1.95
100     2.00

How to get Gnuplot to do your bidding:

To use gnuplot simply type 'gnuplot' at the command line. It will then start up in its own command line environment.

The following transcript shows the commands used to plot the data in the file 'data' and save it to a postscript file.

[ethern@cislab ethern]$ gnuplot    #at a command prompt, type in 'gnuplot' to start the program

Terminal type set to 'x11'
--this is the start of the 'gnuplot' command line interface--
gnuplot> set data style linespoints
gnuplot> set grid   
gnuplot> set title "A graph"
gnuplot> set xlabel "x"         
gnuplot> set ylabel "y = log(x)"
gnuplot> plot "data"
gnuplot> set term postscript    
Terminal type set to 'postscript'
Options are 'landscape noenhanced monochrome dashed defaultplex "Helvetica" 14'
gnuplot> set output "graph.ps"
gnuplot> replot
gnuplot> set output 
gnuplot> set term x11
Terminal type set to 'x11'
Options are '0'
gnuplot> exit
--typing 'exit' will get you out when you have had quit enough of the wonderous command line interface--
The commands, and what they mean:
  1. set data style linespoints puts gnuplot into line drawing mode.
  2. set title "A graph", set xlabel "x" and set ylabel "y = log(x)" sets up titles for the graph as well as the x and y axis'.
  3. plot "data" actually draws the graph using the data in the file 'data'. In this case it draws the graph on the screen as the "Terminal type" is set to "x11".
  4. Using  set term postscript puts gnuplot into postscript producing mode.
  5. set output "graph.ps" opens the file graph.ps so that the postscript output can be directed there.
  6. replot redraws the previous graph, but this time to the postscript output file.
  7. set output closes the output file.
  8. set term x11 puts gnuplot back in screen drawing mode.


The generated postscript file can then be viewed using 'ghostview' ( or your favorite .ps viewer).

The wonderous graph this produces:

(this is a .gif file that was converted from the .ps file, so that it can be viewed by the web browser)


 

    If you need help in gnuplot, you can always type in 'help' in the program and it will list the common commands that you can use.  You can also type in 'help topic' where 'topic' is a specific command, and it will give a more detailed description of how to use that specific command.