0% found this document useful (0 votes)
114 views18 pages

A Collection of Gnuplot Examples - Alvinalexander

The document provides examples and instructions for using the Gnuplot graphing software. It covers how to install Gnuplot, plot simple graphs from data files, add titles and labels, plot multiple curves on one graph, and more. Code samples and explanations are given for the basic plotting commands.

Uploaded by

1113 ForçaBruta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views18 pages

A Collection of Gnuplot Examples - Alvinalexander

The document provides examples and instructions for using the Gnuplot graphing software. It covers how to install Gnuplot, plot simple graphs from data files, add titles and labels, plot multiple curves on one graph, and more. Code samples and explanations are given for the basic plotting commands.

Uploaded by

1113 ForçaBruta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

27/08/2016 A collection of Gnuplot examples | alvinalexander.

com

alvin alexander

“Just Be”
a mindfulness reminder
app for Android
 

A collection of Gnuplot examples


categories By Alvin Alexander. Last updated: June 24 2016

alaska (25) I needed to use Gnuplot


android (138) a little bit over the last Table of Contents
best practices (63)
few days, mostly to
career (50)
create 2D line charts, Installing gnuplot
colorado (21)
cvs (27) and these are my brief Sample data files
design (33) notes on how to get
drupal (120) Starting gnuplot
started with Gnuplot. If
eclipse (6)
funny (3) you haven’t used it Simple plotting
gadgets (108) before, it’s a pretty
git (15) Titles, labels, legend, arrows
amazing tool.
intellij (4)
java (429)
Multiple curves on one plot
jdbc (26)
Jumping right in ...
Multiple graphs (multiplot)
swing (74)
jsp (9) ASCII plotting
latex (26) Installing gnuplot
linux/unix (289) Plotting formulas
mac os x (315)
To get started, you can
mysql (54) Grid, tickmarks, axis ranges,
ooa/ood (11)
use MacPorts or log
perl (156)
http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 1/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com
perl (156) Homebrew to install Comment and column-
php (97)
Gnuplot on Mac OS X separator characters
postgresql (17)
programming (43) systems: Run shell commands
ruby (56)
scala (640) Session management
sencha (23) port install gnuplot
servlets (10) Real-world example
brew install gnuplot
technology (84)
testing (13) Resources
uml (24)
Note that with OS X Help
zen (47)
Yosemite (10.10.x) I had
to use this brew
command instead:
Table of Contents

brew install gnuplot --with-qt

You’ll know that you need that command if you get this error message when you try to
run a plotcommand inside the gnuplotcommand line:

WARNING: Plotting with an 'unknown' terminal.


No output will be generated. Please select a terminal with 'set terminal'.

You can find more information about the need for this new brewcommand at this SO
link.

Sample data files


The examples below use the following 2-column and 4-column data files:

# sample 2-column data file


# -------------------------
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 2/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

9 81
10 100

My four-column file is named 4col.csv:

1, 1, 2, 5
2, 4, 4, 10
3, 9, 6, 15
4, 16, 8, 20
5, 25, 10, 25
6, 36, 12, 30
7, 49, 14, 35
8, 64, 16, 40
9, 81, 18, 45
10, 100, 20, 50

Note that the columns in the first file are separated by whitespace, and the columns in the
second file are separated by commas (a CSV file). The latest version of Gnuplot works
with both formats without requiring you to specify a column-separator.

Starting gnuplot

Start Gnuplot from your Mac Terminal:

$ gnuplot

gnuplot>

It prompts you with gnuplot>as shown, but I won’t show that prompt in the examples
below.

Simple plotting

Plotting the data from a two-column file is easy:

plot '2col.dat'

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 3/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

This creates the following graph:

Key points about this basic command:

Assumes col1=x, col2=y; shows ’+’ at data points


Does not connect points with a line
Opens plot in an ’AquaTerm’ on Mac OS X

From here you can do all sorts of fun things:

# simple plotting
plot '2col.dat' # assumes col1=x, col2=y; shows '+' at da
plot '2col.dat' with lines # connect points with a line
plot '2col.dat' with linespoints # line and points
plot '2col.dat' with points # just points (default)

# plot a subset of the data


plot[1:5] '2col.dat' with linespoints # plot the first 5 elements
plot[3:7] '2col.dat' with linespoints # plot only elements 3 thru 7

# add a title to your line

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 4/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

plot '2col.dat' with lines title 'my curve' # this is really the line-title in the le

# map the columns to the x- and y-axes


