next up previous contents
Next: Getting Hard Copy Up: gnuplot 3.5 User's Guide Previous: Working in the gnuplot

More on Plotting

 

You have already seen the basic plotting commands in Section gif. In this section, you will learn how to create several different kinds of plots with gnuplot.

Two-dimensional Plots

 

gnuplot can create two types of two-dimensional plots. The first is the usual y=f(x) in Cartesian coordinates or tex2html_wrap_inline5715 in polar coordinates. The other type is a parametric curve (e.g.  tex2html_wrap_inline5717 ). Plotting y=f(x) should be a trivial task after going through the previous sections. To plot tex2html_wrap_inline5715 in polar coordinates, you first tell gnuplot to switch to polar coordinates by the command set polar. The syntax for plotting functions in the polar coordinates is exactly the same as that for Cartesian coordinates, except that x is the angle and the value of the function is the radius. For example, to plot the function tex2html_wrap_inline5723 , you can do the following:

  set polar
  plot 1/cos(x)
Note that the default unit for the angle is radians. You can change the unit to degrees by set angle degree. You can change the name of the dummy variable from x to theta by
  set dummy theta
  plot 1/cos(theta)

To switch back to Cartesian coordinates, use the command set nopolar.

Parametric curves are very handy. If you define the x- and y-coordinates as functions of a dummy variable, t, you can plot things like circles, ellipses, and other curves that cannot be expressed in the functional form y=f(x). Try the following examples:

  set parametric
  plot sin(t), cos(t)
  plot 3*sin(t-3), 2*cos(t+2), cos(3*t), sin(2*t)
  set noparametric
The first command tells gnuplot that you want to plot parametric curves. The first plot is a circle (with aspect ratio distortion). The second plot shows an ellipse (the functions tex2html_wrap_inline5733 ), and a strange-looking curve (the functions tex2html_wrap_inline5735 ). Note that up to three ranges can be specified on the plot command, in the order t, x, and y. The command set noparametric switches back to ordinary y=f(x) mode.

You can also plot parametric curves in polar coordinates. In this case you define r=r(t) and tex2html_wrap_inline5747 . Try the following:

  set parametric; set polar
  plot sin(t),cos(t)
Did you guess what the curve will look like?

In parametric polar mode, rrange sets the distance from the origin to the edge, xrange sets the angle, and yrange sets the extent of the plot, which will be square. For instance, try:

  set parametric; set polar
  set rrange [-1:1]; set yrange [-2:2]
  plot t,0

Note that under parametric mode, the range of t can be specified by the set trange command. The syntax is the same as set xrange.

You can set the number of points where each function is to be evaluated by the command set samples. For example:

  set samples 100; plot f(x)
  set xrange [0:-10]; set samples 11; plot g(x)
The first example tells gnuplot to plot f(x) at 100 points equally-spaced in the default x-range. The second example tells gnuplot to plot g(x) at integer-valued x from 0 to -10 (yes, you can reverse the direction that an axis increases just by specifying the endpoints appropriately).

By now you have noticed that each function is listed in the key in the upper right-hand corner of the plot. Perhaps you'd like to specify a title other than the one gnuplot gives you by default. The option is simply title ' tex2html_wrap_inline5637 curve-name tex2html_wrap_inline5639 '. Similarly you can control how the curve is shown by adding the option with tex2html_wrap_inline5637 style tex2html_wrap_inline5639 where tex2html_wrap_inline5637 style tex2html_wrap_inline5639 can be lines, points, impulses, etc. The gnuplot manual has the complete list. If you have several curves on your plot and they are plotted with the same style, gnuplot has several versions of each style which it cycles through. You can override its selections by adding the integral style codes (line, then point) after with tex2html_wrap_inline5637 style tex2html_wrap_inline5639 . (You can see all of the available options by entering the command test.) Try

  set samples n+1; set xrange [0:n]
  mu=5; n=15; p=0.4
  i(x)=int(x+.1)
  pd(x)=mu**x*exp(-mu)/i(x)!
  bd(x)=n!/(i(x)!*(n-i(x))!)*p**x*(1-p)**(n-x)
  plot bd(x) title 'binomial distribution, p=0.4' with linespoints,\
       pd(x) title 'Poisson distribution, mu=5' with impulses
You can leave a function out of the key entirely by replacing title '...' with notitle, and you can eliminate the key completely by the command set nokey.

If you want to plot a bargraph, use with boxes. The width of the bars are controlled by the set boxwidth command. Without any argument, set boxwidth tells gnuplot to calculate the width so that successive bars are adjacent to each other (as in a histogram).

Three-dimensional Plots

 

