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,
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,
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,
Next time you want to draw the figure again, simple execute “load” or “call” command,
gnuplot> load “test.pg”
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”
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,
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,
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 CookbookAndroid NDK Cookbook ebook 40% discount with promotion code MREANC40 at Packt Publishing The promotion code is valid until 15th June.
- Android Apps (18)
- Android Tutorial (26)
- Code Snippet (2)
- Coding Beyond Technique (18)
- Compilers and Related (2)
- ELF (2)
- Computer Languages (31)
- Data Structure & Algorithms (29)
- Database (1)
- SQLite (1)
- Digital Signal Processing (33)
- Distributed Systems (17)
- Linux Study Notes (40)
- Machinery (30)
- misc (1)
- My Ideas (1)
- My Project (3)
- My Publication (1)
- My Readings (1)
- Networking (15)
- Program for Performance (8)
- Uncategorized (1)
- Virtual Machine (2)
- Web Dev (8)
- web components (3)
- 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)