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”.

1

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>

2

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

use the command ‘show palette colornames‘. For example. You can use these to define the color range spanned by a pm3d palette. This help page also lists all the topics on which help is available. 4 .3 Testing Typing test on the gnuplot prompt gets a gnuplot test window as shown below. After all the information is shown. To see the list of known color names.g. Press return for more: You can continue with reading more information on gnuplot using the Enter keystroke. See ‘set palette‘. e. load "filename" cd ’dir’ although there are some subtle differences \\ (see ‘syntax‘ for more details). typing help colornames followed by the Enter keystroke on the gnuplot prompt gets you the relevant information.marks. you get the gnuplot> prompt back. gnuplot> 1. if you need help on colornames. or to assign a terminal-independent color to a particular linetype or linestyle. ‘linestyle‘.. gnuplot> help colornames Gnuplot knows a limited number of color names.

The first is the interactive mode.plt" As noted above. On the other hand. All you need to do is to type plot sin(x) at the gnuplot prompt. it is also possible to run it the script mode.Figure 1: The test window that pops up on typing test at the gnuplot prompt.plt. in this mode. in this mode. 1. we can keep giving commands to gnuplot and it executes them immediately. all the commands are kept in a file and the processing starts with the load command: load "./test.4 Modes of working One can work with gnuplot in two modes. 1.4. the typical script file is given an extension of . 2. 5 . The plot generated is shown in the Figure.1 Plotting sin(x) in interactive mode Plotting a function in interactive mode is very easy.

Figure 2: The plot of sin(x). 6 .

