To do most plotting with gnuplot, a script file is used that is then interpreted by gnuplot using the command
gnuplot "filename"
This file if composed of the various arguments that you would like to send to gnuplot and can be made in any simple text editor.
The arguments contained in the script file range from viewing and terminal options to the lines that may describe equations to be graphed or files from which data to be graphed is obtained. This page will deal with some of the basic arguments that are needed for plotting and for use in COS323. For more advanced arguments see the on-line gnuplot manual in postscript.
Arguments in gnuplot are interpreted one line at a time. Unlike in C or other languages where a line needs to end with a semicolon, gnuplot interprets the carriage return after each line as the end of an argument.
A few of the basic arguments involve making the settings for the plot that is to generated. One of the first commands to start out with is
set output "filename.ps"
where filename is the name of the postscript file that will be generated when gnuplot is run.
Two more commands control the postscript output that is generated.
set terminal postscript portrait
set size .75,.75
The first of these two commands informs gnuplot of the graphics driver will be showing the output. For most terminals at Princeton this will be set as a postscript portrait. For other types of terminal settings refer to page 31 of the on-line manual. The second command simply tells gnuplot what size to make the postscript image of the data. This is similar to opening ghostview and changing the size of the image using the magstep option. The numbers you use with the set size command depends on your personal preference. As a word of caution though, on some terminals changing the size of the image may result in in text being misplaced or other strange results occurring. Whatever you decide just be sure the size of the image is declared in the format
set size xscale,yscale
Arguments can also be added that to change the layout of a generated graph.
set xlabel "label"
set tics out
set ticslevel number
The first line sets the label that will be placed on the x-axis. Similar commands ylabel and zlabel can be used to set the labels of the y-axis and z-axis respectively.
The next two lines dictate the appearance of tics on the graph. The first decides whether the tics on a graph are drawn inwards (set tics in) or outwards (set tics out). The set tics command can only be used to change the tics to be drawn on the left and bottom borders. The default setting is in.
The third line declares the size of the tics. number must be a non negative number with the default setting 0.5.
One of the most simple ways to generate a plot with gnuplot is to read in a set of data points from some file for gnuplot to graph. This file could be generated by a program or simply by typing points into a file. The point in the file should be structured so that the information for one point is on one line in the file. For example, to describe one point in two dimensions, to numbers should be placed on one line with a space between the numbers.
In order to read in the file use an argument argument similar to
plot[0:100] "filename" using 1:2 with lines
This will read in points from the file filename using columns 1 and 2 to describe the points. The resulting graph will also have an x-axis range of 0 to 100.
The last option in the argument describes how the information will be plotted. In the example above lines was used to generate a plot with lines connecting the points. Other options include points,linespoints,dots, and others that are described in the Data Style section on page 21 of the manual.
If a surface is to be graphed with the given information the command splot will be used instead of plot. An example is shown below.
splot[0:10][0:10][0:5] "input" using 1:2:3 with dots
This will generate a surface with an x-axis range of 0 to 10, a y-axis range of 0 to 10, and a z-axis range of 0 to 5. Again the information describing the points is read in from the file input using columns 1, 2, and 3 and the points are plotted with dots.
A plot can also be generated from an equation that is described in the script file. To do this simply replace input with a call to the function. An example is shown below.
splot[0:10][0:10][0:5] v(R,S) using 1:2:3 with dots
This plots the function v(R,S) as a surface in a similar way as input. In a section following this, the use of equations in the script file is described.
Instead of reading in a set of data points from a file, an equation can be described within the script file. A simple equation is shown below
A(X)=(3*X)+1
This equation can then be graphed by inserting A(X)
in the line with the plot command. instead of a filename. Equations
can also be used to describe other equations. Shown below is an
equation that is declared using ,code.A(X) from above.
B(X)=(2*X)+(A(X))
Variables can also be declared within the script file. These are comparable to #define
's in C.
r = 45
g = 9.81
pi = 3.141592
Shown below is a list describing the general order that types of argument should be placed in when writing a script file for gnuplot.
set output "data_p.ps"
set terminal postscript portrait
set size .75,.75
plot[0:100] "income" using 1:2 with lines
plot[0:100] "deriv" using 1:2 with lines
plot[0:100] "one" using 1:2 with lines, "two" using 1:2 with lines
The script file shown above is an example of one that can be
used for gnuplot. The first line sets the output to the file
The arguments described above are intended to start a person of with a basic idea of plotting in gnuplot. Contained in the manual is a rich set of instructions that can be used to configure gnuplot to various needs.
When graphing data for the various assignments it would probably be best to have a program write data points to a file for gnuplot to read. More then likely you will already have written or need to write a program that handles various functions or their derivatives, so by adding a couple fprintf statements to the program information can be sent to a file. Along with fprintf, the C functions fopen and fclose should also be used to open a file for writing and then to close it.
Week 1 PreceptLast Modified: 97.08.13 Michael Carreno <mcarreno@cs.princeton.edu>