gnuplot–Basic 2D Plot
This is the third project that I needed to use gnuplot, and I’m learning it for the third time. I know I’ll need to write a note for it. So here it is.
gnuplot is easy to start with. You can plot a nice graph after reading a tutorial for 5 minutes. This post summarizes some basic stuff for 2D plotting.
gnuplot supports both interactive mode and scripting mode. We’ll cover both.
A Simple Plot
The plot command is used to plot 2D figures and graphs. It’s very simple to use. For example, you want to plot a sine function and a y=x function. First, you enter the “gnuplot” command in the Linux command shell to go the gnuplot command interface.
And then you enter the command below,
gnuplot> plot sin(x), x
This gives you the figure below,
Figure 1. a sine function and a y=x function drawn by gunplot
Plotting for a Data File
gnuplot is easy to integrate with other programs. Another program can output a data file and gnuplot plot the graph based on the data file.
Suppose we have a data file named “data.dat” with content as below,
0.1 31.15
0.2 30.42
0.3 29.94
0.4 27.65
0.5 25.76
0.6 23.61
0.7 21.16
0.8 20.13
0.9 18.30
1.0 16.81
To plot the graph based on the data file, we can use the command below,
gnuplot> plot “data.dat” using 1:2 title “1-3″ with lines
And we get the figure below,
Figure 2. gnuplot plots figure based on a data file
Note that “using 1:2” maps the first column of the data to x axis and the second column of the data to y axis. title “1-3” specifies the legend title for the line. “with line” specifies the drawing styles. Other common options of styles include “points” and “linespoints”.
Also note that it is easy to draw two lines with data from the same file. Suppose the data.dat file contains the third column, then the following command can give us two lines.
gnuplot> plot “data.dat” using 1:2 title “1-3” with lines, “data.dat” using 1:3 title “1-3” with points
Plotting for Multiple Files
It is easy to plot from multiple data files too. Suppose we have another file “data2.dat” with the content as below,
0.1 17.67
0.2 17.67
0.3 17.67
0.4 17.67
0.5 17.67
0.6 17.67
0.7 17.67
0.8 17.67
0.9 17.67
1.0 17.67
gnuplot> plot “data.dat” using 1:2 title “1-3″ with lines, “data2.dat” using 1:2 with linespoints
This gives us the figure below,
Figure 3. gnuplot plot from two data files
Export Current Configuration as Script
Once you finished plotting a figure, you may want to save the command and configuration for later usage. You can do it using the command below,
gnuplot> save “test.gp”
Suppose you just finished the example above, and the save command will give you a file as below,
#!/usr/bin/gnuplot -persist
#
#
# G N U P L O T
# Version 4.2 patchlevel 6
# last modified Sep 2009
# System: Linux 2.6.32-40-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 http://www.gnuplot.info/faq/
#
# Send bug reports and suggestions to <http://sourceforge.net/projects/gnuplot>
#
# set terminal wxt 0
# set output
unset clip points
set clip one
unset clip two
set bar 1.000000
set border 31 front linetype -1 linewidth 1.000
set xdata
set ydata
set zdata
set x2data
set y2data
set timefmt x "%d/%m/%y,%H:%M"
set timefmt y "%d/%m/%y,%H:%M"
set timefmt z "%d/%m/%y,%H:%M"
set timefmt x2 "%d/%m/%y,%H:%M"
set timefmt y2 "%d/%m/%y,%H:%M"
set timefmt cb "%d/%m/%y,%H:%M"
set boxwidth
set style fill empty border
set style rectangle back fc lt -3 fillstyle solid 1.00 border -1
set dummy x,y
set format x "% g"
set format y "% g"
set format x2 "% g"
set format y2 "% g"
set format z "% g"
set format cb "% g"
set angles radians
unset grid
set key title ""
set key inside right top vertical Right noreverse enhanced autotitles nobox
set key noinvert samplen 4 spacing 1 width 0 height 0
unset label
unset arrow
set style increment default
unset style line
unset style arrow
set style histogram clustered gap 2 title offset character 0, 0, 0
unset logscale
set offsets 0, 0, 0, 0
set pointsize 1
set encoding default
unset polar
unset parametric
unset decimalsign
set view 60, 30, 1, 1
set samples 100, 100
set isosamples 10, 10
set surface
unset contour
set clabel '%8.3g'
set mapping cartesian
set datafile separator whitespace
unset hidden3d
set cntrparam order 4
set cntrparam linear
set cntrparam levels auto 5
set cntrparam points 5
set size ratio 0 1,1
set origin 0,0
set style data points
set style function lines
set xzeroaxis linetype -2 linewidth 1.000
set yzeroaxis linetype -2 linewidth 1.000
set zzeroaxis linetype -2 linewidth 1.000
set x2zeroaxis linetype -2 linewidth 1.000
set y2zeroaxis linetype -2 linewidth 1.000
set ticslevel 0.5
set mxtics default
set mytics default
set mztics default
set mx2tics default
set my2tics default
set mcbtics default
set xtics border in scale 1,0.5 mirror norotate offset character 0, 0, 0
set xtics autofreq norangelimit
set ytics border in scale 1,0.5 mirror norotate offset character 0, 0, 0
set ytics autofreq norangelimit
set ztics border in scale 1,0.5 nomirror norotate offset character 0, 0, 0
set ztics autofreq norangelimit
set nox2tics
set noy2tics
set cbtics border in scale 1,0.5 mirror norotate offset character 0, 0, 0
set cbtics autofreq norangelimit
set title ""
set title offset character 0, 0, 0 font "" norotate
set timestamp bottom
set timestamp ""
set timestamp offset character 0, 0, 0 font "" norotate
set rrange [ * : * ] noreverse nowriteback # (currently [0.00000:10.0000] )
set trange [ * : * ] noreverse nowriteback # (currently [-5.00000:5.00000] )
set urange [ * : * ] noreverse nowriteback # (currently [-5.00000:5.00000] )
set vrange [ * : * ] noreverse nowriteback # (currently [-5.00000:5.00000] )
set xlabel ""
set xlabel offset character 0, 0, 0 font "" textcolor lt -1 norotate
set x2label ""
set x2label offset character 0, 0, 0 font "" textcolor lt -1 norotate
set xrange [ * : * ] noreverse nowriteback # (currently [-10.0000:10.0000] )
set x2range [ * : * ] noreverse nowriteback # (currently [-10.0000:10.0000] )
set ylabel ""
set ylabel offset character 0, 0, 0 font "" textcolor lt -1 rotate by 90
set y2label ""
set y2label offset character 0, 0, 0 font "" textcolor lt -1 rotate by 90
set yrange [ * : * ] noreverse nowriteback # (currently [-10.0000:10.0000] )
set y2range [ * : * ] noreverse nowriteback # (currently [-10.0000:10.0000] )
set zlabel ""
set zlabel offset character 0, 0, 0 font "" textcolor lt -1 norotate
set zrange [ * : * ] noreverse nowriteback # (currently [-10.0000:10.0000] )
set cblabel ""
set cblabel offset character 0, 0, 0 font "" textcolor lt -1 rotate by 90
set cbrange [ * : * ] noreverse nowriteback # (currently [-10.0000:10.0000] )
set zero 1e-08
set lmargin -1
set bmargin -1
set rmargin -1
set tmargin -1
set locale "C"
set pm3d explicit at s
set pm3d scansautomatic
set pm3d interpolate 1,1 flush begin noftriangles nohidden3d corners2color mean
set palette positive nops_allcF maxcolors 0 gamma 1.5 color model RGB
set palette rgbformulae 7, 5, 15
set colorbox default
set colorbox vertical origin screen 0.9, 0.2, 0 size screen 0.05, 0.6, 0 front bdefault
set loadpath
set fontpath
set fit noerrorvariables
GNUTERM = "wxt"
plot "data.dat" using 1:2 title "1-3" with lines, "data2.dat" using 1:2 with linespoints
# EOF
Next time you want to draw the figure again, simple execute “load” or “call” command,
gnuplot> load “test.pg”
or
gnuplot> call “test.pg”
The differences between load and call is that call allows one to pass up to 10 parameters, which corresponds to $0 – $9 in the script file.
Saving the Drawing as Pictures
gnuplot allows one to export the plot in many formats, including “png”, “svg”, “postscript” etc. One can check out all the formats supported by “set term” command.
Below is the commands that exports the plot as png file.
gnuplot> set term png
gnuplot> set output “test.png”
gnuplot> replot
The first command set the output device type, the second command sets the output file and the third command replot the plot on the output file.
A more general script can be written as below to export plot,
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
Assume the script file is “test.gp”, then we can execute the command below in gnuplot command line interface to generate the graph.
call “test.gp” “test.png”
A Simple Script
Now we provide a simple script,
unset title
#unset key
#unset xtics
#unset ytics
#set format y "%.1t";
#set format x "%.1t";
set title "Test\ndata.dat VS. data2.dat" 0,1
set xlabel "X";
set ylabel "Y";
#set border 0
#set xrange []
#set yrange []
plot '$0' using 1:2 title "data.dat" with linespoints, '$1' using 1:2 title "data2.dat" with linespoints
#
set term push
set term png size 800, 600
set output "$2"
replot
set output
set terminal pop
This script accepts three input parameters, $0 and $1 are two data files, and $2 is the output png filename.
Suppose the script is named “test.pg”, and we still use the two data files “data.dat” and “data2.dat”, and we output to “test.png” file. Then we can enter the command below,
gnuplog> call “test.pg” “data.dat” “data2.dat” “test.png”
Then we get the figure below,
Figure 4. gnuplot plot with a script
Once we’re done with gnuplot, we can type “quit” or “exit” to exit from the gnuplot command line interface.
2 comments on “gnuplot–Basic 2D Plot”
Leave a Reply Cancel reply
40% Discount on My Book — Android NDK Cookbook
Android NDK Cookbook ebook 40% discount with promotion code MREANC40 at Packt Publishing The promotion code is valid until 15th June.Categories
- Android Apps (18)
- Android Audio Editor (1)
- TS 2 (3)
- Video Converter Android (8)
- Video2Gif (1)
- Android Tutorial (26)
- Android Dev Tools (1)
- API illustrated (8)
- Multimedia API (3)
- ffmpeg on Android (4)
- NDK (6)
- UI (5)
- Animation (1)
- Code Snippet (2)
- Coding Beyond Technique (18)
- a word, a world (4)
- Bug Rectified (4)
- Programming Habit (1)
- Software as a Career (1)
- Software as User Experience (1)
- Compilers and Related (2)
- ELF (2)
- Computer Languages (31)
- C/C++ (13)
- Java (9)
- JavaScript (2)
- PHP (1)
- Python (8)
- Data Structure & Algorithms (29)
- Bits (1)
- Data Structure (5)
- Integers (10)
- BigInteger (1)
- Prime (4)
- Search (3)
- Sorting (5)
- Strings (5)
- Database (1)
- SQLite (1)
- Digital Signal Processing (33)
- Distributed Systems (17)
- Apache Cassandra (6)
- Apache Hadoop (8)
- Apache Avro (3)
- Apache Nutch (3)
- Apache Solr (1)
- Linux Study Notes (40)
- crontab (1)
- Linux Kernel Programming (8)
- Linux Programming (12)
- IPC (2)
- Linux Network Programming (5)
- Linux Signals (2)
- Linux Shell Scripting (1)
- ssh (3)
- Machinery (30)
- misc (1)
- My Ideas (1)
- My Project (3)
- Mobile Caching (1)
- Selective Decoding (2)
- My Publication (1)
- My Readings (1)
- Networking (15)
- Program for Performance (8)
- Uncategorized (1)
- Virtual Machine (2)
- Web Dev (8)
- web components (3)
- Android Apps (18)
Recent Comments
Archives
- May 2013 (1)
- April 2013 (1)
- March 2013 (4)
- December 2012 (2)
- November 2012 (6)
- October 2012 (6)
- September 2012 (3)
- August 2012 (13)
- July 2012 (15)
- June 2012 (3)
- May 2012 (8)
- April 2012 (4)
- March 2012 (13)
- February 2012 (19)
- January 2012 (9)
- December 2011 (11)
- November 2011 (12)
- October 2011 (4)
- September 2011 (12)
- August 2011 (16)
- July 2011 (15)
- June 2011 (6)
- May 2011 (10)
- April 2011 (13)
- March 2011 (20)
- February 2011 (4)
- November 2010 (2)
- May 2010 (1)
- April 2010 (1)
- February 2010 (1)





It’s very good. thanks alot
You’re welcome!