Gnu Plot
Gnu Plot
Introduction
gnuplot (the lower case g is intentional) is a free and open-source scientic data and function plotter for two or three dimensions. It is available for Linux (also Solaris, MacOSX) and Windows. Type gnuplot in a terminal window on a Linux machine (e.g. any IEEE lab machine, or any Linux machine in the Digital Lab) to re it up. The rst thing you probably want to do is to read the on-line help (just type help) - there are lots of examples there. Mostly the commands are common-sense. Useful commands include: plot function will draw the specied function. set xrange [-5:5] sets domain (range of x values) set yrange [-2:4] sets range (range of y values) set grid turns the grid on set nogrid turns the grid off. set autoscale overrides any xrange or yrange settings - scales graph to t. set sample 500 sets the resolution of the plot to 500 points over its width. Increase this value for discontinous functions like 1/x and tan (x), in conjunction with setting yrange to something nite - like [-10:10]. set term type determines where the output goes. type is x11 by default, i.e. display output on the screen. set output "filename" sends any plots to filename. Multiple plots will go to the same le until term is set back to x11 help or help topic e.g. help plot gives some help on various aspects of gnuplot. Just typing help gives you a table of contents. exit, quit or ctrl-D quits. Some useful functions and operators: x**3 is x3 , x**2 is x2 , 2**x is 2x etc +, -, *, / all have their normal meanings () the order of operations can be specied with parentheses 1
0 x
10
Figure 1: The function y = 3x2 4x + 2 log (x) is log to the base e (= 2.71828...) exp (x) is ex sin (x), cos (x), tan (x) are the normal trig functions (similarly for the hyperbolic trig functions) asin (x), acos (x), atan (x) are the inverse trig functions (similarly for the inverse hyperbolic trig functions) You need to put the * to multiply any two terms together (e.g. enter 2*x for the normal mathematical notation 2x there are no cosecant, secant, and cotangent functions - but you can create them yourself by writing: gnuplot> cosec (x) = 1 / sin (x) gnuplot> sec (x) = 1 / cos (x) gnuplot> cot (x) = 1 / tan (x) These are function denitions. Look at the examples in Section 2 to see how they work. Basically wherever gnuplot sees the expression on the left-hand side of the =, it replaces it with the right-hand side. Now, if you say sec (3*x - 1) it will translate to 1 / cos (3*x - 1).
Examples
Some gnuplot examples: gnuplot> plot 3*x**2 - 4*x + 2 gnuplot> set xrange [-2*pi:2*pi] gnuplot> plot sin (x), cos (x) 2
1 0.8 0.6 0.4 0.2 y 0 0.2 0.4 0.6 0.8 1 6 4 2 0 x 2 4 6 sin (x) cos (x)
Figure 3: The straight lines y = 2x 2 and y = 3x + 1. gnuplot> set xrange [-4:1] gnuplot> set zeroaxis gnuplot> plot 2*x - 2, 3*x + 1 gnuplot> set zeroaxis gnuplot> set xrange [-0.5:1.5] gnuplot> plot (2*x - 1) * (1 - x) Now, for something a bit fancier: gnuplot> sec(x) = 1 / cos (x) (defines a function which you can use later) now gnuplot> plot sec (x) 3
0.5 x
1.5
10
0 x
Figure 5: Strange functions involving the sec(x) function which we have dened. gnuplot> plot sec (x**2) gnuplot> plot sec (1 / x) etc. will all work - the results are shown in Figure 5 (plotted on the same graph for convenience). Before plotting, the xrange was set to [5 : 5], the yrange to [10 : 10] and sample was set to 1000 points. It was exported as a .g le and converted to .eps in xg. Some more (plotting these is left as an exercise for the reader :-)
gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> plot plot plot plot plot plot sin (x) / x sin (x**2) asin (sin (x)) (plots a triangular wave) sin (x), (sin (x) > 0) - 0.5 (plots a sine wave and a square wave) "sim_output.dat" with lines (plots user data with lines) "sim_output.dat" using 1:4 with lines (plots columns 1 vs. 4)
etc. If you want to output to a le suitable for inclusion in a lab report or web site, you can do that too. gnuplot supports many different output devices. The default is x11 under X (Linux) or windows for Windows. Check the current setting with show term. Change it to something else with set term.
gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> set term png set output "myplot.png" set xrange [-4:4] set grid plot x**2 set term x11 <<<<<<for PNG file output (e.g. for a web page) the filename range of x values turn the grid on the function to plot back to X windows.
Rather than appearing on the screen, the plot will go to a le called myplot.png in your current working directory (i.e. where you started gnuplot). Sending the output to a PNG le might be useful for a web page, but it wont look very good when printed out on a laser printer, since the le is a raster (bitmap) le. For inclusion in a word processor document (Word, OpenOfce) or LaTeX documents, you probably want to use Encapsulated PostScript (EPS) output - set term eps. Some other alternative term settings include: fig - can be read by XFig (a 2D open-source CAD package, available in the Digital Lab and the IEEE Student Branch Lab. This allows you to conveniently edit the gure (e.g. to add text labels, arrows etc.). From xg you can export to a variety of other le formats. This is recommended if you use LaTeX. dxf - this le type is used by AutoCAD; table - this just dumps the data to a text le which is a table of values - you can import this into Matlab or Excel; For all of these, after setting term, you need to set the output le name.