plot '2col.dat' using 1:2 # 1=x, 2=y (this is the default)
plot '2col.dat' using 2:1 # 2=x, 1=y (reverse the graph)

# abbreviations
plot '2col.csv' u 1:2 w l title 'Squared' # 'u' - using, 'w l' - with lines

This command

plot '2col.dat' with lines

creates this plot:

And this command:

plot '2col.dat' with linespoints

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 5/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

creates this plot:

Titles, labels, legend, arrows

You can adorn your plots with titles, labels, legend, arrows, and more:

set title 'Hello, world' # plot title


set xlabel 'Time' # x-axis label
set ylabel 'Distance' # y-axis label

# labels
set label "boiling point" at 10, 212

# key/legend
set key top right
set key box
set key left bottom
set key bmargin
set key 0.01,100

set nokey # no key

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 6/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

# arrow
set arrow from 1,1 to 5,10

Multiple curves on one plot

To show multiple curves on one plot, use the 4col.csv file:

1, 1, 2, 5
2, 4, 4, 10
3, 9, 6, 15
4, 16, 8, 20
5, 25, 10, 25
6, 36, 12, 30
7, 49, 14, 35
8, 64, 16, 40
9, 81, 18, 45
10, 100, 20, 50

One curve using first two columns:

plot '4col.csv' with lines

Multiple curves:

plot '4col.csv' using 1:2 with lines, '4col.csv' using 1:3 with lines
plot '4col.csv' using 1:2 with lines, '4col.csv' using 1:3 with lines, '4col.csv' using

The second command shown creates this chart:

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 7/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

Add a legend:

plot '4col.csv' using 1:2 with lines title 'Square', '4col.csv' using 1:3 with lines tit

Use abbreviations:

plot '4col.csv' u 1:2 w l title 'Square', '4col.csv' u 1:3 w l title 'Double'

Multiple formulas:

plot sin(x) title 'Sine Function', tan(x) title 'Tangent'

Multiple graphs (multiplot)

How to show multiple graphs in the output:

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 8/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

set multiplot # multiplot mode (prompt changes to 'multiplot')


set size 1, 0.5

set origin 0.0,0.5


plot sin(x), log(x)

set origin 0.0,0.0


plot sin(x), log(x), cos(x)

unset multiplot # exit multiplot mode (prompt changes back to 'gnupl

That series of commands creates this chart:

ASCII plotting
You can create ASCII plots in your Mac Terminal window:

set terminal dumb


plot cos(x)
plot sin(x) w lines cos(x) w lines

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 9/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

Plotting formulas

It’s fun and easy to plot formulas with Gnuplot:

plot sin(x)
plot sin(x)/x

plot cos(x)
plot cos(x)/x

plot sin(x) title 'Sin', tan(x) title 'Tangent'

Plot your own formulas:

f(x) = sin(x) + tan(x)


plot f(x) with points
plot f(x) with filledcurves
plot f(x) with filledcurves above x1

Grid, tickmarks, axis ranges, log


It can be nice to have a grid on a chart, and it can also be nice to control the graph
tickmarks, ranges, and origin:

# grid
set grid

# ranges
set autoscale # let gnuplot determine ranges (default)
set xrange [1:10]
set yrange [1:100]
set xr [0.0:10.0]
set yr [0:5]

# tickmarks

set xtics (1, 5, 10)


set ytics (1, 25, 50, 75, 100)

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 10/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

set xtic auto # set xtics automatically


set ytic auto # set ytics automatically
unset xtics

set ytics 400


set ytics (0,200,400,600,800,1000,1200)
set y2tics (-100,0,100)
set mytics 4
set mxtics 5

set xzeroaxis lt -1
set x2zeroaxis lt -1

# logarithmic scale
set logscale
set logscale y

Comment and column‑separator characters

Set gnuplot file comment character(s):

set datafile commentschars "//"

Set gnuplot file column separator:

# see http://gnuplot.sourceforge.net/docs_4.2/node173.html

set datafile separator "\t"


set datafile separator ","
set datafile separator "|"

set datafile separator {"<char>" | whitespace}

Run shell commands

You can run shell commands from the Gnuplot command line:

# run shell command


!cat 2col.dat

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 11/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

Session management

When you work from the Gnuplot command line, you’re working in a session:

reset # reset everything


replot # re-plot your data after making changes

Un-set the key/legend and re-plot:

unset key
replot

Real‑world example

Here’s a quick real-world example. I had this dataset of my blood pressure and heart rate
from yesterday:

