From LQWiki
Jump to: navigation, search

Gnuplot is a command-line based plotting tool. It can plot data or math equations in a variety of different formats.


To create a graph with gnuplot, you will need a mathematical function or a data file.

Data files

Let's say you want to show your cpu utilization from vmstAt as a graph. Here is how it goes:

vmstat 1 10 | grep -v r > vmstat.txt
gnuplot -p -e "plot 'vmstat.txt' \
   using 13 title 'CPU user load' with lines, 'vmstat.txt' \
   using 14 title 'CPU system load' with lines, 'vmstat.txt' \
   using 15 title 'CPU idle time' with lines, 'vmstat.txt' \
   using 16 title 'CPU IO wait time' with lines"

The first line collects system data into a file vmstat.txt. The command will run 10 seconds (parameter 10). The second line creates a graph out of your data. Column 13 will be displayed as "CPU user load", column 14 as "CPU system load" and so on. There will be lines connecting the measurement points (parameter "with lines").

mathematical functions

You can also plot math functions:

plot x**2

will plot the function x2


Use the 'splot' command for plotting 3d functions:

splot -x**3 -y

plotting multiple equations

You can plot multiple equations on the same graph by seperating the equations with a comma:

plot x**2, x
splot x*y, x**2-y**4


Many settings can be changed, such as the axis ranges and sampling rate. To avoid retyping the plot equation after each change, simple type replot to re-plot the previously inputted equation.

Setting the range

You can use the set command to set the ranges. The following commands set the x range to [0,10], the y range to [2,6] and the z range to [10,100000]:

set xrange [0:10]
set yrange [2:6]
set zrange [10:100000]

By default the ranges are autoscaled. You can revert them back to autoscaling with:

set autoscale x
set autoscale y
set autoscale z

If you need one of the axis to be logarithmic use set logscale AXIS. It can be turned off with set nologscale AXIS.

set logscale z
set nologscale z

Setting the grid size

In 3d this is done with set isosamples followed by the x and y rates of the sampling. Ex:

set isosamples 50, 50

Increasing the sampling rate enough will degrade performance.

Saving in different formats

Saving the graphs in different formats is done by setting the output and terminal variables.

To screen

The default behavior is to show the plot in an X11 window. This is done with:

set output
set terminal X11


Postscript files can be printed from the command line with the lpr command.

set output ""
set terminal postscript

Postscript options

Print in color (default is monochrome):

set terminal postscript color

Use solid lines instead of dashed:

set terminal postscript solid

Encapsulated postscript

If you want to include the graph in a LaTeX document, you need to save it as an eps file:

set output "filename.eps"
set terminal postscript eps

PNG File

To save the graph as a png image:

set output "filename.png"
set terminal png


External links