You are on page 1of 24

Computational Approach to Materials Science and Engineering

Prita Pant and M. P. Gururajan January, 2012

Copyright c 2012, Prita Pant and M P Gururajan. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no BackCover Texts. A copy of the license is included in the section entitled GNU Free Documentation License.

Module: gnuplot the plotting freeware


gnuplot is a freeware, primarily used for visualization of data, that is, plotting. Typically, the data used for plotting could be experimental, or generated on a computer using a programming language; gnuplot itself can also generate simple data for plotting. In this chapter, we give a tutorial introductions to working with gnuplot. For further information, we recommend the reader to peruse some of the links listed in the references below. A note: In these notes, sometimes, when the information on the gnuplot terminal is too long to be shown verbatim, we have used two backslashes (\\) to identify the line break introduced by us.

1
1.1

Working with gnuplot


Invoking gnuplot

Open a terminal. Type gnuplot and Enter. You will get the following message and prompt.

G N U P L O T Version 4.2 patchlevel 6 last modified Sep 2009 System: Linux 2.6.38-11-generic Copyright (C) 1986 - 1993, 1998, 2004, 2007 - 2009 Thomas Williams, Colin Kelley and many others Type help to access the on-line reference manual. The gnuplot FAQ is available from http://www.gnuplot.info/faq/ Send bug reports and suggestions to \\ <http://sourceforge.net/projects/gnuplot>

Terminal type set to wxt gnuplot>

1.2

Getting help

Type help in the gnuplot prompt and Enter. You get the following information. gnuplot> help gnuplot is a command-driven interactive function and data plotting \\ program. Any command-line arguments are assumed to be names of files containing gnuplot commands, with the exception of standard X11 arguments, which \\ are processed first. Each file is loaded with the load command, in the \\ order specified. gnuplot exits after the last file is processed. The \\ special filename "-" is used to denote standard input. When no load files \\ are named, gnuplot enters into an interactive mode. See help for \\ batch/interactive for more details. gnuplot is case sensitive (commands and function names written \\ in lowercase are not the same as those written in CAPS). All command names \\ may be abbreviated as long as the abbreviation is not ambiguous. Any \\ number of commands may appear on a line (with the exception that \\ load or call must be the final command), separated by semicolons (;). \\ Strings are indicated with quotes. They may be either single or double quotation \\ 3

marks, e.g., load "filename" cd dir although there are some subtle differences \\ (see syntax for more details). Press return for more: You can continue with reading more information on gnuplot using the Enter keystroke. After all the information is shown, you get the gnuplot> prompt back. This help page also lists all the topics on which help is available. For example, if you need help on colornames, typing help colornames followed by the Enter keystroke on the gnuplot prompt gets you the relevant information.

gnuplot> help colornames Gnuplot knows a limited number of color names. You can use these to define the color range spanned by a pm3d palette, or to assign a terminal-independent color to a particular linetype or linestyle. To see the list of known color names, use the command show palette colornames. See set palette, linestyle. gnuplot>

1.3

Testing

Typing test on the gnuplot prompt gets a gnuplot test window as shown below.

Figure 1: The test window that pops up on typing test at the gnuplot prompt.

1.4

Modes of working

One can work with gnuplot in two modes. The rst is the interactive mode; in this mode, we can keep giving commands to gnuplot and it executes them immediately. On the other hand, it is also possible to run it the script mode; in this mode, all the commands are kept in a le and the processing starts with the load command: load "./test.plt" As noted above, the typical script le is given an extension of .plt. 1.4.1 Plotting sin(x) in interactive mode

Plotting a function in interactive mode is very easy. All you need to do is to type plot sin(x) at the gnuplot prompt. The plot generated is shown in the Figure. 2. 5

Figure 2: The plot of sin(x).

gnuplot also allows the use of abbreviations; for example, p sin(x), pl sin(x), and plo sin(x) are all equivalent to plot sin(x) and produce the same result; as long as the abbreviation is unambiguous (that is, the abbreviation can uniquely identify a single gnuplot command) it would work. 1.4.2 Plotting sin(x) using a script le

If we write all the gnuplot commands in a le (say, filename.plt), we can use the command load "filename.plt" at the gnuplot command prompt and obtain the same eect. Here is the le for plotting sin(x); note that gnuplot identies comments by the use of the # symbol. In data les too, we can use # to write information about data for a human reader; gnuplot would disregard these lines while plotting. # This is a comment! Plotting sin(x) with gnuplot plot sin(x)

1.5

Plotting more than one curve in the same gure

The following script, for example, plots sin(x) and cos(x) in the same gure, with lines and points; the sin(x) is plotted with lled circles while cos(x) is plotted with lled squares. In addition, the script also shows how to do the following: Make the plot square; Set the x-range (from 0 to 2 , in this case); Set the y-range (from -1.1 to 1.1, in this case); Label the x- and y-axes; and, Give a title to the plot.