For three-dimensional plots, gnuplot supports only Cartesian coordinates. Therefore you can only plot surfaces of the type z=f(x,y) or tex2html_wrap_inline5779 . Try the following:

  set hidden3d
  f(x,y)=x**2+x*y-y**2
  splot f(x,y)
  set parametric
  splot u-v**2, u**2*v, exp(v)
  set noparametric
The command set hidden3d turns on the hidden-line removal, which means lines ``behind'' other lines are not drawn. This feature is not available in the MS-DOS version due to memory limitations. The Windows, OS/2 and Unix versions do support this feature, however.

gnuplot can generate contour lines on the xy-plane, on the surface itself, or both. The command is set contour tex2html_wrap_inline5637 place tex2html_wrap_inline5639 , where tex2html_wrap_inline5637 place tex2html_wrap_inline5639 is either base, surface, or both. You can control the contour levels by the command set cntrparm tex2html_wrap_inline5637 options tex2html_wrap_inline5639 . Here are a few examples:

  set cntrparam levels auto 5           # set 5 automatic levels
  set cntrparam levels incr 0,.1,1      # 11 levels from 0 to 1
  set cntrparam levels disc .1,.2,.4,.8 # 4 discrete levels
Please refer to the online help or the gnuplot manual for more advanced options for set cntrparam.

Note that on a color output device, contour lines at different levels are drawn with different colors. On a monochrome display or non-color printer, they are shown in different line styles. Also note that if you specify contours on the surface and turn on the hidden-line removal, the contour lines will not be shown. This is because contour lines are drawn ``underneath'' the surface, not on top of the surface. You can specify set nosurface to plot only the contour lines.

gnuplot cannot generate a contour plot from parametric functions.

The number of values of each coordinate for which the function is evaluated (and hence the number of lines on the surface) is set by the command set isosamples tex2html_wrap_inline5637 x-number tex2html_wrap_inline5639 , tex2html_wrap_inline5637 y-number tex2html_wrap_inline5639 , where tex2html_wrap_inline5637 x-number tex2html_wrap_inline5639 and tex2html_wrap_inline5637 y-number tex2html_wrap_inline5639 are the number of lines to draw along the x- and y-axes. set samples (discussed above) sets the number of points evaluated along each isosample.

Sometimes you may want to look at the three-dimensional surface plot from another direction. This is accomplished by the set view command. The first number following set view changes the rotation angle around the x-axis and the second changes the rotation around the z-axis. The default view is 60 degrees about the x-axis and 30 degrees about the z-axis. For example, try

  set xlabel 'x'; set ylabel 'y'
  splot [0:1] [0,1] x**2*y*(1-y)
  set view 15,75
  replot
Try a few other rotations.

You can make a two-dimensional contour map of a surface by using the settings

  set nosurface
  set contour
  set view 0,0
  replot
but the y-axis is labeled on the right. Different rotations around the z axis may come closer to what you want.

Plotting Data Files

 

gnuplot can produce plots from tabulated data. In this section, you will see how to handle different kinds of data with gnuplot. Note that you can put comments in a data file the same way you can in a gnuplot script file; gnuplot ignores everything on the line after the # symbol.

The essential rule of data organization is that gnuplot reads one data point per line.

Perhaps the simplest data to plot are a series. Suppose you have the data for a variable stored in a column in a plain text file. The command plot ' tex2html_wrap_inline5637 data-filename tex2html_wrap_inline5639 ' will plot the values of the variable as the y-coordinates - the first value with x-coordinate 0, the second value with x-coordinate 1, etc. This is useful for plotting time series data.

If your data really are a time series, you may want a plot where the x-axis is either the day-of-the-week or the month. The commands set xdtics and set xmtics will do these for you. For xdtics, 0 is treated as Sunday, 1 as Monday and so on. Numbers larger than 6 are converted modulo 7. For xmtics, 1 is treated as January, 2 as February and so on. Numbers larger than 12 are converted modulo 12 plus 1.

You can also plot series from a bivariate data set. In this case the first column in the data file is taken as the x-coordinate and the second column is taken as the y-coordinate. This allows you to plot data with scattered x's or series data with a variable step size.

The data file can have many columns. You can tell gnuplot which columns to plot by the using keyword following ' tex2html_wrap_inline5637 data-filename tex2html_wrap_inline5639 ' on the plot command. For example, suppose your data set is stored in the file reg.dat and contains ten columns. Consider the following commands:

  plot 'reg.dat' using 1:2
  plot 'reg.dat' using 2:3, 'reg.dat' using 2:6,\
       'reg.dat' using 8:4 with lines