# time, sys, dia, hr


8.5, 112, 60, 52
9, 116, 73, 59
10.5, 127, 71, 58
11, 124, 69, 62
11.5, 117, 68, 60
12, 122, 73, 60
13, 121, 67, 62
15, 120, 78, 68
15.5, 134, 70, 96
16, 120, 72, 73
16.5, 114, 68, 72
22, 119, 69, 61

I then used this sequence of commands (including some trial and error that’s not shown):

set grid
set title 'BP and Heartrate'
set yrange [50:160]

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 12/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

set xlabel 'time (military)'


set label 'finished walk' at 15, 140
unset label
set label 'finished walk' at 15, 105
plot 'bp-hr.dat' u 1:2 w lp t 'systolic', 'bp-hr.dat' u 1:3 w lp t 'diastolic', 'bp-hr.d

to create this graph of my blood pressure and heart rate:

It would be better to put the blood pressure on the y-axis on the left, and the heart rate on
the y-axis on the right, but I’m short on time, and haven’t learned how to do that yet.

Resources
I mostly learned about Gnuplot from the following resources:

http://gnuplot.sourceforge.net/demo/
http://www.cs.hmc.edu/~vrable/gnuplot/using-gnuplot.html

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 13/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

http://research.physics.illinois.edu/ElectronicStructure/498-
s97/comp_info/gnuplot.html
http://people.duke.edu/~hpgavin/gnuplot.html
http://gnuplot-tricks.blogspot.com/
http://lowrank.net/gnuplot/datafile-e.html
http://www.helsinki.fi/~jalaaman/gnuplot/index.html

Help

Help commands:

help
help terminal

Other notes:

Don’t type a blank space after the line continuation character, ”\"
Your data may be in multiple data files

More to come ...

category: technology
tags: plot graphics gnuplot chart

related

JavaPlot

Always visualize data

Example of a radar chart

How to create an X/Y graph with JFreeChart and Scala Swing

Microsoft FrontPage (product review)

Hey, there are 226 Flash cookies on my computer

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 14/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

books i’ve written

what’s new

How to use the Android 7 split screen feature (tutorial)

An accidental finding about meditation and sleep

Symbols and experience do not follow the same rules

An RxScala example

Marble diagrams

The ol’ cabin in Talkeetna

Submitted by alvin on January 7, 2016 - 7:39pm

Permalink

More plot examples, including margins

As a “note to self,” I just used these commands to create some charts for my Android
Football Game. I think the use of margins is one thing I haven’t shown before.

#
# Off Tackle Runs
#
http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 15/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

reset
set lmargin 10
set rmargin 10
set tmargin 10
set bmargin 10
set title 'Off-Tackle Runs'
set xlabel 'Yards Gained'
set ylabel 'Percent Chance'
set xrange [-5:25]
set yrange [0:25]
set key top right
plot 'RunOffTackle_BaseD.dat' w lp title 'Base D', 'RunOffTackle_Nickel.dat' w lp titl

#
# Slant Passes
#
reset
set lmargin 10
set rmargin 10
set tmargin 10
set bmargin 10
set title 'Slant Passes'
set xlabel 'Yards Gained'
set ylabel 'Percent Chance'
set xrange [0:30]
set yrange [0:15]
set key top right
plot 'Slant_Base.dat' w lp title 'Base D', 'Slant_Nickel.dat' w lp title 'Nickel', 'Sl

Reply

Add new comment


Your name

Email

The content of this field is kept private and will not be shown publicly.

Homepage

Subject

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 16/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

Comment

About text formats


Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start
type> <li> <pre>
Lines and paragraphs break automatically.

By submitting this form, you accept the Mollom privacy policy.

Save Preview

Links:
front page me on twitter search privacy

java
java applets
java faqs
misc content
java source code
test projects
lejos

Perl
perl faqs
programs
perl recipes
perl tutorials
 

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 17/18
27/08/2016 A collection of Gnuplot examples | alvinalexander.com

Unix
man (help) pages
unix by example
tutorials

source code
warehouse
java examples
drupal examples

misc
privacy policy
terms & conditions
subscribe
unsubscribe
wincvs tutorial
function point
analysis (fpa)
fpa tutorial
 
 

Other
mobile website
rss feed
my photos
life in alaska
how i sold my business
living in talkeetna, alaska
my bookmarks
inspirational quotes
source code snippets

http://alvinalexander.com/technology/gnuplot-charts-graphs-examples 18/18

You might also like