# Plotting sin(x) and cos(x) in the same figure # Let us make the plot window square set size ratio 1.0 # Let us fix the x-range set xrange[0:2*pi] # Let us fix the y-range set yrange[-1.1:1.1] # Let us label the x-axis set xlabel "x" # Let us label the y-axis set ylabel "The trigomometric function" # Let us give a caption to the plot set title "Cos and Sin" # # # # Let us plot sin(x) with Let us plot cos(x) with Note that no line break be preceded by a single lines and points; the points are filled circles lines and points; the points are filled squares is allowed; if we want a line break, that should backslash

plot sin(x) with linespoints pointtype 7, cos(x) with linespoints \ pointtype 5

The gure generated by gnuplot is shown in Figure. 3.

Figure 3: The plot of sin(x) and cos(x) in the same gure.

1.6

Plotting vectors

It is possible to plot curves with arrows. In Figure. 4, for example, sin(x) plotted with arrow heads using the command: plot sin(x) with vectors heads,sin(x)

Figure 4: The plot of sin(x) with arrow heads.

1.7

Plotting data from les

In this section, we show how to plot data storied in a le. Let us consider the data les grainsize10KperS.dat, grainsize100KperS.dat and grainsize1000KperS.dat. They are as given below: grainsize10KperS.dat 10

# # # # # # # # # #

Grain size data at heating rate of 10 K/s The first column is the peak temperature (K) The second column is the Mean Volumetric Grain Size (in microns) The Third column is the standard deviation The fourth column is the number of grains The data is from Kumkum Banerjee, Matthias Millitzer, Michel Perez, and Xiang Wang, Nonisothermal austenite grain growth kinetics in a microalloyed X80 linepipe steel, Metallurgical and Materials Transactions A, Vol. 41, No. 12, pp. 3161-3172, December 2010.

1223 6.0 0.52 1497 1423 15.0 0.60 670 1623 61.0 0.53 763 grainsize100KperS.dat # # # # # # # # # # Grain size data at heating rate of 100 K/s The first column is the peak temperature (K) The second column is the Mean Volumetric Grain Size (in microns) The Third column is the standard deviation The fourth column is the number of grains The data is from Kumkum Banerjee, Matthias Millitzer, Michel Perez, and Xiang Wang, Nonisothermal austenite grain growth kinetics in a microalloyed X80 linepipe steel, Metallurgical and Materials Transactions A, Vol. 41, No. 12, pp. 3161-3172, December 2010.

1223 4.4 0.47 1700 1423 11.0 0.54 839 1623 33.0 0.52 514 grainsize1000KperS.dat # Grain size data at heating rate of 1000 K/s # The first column is the peak temperature (K) # The second column is the Mean Volumetric Grain Size (in microns) 11

# # # # # # #

The Third column is the standard deviation The fourth column is the number of grains The data is from Kumkum Banerjee, Matthias Millitzer, Michel Perez, and Xiang Wang, Nonisothermal austenite grain growth kinetics in a microalloyed X80 linepipe steel, Metallurgical and Materials Transactions A, Vol. 41, No. 12, pp. 3161-3172, December 2010.

1223 4.2 0.51 1741 1423 11.0 0.62 596 1623 32.0 0.42 428 It is possible to plot these data from gnuplot using the plot command. For example, plot "grainsize10KperS.dat" produces the plot as shown in Figure. 5.

Figure 5: The plot of grain size versus temperature

12