The first command plots the first column in the file as x and the second column as y (which is the default). The second command overlays three plots: third column (y) versus second column (x), sixth column (y) versus second column (x), and fourth column (y) versus eighth column (x). The first two are plotted with points, and the third with a line.

With the using option, you can now plot data directly from labeled tables prepared by some other program, as long as you remember to put the # at the beginning of each heading line.

When you plot a data set with style lines, a null line (a line of zero length - not a line of spaces) in the data file breaks the line in the plot.

If you wish to put error bars on your plotted data, you need to give gnuplot the error data in either a three-column (x, y, tex2html_wrap_inline5869 ) or a four-column (x, y, tex2html_wrap_inline5875 , tex2html_wrap_inline5877 ) format. You then specify with errorbars. There is also a style boxerrorbars, which requires a fifth column of data containing the box width.

If you want to have control over the width of the bars in a bar graph, the bar width data must be in the fifth column of the data file (or the fifth item in the using list, e.g. using 1:2:2:2:3 for a three-column file) or defined by the command set boxwidth.

If you have lots of data and only want to put error bars on some of them, there is no way to do so directly in gnuplot. You'll need to make a separate file with the subset, or you could edit the data file, setting the errors to zero for those points to be plotted without error bars. If you're running under Unix, you could use a filter like awk to do the same thing.

One nice feature of gnuplot is that it can transform the y values of the data with the thru option of the plot command. For example,

  plot 'reg.dat' thru sqrt(x)
produces the same plot as the first example above, except that the y data are transformed to tex2html_wrap_inline5883 . The function doesn't have to be one built into gnuplot; you can define one yourself. Just remember that the syntax is a bit odd; you plot thru f(x) even though you're really plotting f(y). A transformation can only be applied to the y data in a two-dimensional plot; currently there is no built-in mechanism in gnuplot to transform the x data, nor is there any transformation available for three-dimensional plots. A short discussion of thru can be found in the manual or online help, under plot data-file, which also mentions how to transform both x and y with awk under Unix.

If you are interested in fitting a curve to a set of x-y data, there are several programs (fudgit and gnufitgif, for example) that perform nonlinear least squares fits. Each of these couples easily to gnuplot. If you are looking for more extensive features, check out Octavegif (available for Unix only). Octave is a Matlab-like program that performs many numerical computations. It uses gnuplot as its plotting tool. Thus you can do curve fitting to your data and then pass the result to gnuplot\ from Octave, for instance.

You can create three-dimensional scatter plots as easily as two-dimensional ones. Suppose the data file 3d.dat contains five columns of numerical data. The commands

  splot '3d.dat'
  splot '3d.dat' using 1:4:3
first plot the points (x,y,z) with first column as x, second as y, and third column as z, and then first column as x, fourth column as y, and third column as z.

