Transcript
  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    1/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. Permissionis granted to copy, distribute and/or modify this document underthe terms of the GNU Free Documentation License, Version 1.3or any later version published by the Free Software Foundation;with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the sectionentitled GNU Free Documentation License.

    1

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    2/24

    Module: gnuplot the plotting freeware

    gnuplot is a freeware, primarily used for visualization of data, that is, plot-ting. Typically, the data used for plotting could be experimental, or gener-ated on a computer using a programming language; gnuplot itself can alsogenerate 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 thelinks 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 Working with gnuplot

    1.1 Invoking gnuplot

    Open a terminal. Type gnuplot and Enter. You will get the following mes-sage 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 \\

    2

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    3/24

    Terminal type set to wxtgnuplot>

    1.2 Getting help

    Type help in the gnuplot prompt and Enter. You get the following informa-tion.

    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 ofcommands 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

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    4/24

    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> promptback. This help page also lists all the topics on which help is available.For example, if you need help on colornames, typing help colornames fol-lowed by the Enter keystroke on the gnuplot prompt gets you the relevantinformation.

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

    4

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    5/24

    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 first is the interactive mode;in this mode, we can keep giving commands to gnuplot and it executes themimmediately. On the other hand, it is also possible to run it the script mode;in this mode, all the commands are kept in a file and the processing startswith the load command:

    load "./test.plt"

    As noted above, the typical script file 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 totype plot sin(x) at the gnuplot prompt. The plot generated is shown inthe Figure. 2.

    5

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    6/24

    Figure 2: The plot of sin(x).

    6

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    7/24

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

    1.4.2 Plotting sin(x) using a script file

    If we write all the gnuplot commands in a file (say, filename.plt), we canuse the command load "filename.plt" at the gnuplot command prompt

    and obtain the same effect.

    Here is the file for plotting sin(x); note that gnuplot identifies comments bythe use of the # symbol. In data files too, we can use # to write informationabout data for a human reader; gnuplot would disregard these lines whileplotting.

    # This is a comment! Plotting sin(x) with gnuplot

    plot sin(x)

    1.5 Plotting more than one curve in the same figure

    The following script, for example, plots sin(x) and cos(x) in the samefigure, with lines and points; the sin(x) is plotted with filled circles whilecos(x) is plotted with filled squares. In addition, the script also shows howto 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.

    7

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    8/24

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    9/24

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    10/24

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    11/24

    # 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 14971423 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

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    12/24

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    13/24

    The first thing to notice is that gnuplot ignores lines that start with the (#)

    symbol; hence, it is easy to incorporate all the relevant information aboutthe data for a human reader but make gnuplot ignore the same. The secondaspect is that simple plot tells gnuplot to plot the first two columns of thedata with the first 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 thefollowing script. We also draw a line through the data points as a guide tothe 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 aline drawn as a guide to the eye.

    13

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    14/24

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    15/24

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    16/24

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    17/24

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    18/24

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    19/24

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    20/24

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    21/24

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    22/24

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

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

    22

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    23/24

  • 7/28/2019 Gnuplot Tutorial - Gururajan IITB

    24/24

    set pm3d

    set contourset 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 + y2)) with contours

    2 References and further reading gnuplot manual

    See http://www.gnuplot.info for FAQ, documentation and other rel-evant links.

    24