The rst thing to notice is that gnuplot ignores lines that start with the (#) symbol; hence, it is easy to incorporate all the relevant information about the data for a human reader but make gnuplot ignore the same. The second aspect is that simple plot tells gnuplot to plot the rst two columns of the data with the rst column being the x-axis and the second being the y-axis. We can get a plot with labeled axes and appropriate title by executing the following script. We also draw a line through the data points as a guide to the eye. The resultant plot is shown in Figure. 6. set xlabel "Temperature (in K)" set ylabel "Grain size (in microns)" plot "grainsize10KperS.dat" with linespoints title "Heating rate: 10 K/s"

Figure 6: The plot of grain size versus temperature, with labeled axes and a line drawn as a guide to the eye.

13

1.8

Plotting with error bars

Most of the times, the experimental data is typically given along with the errorbars; the current data is no exception and the error is given as standard deviation in column 3 of the data les. It is possible to plot the data with the error bars. Here is a script that would do that. The resultant plot is shown in Figure. 7 set xlabel "Temperature (in K)" set ylabel "Grain size (in microns)" plot "grainsize10KperS.dat" with linespoints \ title "Heating rate: 10 K/s", "grainsize10KperS.dat" \ using 1:2:3 with yerrorbar notitle

Figure 7: The plot of grain size versus temperature; the data is plotted with errobars.

14

1.9

Plotting specic columns of the data

Suppose we wanted to plot the peak temperature and the number of grains. That is, in the data le, we want to plot the colmuns 1 and 4. This can be done using the script shown below. The resultant plot is shown in Figure. 8. set xlabel "Temperature (in K)" set ylabel "No of grains" plot "grainsize10KperS.dat" using 1:4 with linespoints \ title "Heating rate: 10 K/s"

Figure 8: The plot of grain size versus temperature; the data is plotted with errobars.

15

1.10

Plotting data from more than one le

Of course, it is possible to plot data from dierent data les in the same plot. Here is a script that plots data for all the three heating rates. The resulting plot is shown in Figure. 9. set xlabel "Temperature (in K)" set ylabel "Grain size (in microns)" plot "grainsize10KperS.dat" with linespoints \ title "Heating rate: 10 K/s", "grainsize10KperS.dat" \ using 1:2:3 with yerrorbar notitle, \ "grainsize100KperS.dat" with linespoints \ title "Heating rate: 100 K/s", "grainsize100KperS.dat" \ using 1:2:3 with yerrorbar notitle, \ "grainsize1000KperS.dat" with linespoints \ title "Heating rate: 1000 K/s", "grainsize1000KperS.dat" \ using 1:2:3 with yerrorbar notitle

1.11

Shifting legends

We see that the legends are on the right hand side and hence are getting printed on top of the plot itself in Figure. 9. To avoid that, we can use the command set key left top, which generates a plot as shown in Figure. 10. The script that generated the gure is also given below for reference. set key left top set xlabel "Temperature (in K)" set ylabel "Grain size (in microns)" plot "grainsize10KperS.dat" with linespoints \ title "Heating rate: 10 K/s", "grainsize10KperS.dat" \ using 1:2:3 with yerrorbar notitle, \ "grainsize100KperS.dat" with linespoints \ title "Heating rate: 100 K/s", "grainsize100KperS.dat" \ using 1:2:3 with yerrorbar notitle, \ "grainsize1000KperS.dat" with linespoints \ 16

Figure 9: The plot of grain size versus temperature for various heating rates.

17

title "Heating rate: 1000 K/s", "grainsize1000KperS.dat" \ using 1:2:3 with yerrorbar notitle

Figure 10: The plot of grain size versus temperature for various heating rates. The legend is shifted from its default position at right top to left top.

1.12

Setting line widths

Let us say we want to make the plots and the axes boxes thicker than the default. It can be done using the linewidth command as shown in the script below. The gure generated using the script is shown in Figure. 11. set size ratio 1.0 set border linewidth 4 plot sin(x) with lines linewidth 2 notitle

18

Figure 11: The plot of sin(x) with thicker border and thicker lines for the plot.

19

1.13

Saving gures

Finally, the generated gures can be saved using the set terminal type command. As an example, we show how to plot and save a gure. The resultant gure is shown in Figure. 12 set term postscript enhanced color set size ratio 1.0 set border linewidth 4 set output "tanx.eps" plot tan(x) with lines linewidth 2 notitle set term wxt replot

1.13.1

3D plots and contours

The following set of commands produce the plot of exp((x2 + y 2 )) shown in Figure 13. set xrange[-2:2] set yrange[-2:2] f(x,y) = exp(-x*x-y*y) splot f(x,y)

The command set isosamples increases the density of grid lines on the surface; in Figure. 14 we show the plot of exp((x2 + y 2)) with denser isolines generated using the following script. set xrange[-2:2] set yrange[-2:2] set isosamples 41,41 f(x,y) = exp(-x*x-y*y) splot f(x,y) 20

30

20

10

-10

-20

-30 -10

-5

10

Figure 12: The plot of tan(x). The gure was generated in gnuplot and saved (as compared to the other gures in this chapter which were captured from the screen and saved).

21

Figure 13: The 3D plot of exp((x2 + y 2 ))

Figure 14: The 3D plot of exp((x2 + y 2 )) with denser isolines 22

We can also make the surface show in colour and as a solid instead of meshed as above. For that we use the command set pm3d. Here is the source code that generates a colour coded surface (and the resulting gure is shown in Figure. 15). set pm3d set xrange[-2:2] set yrange[-2:2] set isosamples 41,41 f(x,y) = exp(-x*x-y*y) splot f(x,y)

Figure 15: The 3D plot of exp((x2 + y 2)) with colour coded surface

Finally, it is possible to plot contours below the surface. It is done using the command set contour. The source code for a plot with contour is given below. The gure generated by the commands is shown in Figure. 16. 23

set pm3d set contour set xrange[-2:2] set yrange[-2:2] set isosamples 41,41 f(x,y) = exp(-x*x-y*y) splot f(x,y)

Figure 16: The 3D plot of exp((x2 + y 2 )) with contours

References and further reading


gnuplot manual See http://www.gnuplot.info for FAQ, documentation and other relevant links.

24

You might also like