The file 3d.dat can even contain more than one set of data. If you separate the sets with two null lines, gnuplot will refer to the first set as index 0, the second as index 1, and so on. You tell gnuplot\ which one to plot by placing index and the number immediately after tex2html_wrap_inline5637 data-filename tex2html_wrap_inline5639 . (It is a pity that plot doesn't have this option, too.)

If you want to make a contour plot from a three-dimensional data set, the number of isosamples is set by the data. Your data set must be organized as rasters, tex2html_wrap_inline5917 ,

  x(1)   y(1)   z(1,1)
  x(1)   y(2)   z(1,2)
  x(1)   y(3)   z(1,3)
tex2html_wrap_inline5919
  x(2)   y(1)   z(2,1)
  x(2)   y(2)   z(2,2)
  x(2)   y(3)   z(2,3)
tex2html_wrap_inline5919

and so on. Each raster of constant x-index is terminated by a null line. (The data can be a single column of z's, but the null lines will still be needed.) There must be the same number of points in each raster, but the x and y values don't have to be the same!

If you have data that are not defined in the tidy grid fashion shown above and you want to draw contours anyway, gnuplot has a command set dgrid3d which does the necessary interpolations for you.

The options on the plot and splot commands are order-dependent. The proper sequences are

  plot ranges data-file thru using title style
  plot ranges functions title style
  splot ranges data-file index using title style
  splot ranges functions title style
where we have listed only the options for clarity.

Customizing Your Plot

 

In this section you will see how your plots can be customized further with the set command.

Note that you can check the settings of all the options that the command set controls by the show command. For example, to check the current x range, use the command show xrange.

set logscale makes the specified axis logarithmic. It takes one or two arguments. The first argument can be x, y, xy, or z. The second argument specifies the base of the logarithms and is optional (the default is base 10). set nologscale turns off logarithmic scaling.

set zeroaxis causes the x- and y-axes to be plotted, if the plotting range contains either axis. set nozeroaxis turns off plotting of the axes. The commands set xzeroaxis, set yzeroaxis, set noxzeroaxis and set noyzeroaxis work similarly.

set key tells gnuplot where to put the legend of the plot. For a two-dimensional plot, set key x,y says to put the legend at the point (x,y) in the plot. For a three-dimensional surface plot, the z-coordinate can be specified. The units for x and y are the same as for the plotted data or functions. x and y are Cartesian coordinates even in polar mode.

set label lets you add text to the plot. For example,

  set label 1 'Max' at .5, 2.3 center
  set label 2 'Min' at 1.3, -.3 right
  set label 2 at 1.3, .3
put the text ``Max'' centered at the point (0.5,2.3), and the text ``Min'' right-justified at the point (1.3,-0.3). The number after the keyword label is the identifier for that label. The third sample shows that you can move an identified label to a different position (or change its justification) without retyping the label. The same comments about the position that were made in reference to set key apply here. To turn off label number two, use set nolabel 2. To turn off all labels, use set nolabel.

set arrow can be used to draw arrows or line segments in a plot. For example,

  set arrow 1 from 1,2 to -.5,3
  set arrow 2 to 4,4 nohead
The first command draws an arrow from the point (1,2) to the point (-0.5,3). The second command draws a line segment (no arrow head) from the origin (since the from is omitted) to the point (4,4). The same comments about the position that were made in reference to set key once again apply here. The command set noarrow can be used to turn off one or all arrows, like set nolabel. The identifier works the same way as in set label, too.

set grid causes grid lines to be drawn (in dotted lines) on the plot. For a three-dimensional surface plot, the grid lines are drawn at the base of the plot. set nogrid turns off the grid lines.

set border causes a box to be drawn around the plot (the default setting). To get rid of the box, use set noborder.

set data tex2html_wrap_inline5637 options tex2html_wrap_inline5639 and set function tex2html_wrap_inline5637 options tex2html_wrap_inline5639 can be used to control the default line or point style for plotting data files and functions. These are similar to the with option on plot and splot, but apply to more than one plot. For example, {

  set data style points
  set function style lines
  plot f1(x),f2(x),'data1','data2'
would produce a plot with lines (one solid, one dashed) representing the functions tex2html_wrap_inline5971 and tex2html_wrap_inline5973 and with two different symbols representing the data in the two files.

set tics takes one argument: either in or out. This indicates whether the tic marks are to be plotted inside the box or outside the box.

set xtics gives you control over which x-values are to be given tic marks and how these are to be labeled. There are two syntaxes:

set xtics 0,.5,10
set xtics ('5' 1, ' ' 2, 'Hi, Mom' 4)
The first example will produce labeled tics at 0, .5, 1, 1.5, ..., 9.5, 10. The second will produce three tic marks, one of which will be unlabeled. If no label is specified, the tic mark will be labeled with its x-value. set noxtics does precisely what you think it does. set ytics and set ztics work the same way.

set ticslevel sets the ``height'' of the surface when doing splot. set tickslevel 0 causes the surface to be drawn from the base. Giving a positive argument to set ticslevel ``elevates'' the surface. Negative arguments are not allowed.

set size tex2html_wrap_inline5637 height tex2html_wrap_inline5639 , tex2html_wrap_inline5637 width tex2html_wrap_inline5639 changes the size of the plot. The argument tex2html_wrap_inline5637 height tex2html_wrap_inline5639 and tex2html_wrap_inline5637 width tex2html_wrap_inline5639 are multiples of the default size (which is different for each terminal type). For example, the terminal type postscript has default size 10 inches wide and 7 inches high. The command set size 5./10., 5./7. changes the size to 5 inches by 5 inches. This command is useful for controlling the size of your plot when printing it on paper. But it doesn't scale the plot quite like you'd expect, because size actually scales an area larger than the plot to include the exterior labels. So if you want a plot of a specific size on the paper (for overlays, perhaps), you'll have to experiment. If you are using a windowing system, (e.g. MS-Windows, OS/2, X Windows, etc.) you can change the size of the plot simply by changing the size of the plot window.

The pause command is useful when you are creating several plots with a script file and viewing them on the screen. The commands

  plot f(x)
  pause -1 'Hit <return> for next plot'
  plot g(x)
plot f(x), and then show the message ``Hit <return> for next plot'' on the screen. After you hit the return key, g(x) is plotted. A positive argument to the pause command is taken as the number of seconds to wait before going on to the next command.


next up previous contents
Next: Getting Hard Copy Up: gnuplot 3.5 User's Guide Previous: Working in the gnuplot

Andy Liaw
Tue Jul 16 23:20:34 CDT 1996