gnuplot basics
Post on 04-Apr-2018
217 Views
Preview:
TRANSCRIPT
-
7/30/2019 Gnuplot Basics
1/5
gnuplot in Action, Philipp K Janert, 2010 by Manning Publications Co.
This document's conventions: gnuplot features are going to be bulleted and
mathematical/data analysis theory without bullet.
Part 1: Basics
A tool for visualizing data and analyzing it using graphical methods.
Chapter 1: Prelude
The art of discovering relationships in data and extracting information from it by visual
means is called graphical analysis. gnuplot reads data from simple text files, with the data arranged in columns.
The plotcommand requires the name of the data file as argument in quotes. The
rest of the command line specifies which columns to use for the plot, and in which
way to represent the data.
The using 1:2declaration tells gnuplot to use the first and second column in the
file called marathon. The final part of the command, with boxes, selects a box
style, which is often suitable to display counts of events.
plot "marathon" using 1:2 with boxes
gnuplot handles most everything else by itself: it sizes the graph and selects the
most interesting plot range, it draws the border, and it draws the tic marks and
their labels. All these details can be customized.
Diffusion limited aggregation (DLA), a process wherein a particle performs a random
walk until it comes in contact with a growing cluster of particles. At the moment of
contact, the particle sticks to the cluster at the location where the contact occurred and
becomes part of the cluster. Now, a new random walker is released to perform a
random walk, until it sticks to the cluster. And so on. DLA clusters are fractals. If you
want to learn more about DLA and similar processes, check out Fractals, Scaling, and
Growth Far From Equilibrium by Paul Meakin (1998).
Setting axes labels and logscales: In gnuplot, all details of a graphs appearance
are handled by setting the appropriate options. To place the labels on the x and y
axes in figure 1.3, I used
set xlabel "Cluster Size"
set ylabel "Run Time [sec]"
Figure 1.4 is drawn using double-logarithmic axes. This is another option, which is
set as follows:
-
7/30/2019 Gnuplot Basics
2/5
set logscale
Plotting Several mathematical functions together: we just list them one after
another on the command line for the plot command:
plot "runtime" using 1:2 title "data" with lines,
1.2*(x/1000)**2.7 title "model"
title directive takes a string as argument, which will be displayed together with
a line sample in the plots key or legend.
Iteration: a crucial aspect of graphical analysis: plotting the data this way and that way;
comparing it to mathematical functions or to other data sets; zooming in on interesting
regions or zooming out to detect the overall trend; applying logarithms or other data
transformations to change its shape; using a smoothing algorithm to tame a noisy data
set; and so on. Ingenuity, intuition, and curiosity are the most important character traits.
Everyone can play this game, if theyre interested in finding out what the data tries totell them.
Chapter 2: Essential gnuplot
If gnuplot is installed on your system, it can usually be invoked by issuing the
command:
gnuplot
Once launched, gnuplot displays a welcome message and then replaces the shellprompt with a gnuplot> prompt. Anything entered at this prompt will be interpreted
as gnuplot commands until you issue an exit or quit command, or type an
end-of-file (EOF) character, usually by hitting Control-D.
define the plot range that we are interested in:
plot [][-2:2] sin(x), x, x-(x**3)/6
The range is given in square brackets immediately after the plot command. The first
pair of brackets defines the range of x values (we leave it empty, since were
happy with the defaults in this case); the second restricts the range of y valuesshown.
Plot of a series of sine functions looked like
sin(x/4), sin(x/2), sin(x), sin(2*), sin(4*x)
-
7/30/2019 Gnuplot Basics
3/5
Plotting from a File
While whitespace-separated numerical data is what gnuplot expects natively, recent
versions of gnuplot can parse and interpret significant deviations from this norm, including
text columns (with embedded whitespace if enclosed in double quotes), missing data,
and a variety of textual representations.
Assuming that the file is called prices, we can simply type
plot "prices"
Since data files typically contain many different data sets, well usually want to
select the columns to be used as x and y values.
This is done through the usingdirective to the plot command:
plot "prices" using 1:2
This will plot the price of PQR shares as a function of time: the first argument to
the usingdirective specifies the column in the input file to be plotted along the
horizontal (x) axis, while the second argument specifies the column for the
vertical (y) axis.
If we want to plot the price of XYZ shares in the same plot
plot "" using 1:2, "" using 1:3
By default, data points from a file are plotted using unconnected symbols. we
need to tell gnuplot what style to use for the data. This is done using the with
directive. Many different styles are available. Among the most useful ones are
with linespoints, which plots each data point as a symbol and also connects
-
7/30/2019 Gnuplot Basics
4/5
subsequent points, and with lines, which just plots the connecting lines, omitting
the individual symbols.
We can do much better by including a title for each data set as part of the plot
command:
plot "prices" using 1:2 title "PQR" with lines,
The title has to come after the using directive in the plot command.
If its missing entirely and the data file contains multiple columns, gnuplot plots
the second column versus the first (this is equivalent to using1:2). If a using
directive is given, but lists only a single column, gnuplot will use this column for
y values and provide x values as integers starting at zero. This is also what
happens when no using is given and the data file contains only a single
column.
Saving and exporting: Gnuplot commands can be saved to a file simply using the
save command:
save "graph.gp"
This will save the current values of all options, as well as the most recent plot
command, to the specified file. This file can later be loaded again using the load
command:load "graph.gp"
The effect of loading a file is the same as issuing all the contained commands.
-
7/30/2019 Gnuplot Basics
5/5
Call command: An alternative to loadis the callcommand, which is similar to load,
but also takes up to 10 additional parameters after the filename to load. The
parameters are available inside the loaded file in the variables $0 through $9.
Strings are passed without their enclosing quotes, and the special variable $#
holds the number of parameters to call. We can use call to write some simple
scripts for gnuplot.
If the commands shown in the listing are placed into a file, this file can be
executed using call and will write the most recent plot to a PNG file and
restore the initial session to its original state:
set terminal push #save the current terminal settings
set terminal png #change terminal to PNG
set output "$0" #set the output filename to the first option
replot #repeat the most recent plot command
set output #restore output to interactive mode
set terminal pop #restore the terminal
Push and Pop Pseudo-terminals: weve used the two pseudo-terminals push
and pop to help with the back-and-forth between the interactive and file
terminals. The former (push) saves the current terminal settings onto a stack; the
latter (pop) restores the terminal to the state saved on the stack. Neither makes
any assumptions about the choice of interactive terminal, and therefore both
can safely be used in scripts that must be portable across architectures. Process of Plotting: Assuming the file shown in the listing was named export.gp
in the current directory, we would call it like this, to write the current plot to a
file called graph.png:
call "export.gp" "graph.png"
Here, both arguments are quoted. Quoting the second argument isnt strictly
necessary, but highly recommended to avoid unexpected parsing of the output
filename. The quotes are stripped before the argument is made available to the
command file in the $0 variable.
Exporting the graph to a file:
plot exp(-x**2)
set terminal png
set output "graph.png"
replot
Always save the commands used to generate a plot to a command file before
exporting to a printable format. This can only be done from the commands savedto file using save, not from plots exported to a graphics file.
top related