Using Gnuplot

  • View

  • Download

Embed Size (px)

Text of Using Gnuplot

  • 8/12/2019 Using Gnuplot



    Gnuplotis a very powerful plotting program that can generate publication-quality

    images and plots. It creates both 2-dimensional plots and 3-dimensional surface plots.

    works with basic text files with, as far as I know,whitespace-delimited numbers.

    Furthermore, gnuplot works with a number of different file formats for outputting

    graphs, including GIF, JPEG, and PNG images, as well as more LaTeX-friendly formats

    like postscript and encapsulated postscript.

    I tried to lay this out to satisfy two different goals. First, I wanted it to be useful as a

    step-by-step tutorial to ease would-be users into using gnuplot, and second, I wanted it

    to serve as a useful reference (particularly to myself, most likely), which is why I would

    expect the Table of Contents to be rather large.


    Table of Contents

    Why gnuplot?Setting up FontsRunning GNUPLOT and Getting HelpExecuting Basic Shell Commands in GNUPLOT

    Gnuplot Command ShortcutsFunctionsList of operators and functions gnuplot knows about

    Very Basic Plotting (and Replot) in gnuplotPlotting functionsPlotting dataMaking Plots PrettySelecting a TerminalTerminal propertiesTitles and Axis labels

    Controlling How Axes LookSetting Axis RangesSetting Tic LevelsLegendsLabels

    ArrowsPrint Variables in Legends and LabelsMore Advanced PlottingMaking functions look smoother
  • 8/12/2019 Using Gnuplot


    Changing How Lines and Points LookChanging line and point colors / stylesChanging Line Width and Point Size

    Advanced Datafile PlottingPlotting data from different columnsTransforming Plotted Data

    Adding Error BarsTwo X- and/or Y-axes in One Plot

    Surface Plots (3-D plots)Plotting SurfacesMaking Surfaces Smoother and More Complete

    Adjusting the Viewing AngleMaking a Contour PlotColorful SurfacesChanging Color SchemesDefining your own color rangeSurface Plots of Data FilesFitting Functions to Data

    MultiplotMultiplot: Automatic layoutMultiplot: Creating your own layoutMargins

    Why gnuplot?

    You may ask, why pick gnuplot over some of the other plotting alternatives like Excel (o

    some other spreadsheet), origin, matplotlib, etc.? I'll try to list some of the reasons I ca

    think of, starting with the most important:

    1. It is scriptable that is, you can spend a little bit of time getting your graph to loo

    just the way you want it, then you have a script that will work for whatever data se

    you have. Instantly. You only have to spend the time formatting a singlegraph, an

    all other similar ones can be created instantly (and identically) from that template

    2. It has an expansive built-in help function to help you every step of the way

    3. It can print to an almost limitless number of file formats (terminals), so theterminal you want is almost certainly available

    4. It is incredibly flexible, giving you control over almost every aspect of your graph

    5. Once you become comfortable with gnuplot, it is fast and easy to create

    customizable plots (especially once you've built up an army of scripts)

    6. The learning curve isn't nearly as steep as you'd think with it being a command-

  • 8/12/2019 Using Gnuplot


    line-only interface

    7. It's widely used, so there are many, many tutorials and references around for

    gnuplot (including this one).

    8. It's free! Though not actually associated with the GNU project or the FSF (Free

    Software Foundation). It's license is slightly more restrictive, not allowing you to

    actually distribute your own modified versions of gnuplot though you can

    distribute patches to modify certain versions.

    Setting up Fonts

    If you want to expand the fonts that gnuplotknows about when printing image files

    (like GIF, PNG, JPG, etc.), you need to set the GDFONTPATHenvironment variable to

    point to directories that contain font files.

    For Mac OS X, this is typically

    export GDFONTPATH=/Users//Library/Fonts

    Using Cygwin on the PC, a typical choice is

    export GDFONTPATH=/cygdrive/c/Windows/Fonts

    On Linux, I typically keep a .fontsfolder in my home directory with all of my fonts in

    it, so I would set

    export GDFONTPATH=$HOME/.fonts

    Running GNUPLOT and Getting Help

    You can run gnuplot straight from a terminal (command-line) using the command

    gnuplot. If it is available, you should see something like

    bash$ gnuplot

    G N U P L O T

    Version 4.2 patchlevel 6

  • 8/12/2019 Using Gnuplot


    last modified Sep 2009

    System: Linux 2.6.32-36-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

    Send bug reports and suggestions to

    Terminal type set to 'wxt'


    (The terminal may be set to "aqua" on a Mac or "x11" on many other platforms.) This is

    the main interface to Gnuplot. You can use the helpcommand to get interactive help

    while inside the gnuplot interpreter. You can specify a command to get more pointed

    help, too.

    gnuplot> help plot

    `plot` is the primary command for drawing plots with `gnuplot`.

    It creates

    plots of functions and data in many, many ways. `plot` is used

    to draw 2-d

    functions and data; `splot` draws 2-d projections of 3-d

    surfaces and data.

    `plot` and `splot` contain many common features; see `splot` fo


    Note specifically that although the `binary `

    variation does

    work for both `plot` and `splot`, there are small differences

  • 8/12/2019 Using Gnuplot


    between these

    modes. Furthermore, `plot`'s `axes` option does not exist for



    plot {}

    { | {"" {datafile-modifiers}}}

    {axes } {} {with }

    {, {definitions,} ...}

    where either a or the name of a data file enclosed i

    quotes is

    supplied. A function is a mathematical expression or a pair ofmathematical

    expressions in parametric mode. The expressions may be defined

    completely or

    in part earlier in the stream of `gnuplot` commands (see `user-


    It is also possible to define functions and parameters on the`plot` command

    itself. This is done merely by isolating them from other items

    with commas.

    Press return for more:


    Executing Basic Shell Commands in GNUPLOT

    Sometimes you may find yourself wishing you could run a shell command while inside

    gnuplot. Say, for instance, you forgot the name of the data file you wanted to plot, etc

    You can execute some shell commands (like ls), using the !operator first. For instanc

  • 8/12/2019 Using Gnuplot


    gnuplot> !ls

    a.out nctest.f90 test.csv test_debug.f90



    You can also change directories using cd "directory_name"

    Gnuplot Command Shortcuts

    A very helpful thing to know before going too far is that gnuplot is very clever about

    recognizing shortcuts as you become more advanced. Commands only need to be typed

    out enough to make them uniquely identifiable by the interpreter. For instance, becaus

    the quitcommand is the only command that starts with "q" in gnuplot, any of the

    commands (q, qu, qui, or quit) will trigger that action.

    Likewise, the lines

    plot 'datafile' with lines


    plot 'datafile' w l

    are equivalent.


    This section deals with generating mathematical functions, like


    Functions are very useful because you can plot them, fit data to them, and modify data

    using them. You can define a function via the following syntax:

    gnuplot> f(x) = 3*x**2 + 2*x + 1


  • 8/12/2019 Using Gnuplot


    All 1-D functions should take xas its argument if you intend to plot them. If you plot a

    parametric equation, both variables should be functions of t. For instance

    gnuplot> f(t) = 1

    gnuplot> g(t) = t


    plots a straight, vertical line if plotted as plot f(t), g(t), and a straight horizontal

    line if plotted as plot g(t),f(t)

    All 2-D equations take x,yas arguments if you plan to plot those. Additionally, you ma

    define variables as coefficients. That is,


    has three variable coefficients that are not arguments to the function. You can assign

    values to these variables at any point (and change them whenever you want to), or you

    can use them to fit to data. Assign a variable just like you assign an equation with an

    equals sign.

    gnuplot> a = 10


    List of operators and functions gnuplot knows about

    I omit here the operators that are obvious, like +, -, /, and *.


    exponential operator (a ** b is a raised to the power of b)

    lognatural logarithm


    logarithm base 10

    sin, cos, tan

    normal trig functions sine, cosine, tangent

  • 8/12/2019 Using Gnuplot


    sinh, cosh, tanh

    hyperbolic trig functions

    asin, acos, atan

    arc-trig functions

    asinh, acosh, atanh

    arc-hyperbolic trig functions


    gamma function


    complementary error function


    error function

    ceilceiling function (smallest integer greater than