Gnuplot
A Wikibookian believes this page should be split into smaller pages with a narrower subtopic. You can help by splitting this big page into smaller ones. Please make sure to follow the naming policy. Dividing books into smaller sections can provide more focus and allow each one to do one thing well, which benefits everyone. |
gnuplot is a command-line driven multiplatform plotting program. Despite the name, it is not associated with GNU project and is not covered by GNU GPL. The source code license is a gratis one, but not a copyleft one; "Permission to modify the software is granted, but not the right to distribute the complete modified source code."[1]
Usage
editgnuplot can be used interactively, in batch mode, or embedded in (scripted by) another program, such as GNU Octave.
- Interactively, run gnuplot at the command line.
- In batch mode, run gnuplot input.plt (where input.plt is the name of the input file) at the command line.
- In another program, use that program’s plotting facilities – gnuplot will be called transparently.
- For use in one’s own programs, one can run gnuplot via popen, or use a library that wraps gnuplot for the programming language. These wrapper libraries exist for C, C++(e.g. Gnuplot-iostream, gnuplot-cpp) Python, Perl, Java, Fortran95, and others.
Example
editAs very simple usage, start gnuplot and type:
plot sin(x) exit
This will display a plot of the sine function, and then exit.
Outline
editTo plot a function or functions:
- define the function;
- determine the range of inputs and outputs;
- determine the style for the region and the graphs;
- plot it (using the plot function)
To plot data, collect the data in a file instead of defining a function.
Format
editPlain
editUnsetting the default decorations will yield a completely plain graph area:
unset title unset key unset xtics unset ytics set border 0
These fields can now be set individually, if desired.
Axes
editPercentage
editTo format an axis as a percentage, multiply the number by 100 and suffix a “%” symbol using format, as in:
set format y "%g %%" plot "dat1.txt" using 1:($2*100)
Types of functions
editPiecewise-defined functions
edit- See: Piecewise function
Several ways are possible.
The ? operator
editOne can plot piecewise-defined functions in gnuplot with the ternary condition operator (?:). For instance, one can manually define the absolute value function by:
f(x) = x > 0 ? x : -x
Read this as “if…then…else”: “if x is greater than 0, then else ”
One can chain these, for instance by:
f(x) = x < 1 ? 1 \ : x < 2 ? 3 \ : 5
This corresponds to the piecewise function
For piecewise functions, you will likely want many samples, so that discontinuities appear as vertical lines, and corners appear sharp, so:
set sample 1001
(Using 1001 instead of 1000 avoids artifacts of having a sample point appear directly on a discontinuity, which can introduce "stair steps.")
plot each function individually
editOr you can plot each function individually, like you appear to be doing now.
f(x) = a1*x**2+b1*x+c1 for x in [t11,t12] f(x) = a2*x**2+b2*x+c2 for x in [t21,t22] f(x) = a3*x**2+b3*x+c3 for x in [t31,t32] .... .... ...
where
t11<=t12<=t21<=t22, etc.
Parametric mode
editBetter yet, switch to parametric mode, map a common t interval [0:1] to your individual t ranges, and then:
x1(t) = t11+t*(t12-t11) ...
plot x1(t), f1(x1(t)), x2(t), f2(x2(t)), ... plot each function individually
Source files
editgnuplot allows one to load files via the load command, or by passing them as arguments on the command line. This is very useful for complicated graphs.
There is no official standard extension, but some semi-official extensions are used:
- .plt, .gnu, .gpi, or .gih for general gnuplot files;
- .dat for data;
- .fnc for function definitions.
For formatting source code, two useful pieces of syntax are:
- comments
- The hash (#) character starts inline comments, which continue to the end of the line.
- line continuation
- A trailing backslash (\) is a line continuation character, and allows one to split a long expression over multiple lines. One can also indent/line up the continuing lines for legibility (as in piecewise functions), as initial whitespace is ignored.
Debugging
editFor debugging a gnuplot file, it is often useful to:
- Change the terminal to interactive (instead of outputting to a file), by commenting out set terminal and output lines.
- Start gnuplot interactively, then load the file in question.
- Put pause -1 (pause until carriage return) at the end of the file, then run it from the command line.
- Alternatively, run gnuplot with the -persist command line switch, so gnuplot exits, but the window persists.
- Make the file itself executable, by shebang (#!) notation (depends on exact path):
#!/usr/bin/gnuplot -persist
Syntax highlighting
editvim has automatic syntax highlighting for gnuplot (gnuplot.vim) as long as the file extension is .gpi
.
Alternatively, other file extensions, such as .plt, can be added in the usual autocommand way:
au BufNewFile,BufRead *.plt,*.gnuplot setf gnuplot
Also see a github repository compatible with pathogen's auto filetype detection features.
Color
editcolorsequence
editSyntax:
set colorsequence {default|classic|podo}
Offline help:
help set colorsequence
- cycle set:
set linetype 1 lc rgb "dark-violet" lw 2 pt 0
set linetype 2 lc rgb "sea-green" lw 2 pt 7
set linetype 3 lc rgb "cyan" lw 2 pt 6 pi -1
set linetype 4 lc rgb "dark-red" lw 2 pt 5 pi -1
set linetype 5 lc rgb "blue" lw 2 pt 8
set linetype 6 lc rgb "dark-orange" lw 2 pt 3
set linetype 7 lc rgb "black" lw 2 pt 11
set linetype 8 lc rgb "goldenrod" lw 2
set linetype cycle 8
9 cycle set:
# https://stackoverflow.com/questions/46775612/colorsequence-for-more-than-8-colors-gnuplot
# Ethan A Merritt - my preference for gnuplot colors
# 2 3 4 5 6 8 are borrowed from the colors_podo set
#
set linetype 1 lc rgb "dark-violet" lw 1
set linetype 2 lc rgb "#009e73" lw 1
set linetype 3 lc rgb "#56b4e9" lw 1
set linetype 4 lc rgb "#e69f00" lw 1
set linetype 5 lc rgb "#f0e442" lw 1
set linetype 6 lc rgb "#0072b2" lw 1
set linetype 7 lc rgb "#e51e10" lw 1
set linetype 8 lc rgb "black" lw 1
set linetype 9 lc rgb "gray50" lw 1
set linetype cycle 9
code
edit- gnuplot/src/getcolor.c
- gnuplot/src/graphics.c
- gnuplot/src/color.c
Generating Wikimedia graphs
editTo generate graphs for Wikimedia:
- Store your code in a file, preferably with comments
- Use high quality (vector graphic) SVG output.
- Generate the SVG.
- Optionally post-process.
- Upload the graph and source code to Wikimedia Commons.
Details follow.
gnuplot options
editHere is a sample header:
set terminal svg enhanced size 300 300 set samples 1001 # high quality set border 31 linewidth .3 # thin border set output "filename.svg"
(The file name should be changed to something more descriptive, though this is not strictly necessary.)
This will create an SVG which is nominally 300 × 300, a common Wikimedia display size: it is easiest to make graphs whose nominal size is the expected display size, but beware that images can and are resized, and that this affects thickness and legibility – if using a large nominal size, so that it will likely be resized down, use large fonts and thick lines.
Beware that with horizontal writing, the y-axis labels will likely take up more space (horizontally) than the x-axis labels take (vertically), and thus a nominally square graph will have an actually graphing area which is slightly taller than it is wide.
One can set the font via:
set terminal svg enhanced size 300 300 fname "Times" fsize 36
Technical details
edit- set terminal svg
- sets output to be an SVG file
- enhanced
- means to use enhanced text output, when Greek letters are needed. a table can be found [here].
- size 300 300
- sets the nominal size of the SVG as 300 × 300
- set samples 1001
- this sets many samples for high quality; the 1001 (instead of 1000) is so that a sample is unlikely to land directly on a pixel or discontinuity, which can cause aliasing. If you have unexpected aliasing, try changing this to 1002 or 1003, as that will move all sample points, possibly fixing the problem.
- set border 31 linewidth .3
- “31” is 1111 in binary, meaning “all borders”; use “3” (0011) in binary for just the lower and left borders. The thin linewidth makes the border less prominent, emphasizing the line. [Note, 31 actually is 11111 in binary. The fifth bit is irrelevant to this example and the example should be rewritten with 15 instead of 31.] Note, use interactive gnuplot command "help set border" to see an explanation of what each bit controls.
- set output "filename.svg"
- This sets the output file name. In normal use, choose a filename more descriptive of file contents.
Post-processing
editOne may wish to post-process the SVG, either in a vector graphics program such as Inkscape, or by hand (as SVG files are text). This can be useful to add annotations which would be otherwise hard to produce in gnuplot, or one may incorporate the plot as one component of a larger or more complex figure.
Wikimedia Commons Upload
edit- Please use the template {{gnuplot}} to flag it as made with gnuplot.
- The source code may be included in the “Source” section of the description (if brief), or more often in a separate == gnuplot source == section.
- The source is most legible if wrapped in Syntax highlighting via:
<syntaxhighlight lang="gnuplot"> ... </syntaxhighlight>
- If you use text, it may be translated – please use the template {{Translation possible}} to indicate that translation is possible.
- Alternatively, minimize the use of text (place in a separate caption) to aid reuse of the image in other languages.
Design considerations
editThere are a number of design considerations in graphs, considered as information graphics. A good resource are the works and writings of information graphic designers, such as the highly regarded works of Edward Tufte: his The Visual Display of Quantitative Information is most relevant for graphs, but his and others other work can be insightful and inspiring.
The first consideration is what to graph, and whether a graph is the best way to convey certain information: graphs can be unexpectedly useful, or conversely, a graph may not be the best way to convey information. Further, how a graph is connected an integrated with other material is a question – is it referred to? Described and discussed?
Other media that can be an alternative to a graph, or support it, include:
- text, either running text, a list, or an isolated single item (pull quote)
- tables
- schematic diagrams
- animations (possibly animated graphs)
- photographs
Often it is useful to portray the same information in several ways.
A second question is how many graphs to use.
- Most obviously one may use a single, large, detailed graph, and this is often appropriate, such as if the details of the data are important.
- Alternatively, consider using small multiples – several small graphs to make a point, through repetition and variation, analogous with written “compare and contrast”.
- It is especially helpful to align graphs or place them on a grid, so the eye can easily switch between them.
- Presenting the same data on different scales can also be revealing; a simple example is shown at estimated sign, where the same data is shown on an absolute scale and relative scale.
- One can also use graphs in-line, as in sparklines.
Beyond these general considerations, there are finer questions:
- Which range of data to display?
- What scale to use? For example, should the range of the graph area agree with the range of the data (maximizing use of space), or should the range be larger, providing context? Often a log scale or log-log scale is appropriate, but may be confusing to novice readers.
- How to distinguish data? What color and line styles to use?
- How prominent to make various data – how thick or thin to make different lines, how large to make text?
Other Wikimedia resources
edit- gnuplot at Wikipedia
- How to create graphs for Wikipedia articles has a section on gnuplot
- The book Ad Hoc Data Analysis From The Unix Command Line has a chapter on Quick Plotting With gnuplot
- commons :
- commons:Category:Gnuplot diagrams has many examples of gnuplot graphs, many with source code.
- Images with Gnuplot src code
References
edit- Information graphics
- Information graphic designers and their work, such as the highly regarded works of Edward Tufte
External resources
edit- gnuplot Central – homepage
- domain_coloring ( gnuplot)
- "gnuplot not so Frequently Asked Questions". Archived from the original on 2012-10-29.
- Janert, Philipp K. (2015), Gnuplot in Action, Second Edition, Manning Publications, New York, USA, p. 425, ISBN 978-1-633430-18-1.
- Phillips, Lee (2012), gnuplot Cookbook, Packt Publishing, p. 220, ISBN 184951724X.