Using GNUPLOT ------------- Gnuplot is a free graphics program that is especially good at making pictures from data stored in files. There is a homepage at http://www.gnuplot.info/ There is a user manual at http://www.gnuplot.info/docs_4.6/gnuplot.pdf DATA FILES: ---------- A data file to be used by gnuplot should have one or more columns of numbers. In a common case, we might have a table of numbers from 1 to 10, along with their squares and square roots, which we could have created with a text editor, or as output from a program: # powers.txt 1 1 1.000 2 4 1.414 3 9 1.732 4 16 2.000 5 25 2.236 6 36 2.449 7 49 2.645 8 64 2.828 9 81 3.000 10 100 3.162 # data.txt # 0.0000 13.5939 2.5000 9.9192 5.0000 6.9152 10.0000 5.5466 15.0000 4.8242 20.0000 4.3478 25.0000 3.9265 # tourists.txt # 1 100 Jan 2 23 Feb 3 37 Mar 4 43 Apr 5 156 May 6 237 Jun 7 255 Jul 8 314 Aug 9 12 Sep 10 20 Oct 11 34 Nov 12 67 Dec PLOTTING A LIST OF X, Y DATA AS A CURVE: --------------------------------------- We can make a plot of the numbers versus their squares like this: gnuplot plot "powers.txt" using 1:2 where "powers.txt" is the name of the file containing the data, and "using 1:2" indicates that we want to use the first column of data for the X axis, and the second for the Y axis. Since this is the default, we can get the same picture by the command: plot "powers.txt" To plot the square roots, we simple ask to see column 1 as the X axis, and column 3 as the Y axis. But first we ask that all subsequent plots connect the data points with lines, so the curve is more visible: set style data linespoints plot "powers.txt" using 1:3 If we want to see a plot of the squares and square roots together, we essentially have to give this command: plot "powers.txt" using 1:2, "powers.txt" using 1:3 but gnuplot will let us abbreviate the second filename to "": plot "powers.txt" using 1:2, "" using 1:3 On the other hand, if you actually do have two files containing data that you want to see on a single plot, we now know how to do it: plot "powers.txt" using 1:2, "data.txt" using 1:2 and again, since we are using columns 1 and 2 in both cases, we could abbreviate this to plot "powers.txt", "data.txt" Now let's terminate this session: quit PLOTTING A LIST OF X, Y DATA AS A HISTOGRAM: ------------------------------------------- Sometimes you want to see a plot using vertical bars. This is common when the data involved represents counting things in specific ranges. It is also helpful when suggesting how an integral is approximated by a Riemann sum. You still need a set of data values x and y, but now x will indicate the point at which a vertical bar will be drawn y units high. GNUPLOT will only outline the bar unless issue the command "set style fill solid". Here's an example with the tourist data: gnuplot First, we can specify some labeling information. set title "Tourists to Apple Beach" set xlabel "Month" set ylabel "Count" We can ask for a grid, to make our data easier to read. set grid <-- (adding grid lines) If we want boxes, we need so set the data style. If we also want those boxes to be filled in with color, we need to set the fill style. set style data boxes <-- (plot data using boxes) set style fill solid <-- (boxes should be filled in) Now we can plot the data plot "tourists.txt" using 1:2 The plot may look a little crowded, since the bars touch each other. We can reduce the width of each bar by a factor of 90% and see if we like that better: plot "tourists.txt" using 1:2:(0.90) <-- (this command makes the boxes skinnier) Since column 3 of the data gives the month, it would be nice to print that under each bar. By adding the "xtic" command, we can do exactly that. plot "tourists.txt" using 1:2:(0.90):xtic(3) <-- Uses column 3 as a label quit PLOT OPTIONS: ------------- set grid will add gridlines to your plot set style data points will display all data with points (default) set style data lines will display all data as connected line segments set style data linespoints will display all data as points and connected line segments set style data boxes will display all data as boxes (histogram) set style fill solid will make boxes a solid color rather than outlines set title "Yearly profit" will title the plot set xlabel "Year" will label the X axis set ylabel "Profit" will label the Y axis set term png will send output to a PNG file instead of the screen set term jpg will send output to a JPEG file instead of the screen set output "name.png" will name the output file "name.png" plot "data.txt" will plot using default data (columns 1 and 2) and default style plot "data.txt" using 1:3 will plot using columns 1 and 3 and default style plot "data.txt" using 1:2, "" using 1:3 plot using columns 1 and 2, and then 1 and 3, on same plot. plot "data.txt" with points will plot using default data (columns 1 and 2) and line style plot "data.txt" with boxes will plot using default data (columns 1 and 2) and box style plot "data.txt" using 1:2:(w) will make the boxes "w" units wide. You NEED the parentheses! plot "data.txt" using 1:2:(w) with boxes will plot columns 1 and 2 with boxes "w" units wide, using boxes, even if the default is points. PLOTTING FUNCTIONS: ------------------ GNUPLOT will accept a formula instead of data: plot sin(x) will plot sin(x), and gnuplot will pick the range plot [0:10] sqrt(x) will plot sqrt(x) for 0 <= x <= 10 plot [0:4] (x+1)*(x-1)*(x-1)*(4-x) will plot this polynomial plot [x=-5:5] [y=-1.2:1.2] erf(x) will plot erf(x) using ranges for x and y