Gnuplot: Difference between revisions

From csml-wiki.northwestern.edu
Jump to navigation Jump to search
No edit summary
 
(38 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=== Overview ===
=== Overview ===


Gnuplot is a program for plotting scientific data. The resulting figures can be of very high quality, provided that attention is paid to detail. Impressive examples can be found on the [http://www.gnuplot.info/ gnuplot homepage]. This page does not provide a detailed manual for gnuplot, but instead focuses on specific aspects typically accounted in the CSML when creating figures for manuscripts.
Gnuplot is a program for plotting scientific data. The resulting figures can be of very high quality, provided that attention is paid to detail. Impressive examples can be found on the [http://www.gnuplot.info/ gnuplot homepage]. '''This page does not provide a detailed manual for gnuplot''', but instead focuses on specific aspects typically encountered in the CSML when creating figures for manuscripts.


=== PDF terminal ===
=== PDF terminal ===


The version of gnuplot installed on all [[hardware|local machines]] has been compiled with [http://www.pdflib.com/ PDFlib], which makes it possible to directly produce PDF images. Such figures offer the important advantage that they are fully scalable and yet have a small file size. Choose this terminal via
The version of gnuplot installed on all [[hardware|local machines]] has been compiled with [http://www.pdflib.com/ PDFlib], which makes it possible to directly produce PDF images. Such figures offer the important advantage that they are fully scalable and yet have a small file size. Choose this terminal via

<blockquote>set term pdf enh</blockquote>
set term pdf enh [additional options]
set output "filename.pdf"

Sometimes, fine-tuning of the resulting PDF figure is most conveniently done in Adobe Illustrator: each component of the figure will appear as an object that you can move, resize, etc.
Sometimes, fine-tuning of the resulting PDF figure is most conveniently done in Adobe Illustrator: each component of the figure will appear as an object that you can move, resize, etc.


=== Encapsulated PostScript terminal ===
=== Encapsulated PostScript terminal ===


Often when the output of a gnupot script requires further formatting it is easiest to do so using the [http://en.wikipedia.org/wiki/Encapsulated_PostScript Encapsulated PostScript] (EPS) terminal. This is essentially the same as the PostScript terminal except that it contains some extra lines that allow it to interface with other programs. Invoke the EPS terminal as follows:
Often when the output of a gnupot script requires further formatting it is easiest to do so using the [http://en.wikipedia.org/wiki/Encapsulated_PostScript Encapsulated PostScript] (EPS) terminal. This is essentially the same as the PostScript terminal except that it contains some extra lines that allow it to interface with other programs. Invoke the EPS terminal via


<code>set term eps [options]<br />set output filename.eps</code>
set term post eps enh color [additional options]
set output "filename.eps"


For possible options see the instructions for the PostScript terminal on [http://www.gnuplotting.org/output-terminals/ this page].
For possible options see the instructions for the PostScript terminal on [http://www.gnuplotting.org/output-terminals/ this page]. One advantage of .eps files is that they can be edited directly in a text editor. See [[#eps-edit|below]] for an example.
Once the eps file has been formatted properly, you can convert it to PDF format using [http://www.ctan.org/pkg/epstopdf epstopdf]. Executing the command


epstopdf filename.eps
To edit the resulting .eps file it is convenient to use [http://www.grymoire.com/unix/sed.html sed]. For example to change all instances of 0.0 to 0 one would use


<code>sed -i 's/(0.0)/(0)/g' filename.eps</code>
will create the PDF file <code>filename.pdf</code>.


=== Formatting labels ===
Once you have your eps file exactly as you like it you can convert it to PDF format using epstopdf. Executing the command


When choosing for instance an axis label via
<code>epstopdf filename.eps</code>


set xlabel "textlabel"
will create the PDF file <code>filename.pdf</code>.


it is straightforward to use simple text, however often special formatting is necessary. A brief set of rules follows. For italic script place
=== Using symbols in labels ===

{/Helvetica-Oblique [text]}

inside the quotes, where <code>[text]</code> is what you wish to have in italics. All variables should be in italics. For greek letters use

{/Symbol [text]}

where <code>[text]</code> is any character off of the list linked [http://mathewpeet.org/lists/symbols/ here]. For italic Greek characters use

{/Symbol-Oblique [text]}

In emacs, one can use the shorthand <code>M-x ho</code> for Helvetica-Oblique and <code>M-x so</code> for Symbol-Oblique by inserting the following lines into your .emacs file.

(fset 'ho
(lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ("{/Helvetica-Oblique }^[OD" 0 "%d")) arg)))
(fset 'so
(lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ("{/Symbol-Oblique }^[OD" 0 "%d")) arg)))

More instructions are available [http://www.gnuplotting.org/manpage-gnuplot-4-6/#Q1-1-42 here].


=== Sample usage script===
=== Sample usage script===
Line 33: Line 57:
The following is a sample gnuplot script. Everything in braces should be replaced. Again, see the [http://www.gnuplot.info/ gnuplot homepage] or appropriate [http://www.gnuplotting.org/ tutorial] for more comprehensive usage.
The following is a sample gnuplot script. Everything in braces should be replaced. Again, see the [http://www.gnuplot.info/ gnuplot homepage] or appropriate [http://www.gnuplotting.org/ tutorial] for more comprehensive usage.


set term post eps enh color {dashed|solid} lw 2
<code>
set output "filename.eps"
set term post eps enh color {dashed|solid} lw 2<br />
set encoding iso_8859_1
set output "filename.eps" <br />
set key top right
set encoding iso_8859_1 <br />
set format x "%3.2f" #format tics to have max 3 digits and
set key top right <br />
set format x "%3.2f" #format tics to have max 3 digits and<br />
set format y "%3.2f" #max 2 digits after the decimal
set xlabel "{xLabel}"
set format y "%3.2f" #max 2 digits after the decimal<br />
set xlabel "{xLabel}" <br />
set ylabel "{yLabel}"
set ylabel "{yLabel}" <br />
set xrange [{xmin}:{xmax}]
set xrange [{xmin}:{xmax}] <br />
set yrange [{ymin}:{ymax}]
plot "datafile.dat" using 1:2:3 with errorbars
set yrange [{ymin}:{ymax}] <br />
plot "datafile.dat" using 1:2:3 with errorbars
</code>


=== General usage tips ===
=== General usage tips ===


<ul>
* Journals typically prefer Helvetica as the typeface for figures.
<li>Journals typically prefer Helvetica as the typeface for figures.</li>
<li><span id="eps-edit"></span>The origin should be marked with '0', not '0.0'. If you produce a figure directly in PDF format, this can be fixed in Adobe Illustrator. However, it is easier to use the [[#Encapsulated PostScript terminal|EPS terminal]] and then edit the resulting .eps file using [http://www.grymoire.com/unix/sed.html sed]:
<pre>
sed -i 's/(0.0)/(0)/g' filename.eps
</pre>
</li>
<li>Gnuplot accepts abbreviated commands: you just have to enter enough characters to uniquely resolve a command. For example, it is acceptable to write
<pre>
plot "file.dat" us 1:2:3 w err
</pre>
instead of
<pre>plot "file.dat" using 1:2:3 with errorbars
</pre>
</li>
<li>Gnuplot can plot compressed data files by sending the data through a pipe:
<pre>
plot '< gzip -dc data.gz'
</pre>
</li>
<li>To plot the average of columns that are spread over multiple files (but have the same number of lines in each file), take advantage of the [[Command-line_interface_on_Linux/UNIX#paste|paste]] command in Unix to combine them in single file. For example, if three files each contain two columns (x y), use
<pre>
paste file1.dat file2.dat file3.dat > combined.dat
</pre>
and then within gnuplot
<pre>
plot "combined.dat" us 1:(($2+$4+$6)/3)
</pre>
This can even be done on the fly by using a pipe:
<pre>
plot '< paste file1.dat file2.dat file3.dat' using 1:(($2+$4+$6)/3)
</pre>
<li>Gnuplot can histogram data without the need for preprocessing:
<pre>
binwidth=[binwidth]
bin(x,width)=width*floor(x/width) + binwidth/2.0
set boxwidth binwidth
plot "file.dat" using (bin($[colnum],binwidth)):(1.0) smooth freq with boxes
</pre>
where the user should replace <tt>[binwidth]</tt> with the desired bin width and <tt>[colnum]</tt> with the column number of the data to be binned.
</ul>

=== Executable version for OS X ===

Precompiled versions of Gnuplot are provided [[Binary versions of Gnuplot for OS X|here]].

Latest revision as of 08:29, 6 May 2018

Overview

Gnuplot is a program for plotting scientific data. The resulting figures can be of very high quality, provided that attention is paid to detail. Impressive examples can be found on the gnuplot homepage. This page does not provide a detailed manual for gnuplot, but instead focuses on specific aspects typically encountered in the CSML when creating figures for manuscripts.

PDF terminal

The version of gnuplot installed on all local machines has been compiled with PDFlib, which makes it possible to directly produce PDF images. Such figures offer the important advantage that they are fully scalable and yet have a small file size. Choose this terminal via

 set term pdf enh [additional options]
 set output "filename.pdf"

Sometimes, fine-tuning of the resulting PDF figure is most conveniently done in Adobe Illustrator: each component of the figure will appear as an object that you can move, resize, etc.

Encapsulated PostScript terminal

Often when the output of a gnupot script requires further formatting it is easiest to do so using the Encapsulated PostScript (EPS) terminal. This is essentially the same as the PostScript terminal except that it contains some extra lines that allow it to interface with other programs. Invoke the EPS terminal via

 set term post eps enh color [additional options]
 set output "filename.eps"

For possible options see the instructions for the PostScript terminal on this page. One advantage of .eps files is that they can be edited directly in a text editor. See below for an example. Once the eps file has been formatted properly, you can convert it to PDF format using epstopdf. Executing the command

 epstopdf filename.eps

will create the PDF file filename.pdf.

Formatting labels

When choosing for instance an axis label via

 set xlabel "textlabel"

it is straightforward to use simple text, however often special formatting is necessary. A brief set of rules follows. For italic script place

 {/Helvetica-Oblique [text]}

inside the quotes, where [text] is what you wish to have in italics. All variables should be in italics. For greek letters use

 {/Symbol [text]}

where [text] is any character off of the list linked here. For italic Greek characters use

 {/Symbol-Oblique [text]}

In emacs, one can use the shorthand M-x ho for Helvetica-Oblique and M-x so for Symbol-Oblique by inserting the following lines into your .emacs file.

 (fset 'ho
    (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ("{/Helvetica-Oblique }^[OD" 0 "%d")) arg)))
 (fset 'so
    (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ("{/Symbol-Oblique }^[OD" 0 "%d")) arg)))

More instructions are available here.

Sample usage script

The following is a sample gnuplot script. Everything in braces should be replaced. Again, see the gnuplot homepage or appropriate tutorial for more comprehensive usage.

set term post eps enh color {dashed|solid} lw 2
set output "filename.eps"
set encoding iso_8859_1
set key top right
set format x "%3.2f" #format tics to have max 3 digits and
set format y "%3.2f" #max 2 digits after the decimal
set xlabel "{xLabel}"
set ylabel "{yLabel}"
set xrange [{xmin}:{xmax}]
set yrange [{ymin}:{ymax}]
plot "datafile.dat" using 1:2:3 with errorbars

General usage tips

  • Journals typically prefer Helvetica as the typeface for figures.
  • The origin should be marked with '0', not '0.0'. If you produce a figure directly in PDF format, this can be fixed in Adobe Illustrator. However, it is easier to use the EPS terminal and then edit the resulting .eps file using sed:
    sed -i 's/(0.0)/(0)/g' filename.eps
    
  • Gnuplot accepts abbreviated commands: you just have to enter enough characters to uniquely resolve a command. For example, it is acceptable to write
    plot "file.dat" us 1:2:3 w err
    

    instead of

    plot "file.dat" using 1:2:3 with errorbars
    
  • Gnuplot can plot compressed data files by sending the data through a pipe:
    plot '< gzip -dc data.gz'
    
  • To plot the average of columns that are spread over multiple files (but have the same number of lines in each file), take advantage of the paste command in Unix to combine them in single file. For example, if three files each contain two columns (x y), use
    paste file1.dat file2.dat file3.dat > combined.dat
    

    and then within gnuplot

    plot "combined.dat" us 1:(($2+$4+$6)/3)
    

    This can even be done on the fly by using a pipe:

    plot '< paste file1.dat file2.dat file3.dat' using 1:(($2+$4+$6)/3)
    
  • Gnuplot can histogram data without the need for preprocessing:
    binwidth=[binwidth]
    bin(x,width)=width*floor(x/width) + binwidth/2.0
    set boxwidth binwidth
    plot "file.dat" using (bin($[colnum],binwidth)):(1.0) smooth freq with boxes
    

    where the user should replace [binwidth] with the desired bin width and [colnum] with the column number of the data to be binned.

Executable version for OS X

Precompiled versions of Gnuplot are provided here.