• Set the y-range (from -1. and plo sin(x) are all equivalent to plot sin(x) and produce the same result. for example. # This is a comment! Plotting sin(x) with gnuplot plot sin(x) 1. pl sin(x). 7 . • Give a title to the plot. p sin(x). gnuplot would disregard these lines while plotting. we can use # to write information about data for a human reader. as long as the abbreviation is unambiguous (that is. plots sin(x) and cos(x) in the same figure.plt" at the gnuplot command prompt and obtain the same effect.1. and. with lines and points.4. for example. in this case). the abbreviation can uniquely identify a single gnuplot command) it would work. In data files too.plt).1 to 1.5 Plotting more than one curve in the same figure The following script. • Set the x-range (from 0 to 2π . the script also shows how to do the following: • Make the plot square. In addition. we can use the command load "filename. Here is the file for plotting sin(x).gnuplot also allows the use of abbreviations. note that gnuplot identifies comments by the use of the # symbol. filename.and y-axes. in this case). 1.2 Plotting sin(x) using a script file If we write all the gnuplot commands in a file (say. • Label the x. the sin(x) is plotted with filled circles while cos(x) is plotted with filled squares.

the points are filled squares is allowed.0 # Let us fix the x-range set xrange[0:2*pi] # Let us fix the y-range set yrange[-1. 8 . cos(x) with linespoints \ pointtype 5 The figure generated by gnuplot is shown in Figure.1:1.# Plotting sin(x) and cos(x) in the same figure # Let us make the plot window square set size ratio 1. if we want a line break. the points are filled circles lines and points. that should backslash plot sin(x) with linespoints pointtype 7. 3.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.

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

They are as given below: grainsize10KperS.dat.7 Plotting data from files In this section. for example.dat.sin(x) Figure 4: The plot of sin(x) with arrow heads. we show how to plot data storied in a file. sin(x) plotted with arrow heads using the command: plot sin(x) with vectors heads. Let us consider the data files grainsize10KperS.1.dat 10 . grainsize100KperS. 4.dat and grainsize1000KperS. In Figure.6 Plotting vectors It is possible to plot curves with arrows. 1.

# # # # # # # # # # 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. Nonisothermal austenite grain growth kinetics in a microalloyed X80 linepipe steel. 12.47 1700 1423 11.0 0. Matthias Millitzer. Nonisothermal austenite grain growth kinetics in a microalloyed X80 linepipe steel.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 .0 0. December 2010.53 763 grainsize100KperS. No. December 2010. Vol.0 0. 3161-3172. 1223 6. 41. Michel Perez.0 0. and Xiang Wang. Matthias Millitzer.52 1497 1423 15. 12.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.54 839 1623 33.60 670 1623 61. and Xiang Wang. 1223 4. Metallurgical and Materials Transactions A. 41.4 0. pp. 3161-3172.52 514 grainsize1000KperS. Vol. Metallurgical and Materials Transactions A. Michel Perez. No. pp.0 0.

12. 41.0 0.42 428 It is possible to plot these data from gnuplot using the plot command. Figure 5: The plot of grain size versus temperature 12 . 1223 4.# # # # # # # The Third column is the standard deviation The fourth column is the number of grains The data is from Kumkum Banerjee. and Xiang Wang. For example. pp. Matthias Millitzer.dat" produces the plot as shown in Figure.2 0. Vol. Nonisothermal austenite grain growth kinetics in a microalloyed X80 linepipe steel. Michel Perez. Metallurgical and Materials Transactions A.62 596 1623 32. 3161-3172. plot "grainsize10KperS. 5.0 0.51 1741 1423 11. No. December 2010.

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

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

That is. in the data file. set xlabel "Temperature (in K)" set ylabel "No of grains" plot "grainsize10KperS.9 Plotting specific columns of the data Suppose we wanted to plot the peak temperature and the number of grains. This can be done using the script shown below. we want to plot the colmuns 1 and 4. 8.1. the data is plotted with errobars.dat" using 1:4 with linespoints \ title "Heating rate: 10 K/s" Figure 8: The plot of grain size versus temperature. The resultant plot is shown in Figure. 15 .

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. \ "grainsize100KperS. 9.dat" \ using 1:2:3 with yerrorbar notitle. 10.dat" \ using 1:2:3 with yerrorbar notitle 1. \ "grainsize1000KperS.dat" with linespoints \ title "Heating rate: 10 K/s". "grainsize1000KperS. set key left top set xlabel "Temperature (in K)" set ylabel "Grain size (in microns)" plot "grainsize10KperS. which generates a plot as shown in Figure.dat" with linespoints \ title "Heating rate: 100 K/s".dat" \ using 1:2:3 with yerrorbar notitle. "grainsize100KperS.dat" with linespoints \ 16 . \ "grainsize1000KperS. The resulting plot is shown in Figure.dat" \ using 1:2:3 with yerrorbar notitle. "grainsize100KperS.dat" \ using 1:2:3 with yerrorbar notitle. The script that generated the figure is also given below for reference.1. \ "grainsize100KperS.dat" with linespoints \ title "Heating rate: 10 K/s". To avoid that. "grainsize10KperS. set xlabel "Temperature (in K)" set ylabel "Grain size (in microns)" plot "grainsize10KperS. "grainsize10KperS.10 Plotting data from more than one file Of course. Here is a script that plots data for all the three heating rates. it is possible to plot data from different data files in the same plot.dat" with linespoints \ title "Heating rate: 100 K/s".dat" with linespoints \ title "Heating rate: 1000 K/s". we can use the command set key left top. 9.

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

The figure generated using the script is shown in Figure.title "Heating rate: 1000 K/s".12 Setting line widths Let us say we want to make the plots and the axes boxes thicker than the default. 11.0 set border linewidth 4 plot sin(x) with lines linewidth 2 notitle 18 . "grainsize1000KperS. It can be done using the linewidth command as shown in the script below. 1. The legend is shifted from its default position at right top to left top. set size ratio 1.dat" \ using 1:2:3 with yerrorbar notitle Figure 10: The plot of grain size versus temperature for various heating rates.

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

The resultant figure is shown in Figure. As an example.1.eps" plot tan(x) with lines linewidth 2 notitle set term wxt replot 1. we show how to plot and save a figure. 12 set term postscript enhanced color set size ratio 1. in Figure. set xrange[-2:2] set yrange[-2:2] set isosamples 41.13. set xrange[-2:2] set yrange[-2:2] f(x.41 f(x.1 3D plots and contours The following set of commands produce the plot of exp(−(x2 + y 2 )) shown in Figure 13. 14 we show the plot of exp(−(x2 + y 2)) with denser isolines generated using the following script.y) The command set isosamples increases the density of grid lines on the surface.y) = exp(-x*x-y*y) splot f(x. the generated figures can be saved using the set terminal type command.y) = exp(-x*x-y*y) splot f(x.0 set border linewidth 4 set output "tanx.13 Saving figures Finally.y) 20 .

30 20 10 0 -10 -20 -30 -10 -5 0 5 10 Figure 12: The plot of tan(x). 21 . The figure was generated in gnuplot and saved (as compared to the other figures in this chapter which were captured from the screen and saved).

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

41 f(x. It is done using the command set contour. it is possible to plot contours below the surface.We can also make the surface show in colour and as a solid instead of meshed as above. 16. 15). Here is the source code that generates a colour coded surface (and the resulting figure is shown in Figure.y) Figure 15: The 3D plot of exp(−(x2 + y 2)) with colour coded surface Finally. 23 . The figure generated by the commands is shown in Figure. set pm3d set xrange[-2:2] set yrange[-2:2] set isosamples 41. For that we use the command set pm3d.y) = exp(-x*x-y*y) splot f(x. The source code for a plot with contour is given below.

info for FAQ. 24 .41 f(x. documentation and other relevant links.gnuplot.y) = exp(-x*x-y*y) splot f(x.y) Figure 16: The 3D plot of exp(−(x2 + y 2 )) with contours 2 References and further reading • gnuplot manual See http://www.set pm3d set contour set xrange[-2:2] set yrange[-2:2] set isosamples 41.

Sign up to vote on this title
UsefulNot useful