Source: http://www.ibm.com/developerworks/cn/aix/library/au-gnuplot/Introduction
Gnuplot is a tool for generating trend graphs and other graphics. It is typically used to collect time-based data, but it is not limited to this; You can also use static data. Gnuplot can run in batch mode or run dynamically, and the results are displayed by a graphical viewer or Web browser. This article demonstrates how to use Gnuplot and batch files to generate graphics for data collected from a SAR or other data source. Gnuplot has a number of command options that can be used with the set operator. However, to generate graphics using lines or boxes, you need to understand the documentation.
In this article, I'll use a WEB server to provide graphics.
Back to top of page
Gnuplot Overview
Gnuplot The original table data into a graphics file. The popular formats are PNG, PDF, and JPEG, which can then be passed dynamically or directly to X terminals, Web pages, or generic graphical viewers. You can also import the image into a document. Using the command interface to interact with Gnuplot, use the SET command to specify how the image is formatted and displayed. This usually includes the size of the image, the color used, the scale, the x, the y-coordinate, and the output image name. Then, use the plot command to actually generate the image through the gnuplot engine. You can also draw 3D images using the Splot command. Although you can use the command interface to execute commands interactively, the best approach is to use a configuration file, which allows you to reuse the configuration file using the variables in the file (if needed) through the shell ' here ' document method. The configuration file is then connected to the gnuplot by a pipeline to generate the image file. If there are errors in the configuration file, these errors are displayed during this process and the location of the error is highlighted. After you have generated the image files, you can display them.
As with any data collection process used to generate graphics, you must filter the data you collect before you pass the data to gnuplot. This may include removing information that is not needed in the data file, and SED and awk help meet any text filtering needs.
Back to top of page
Installing Gnuplot
You can download gnuplot 4.2:http://www.perzl.org/aix/index.php in rpm form from the aix®5l Source Packages website.
For the example in this article, you need a running HTTP server in addition to gnuplot.
You can also download the required dependent libraries and HTTP servers from behind this article or from the AIX Toolbox Web site.
Before installing Gnuplot, be sure to install the following RPM libraries, as gnuplot requires these libraries:
fontconfig-2.7.2-1.aix5.1.ppc.rpmexpat-2.0.1-2.aix5.1.ppc.rpm freetype2-2.3.9-1.aix5.1.ppc.rpmzlib-1.2.3-5.aix5.1.ppc.rpm libpng-1.2.40-1.aix5.1.ppc.rpmgd-2.0.35-4.aix5.1.ppc.rpmlibjpeg-7-1.aix5.1.ppc.rpmlibxpm-3.5.7-2.aix5.1.ppc.rpm gettext-0.17-1.aix5.1.ppc.rpmglib2-2.20.5-1.aix5.1.ppc.rpm
Use the following command to list the RPMs that have been installed:
# RPM–QA
Finally, install the Gnuplot package:
# RPM-IVH gnuplot-4.2.4-1.aix5.1.ppc.rpm
Run gnuplot to test it. This will display the Gnuplot command interface ( quit
exit the interface with a command):
$ gnuplot G N U P L O T Version 4.2 patchlevel 4 last modified Sep system:5.3 Copyright (C) 1986 -1993, 1998, 2004, Thomas Williams, Colin Kelley and many others Type ' help ' to access the on-line RE Ference Manual. The Gnuplot FAQ is available from http://www.gnuplot.info/faq/ Send Bugs reports and suggestions to
Back to top of page
Create a graphic with SARSAR is probably the most common method of collecting performance data, so let's take it as an example. Listing 1 (sarx.txt) is the data collected by running SAR, the data collection time is 5 hours, collected hourly, the output has been filtered.
Listing 1. Sarx.txt14:10:50 4 4.0015:10:50 3 4.0016:10:50 1 4.0017:10:49 2 4.0018:10:40, 3 35 4.00
Next, create a file to contain all the gnuplot commands needed to generate the drawing. You can give this file any name; In this article, I'll name it sarx.conf.
To be able to generate images, you must tell gnuplot what format the graphics file should be in and how it should be displayed. Listing 2 (sarx.conf) contains the configuration file that performs the set operation. The line that begins with the # character is a comment. Let's take a closer look at listing 2.
Set Terminal PNG TrueColor
Set the terminal type First, which tells Gnuplot what format to use for the generated image. This article uses the PNG (Portable Network Graphics) format.
Set Output "Sarimage.png"
Next, tell gnuplot the actual output image file name. Here, the file name is sarimage.png.
Set Autoscale
When you generate a drawing, you specify the X-and y-axis ranges for the data. This example lets gnuplot calculate the range value itself. However, you can modify this option (shown later).
Set XData timeset timefmt "%h:%m:%s"
Because this example uses a date value as the reference point for the data, you need to tell the format of the gnuplot date data. In Listing 1 Sarx.txt, the date format is:
Hour:Minute:Seconds
Date variables should be enclosed in double quotes, based on the UNIX date notation. Other commonly used formats include the following:
%d-day of month 1-31%m-month of the year 1-12%y -year 0-99%b -three character of month name, Ie:jan, Feb %B -Name of month
If the date column Hour-Minute
is formatted, it is : set timefmt ″%H-%M″
represented.
Set style data lines
When the graphic is displayed, the plotted data should be a smooth data line. Other common drawing formats include dots, boxes, errorbars, candlesticks.
Plot "Sarx1.txt" using 1:2 title "%user", "Using 1:3 title"%sys "
Next, you actually draw or generate the graph using the plot command. First, specify the data input file name, and then tell gnuplot which columns to draw. In this example, the 1th column is used as the x-axis, the 2nd column of data is plotted, the title is "%user", and the column 3rd is drawn with the title "%sys". The title (that is, the label) appears in the upper-right corner of the graphic. When drawing, Columns 2nd and 3rd use the 1th column as the x value. Each ' using ' statement in the plot command is separated by commas. The next section discusses why two single quotes are used.
The command format for generating image files is:
Cat < conf file> | Gnuplot
For this article, I use:
$ Cat Sarx.conf | Gnuplot
The Sarimage.png file is now generated. To view this image, copy the file to the Htdocs directory in the WEB server file system.
Figure 1 (sarimage) shows the output that you see in a Web browser using the provided sample data.
Listing 2. Sarx.conf#sarx. Confset terminal png truecolorset output "sarimage.png" set Autoscaleset xdata timeset timefmt "%h:%m:%s" Set style da Ta linesplot "sarx.txt" using 1:2 title "%user", "Using 1:3 title"%sys "
Figure 1. SarimageBack to top of page
Representation of drawing optionsAbbreviations can be used in the plot command. For example, after the original plot command, the other plot command options can be abbreviated, denoted by the first letter of the option. However, for this article, I only use abbreviations to represent input files, even if the input file (sarx1.txt) is represented by two single quotes. Here is a detailed explanation. The first example is the notation used in this article, the second example is the abbreviation notation, and the third example is a complete command statement that does not use any abbreviations. These three examples produce the same output.
Plot "Sarx1.txt" using 1:2 title "%user", "Using 1:3 title"%sys "Plot" Sarx1.txt "using 1:2 title"%user "," u 1:3 T "% SYS "Plot" sarx1.txt "using 1:2 title"%user ", ″sarx1.txt ' using 1:3 title '%sys '
When you display graphics to other users, you should sometimes include explicit labels and headings so that users can understand the meaning of the data. To include the X and y labels, you should use the Xlabel and Ylabel commands and enclose the label text in quotation marks:
Set Ylabel "Y line info here" set Xlabel "x line info here"
To add a caption to a graphic using the title command:
Set title "Main Title info here"
When you generate a drawing, Gnuplot uses its own default color. By default, graphics are generated on a white background, which is meaningful for graphics that might be printed. However, you can specify any color using color coding, which begins with the letter X and is represented in hexadecimal. The hexadecimal encoding format is:
Xrrggbb
Search for ' hex color codes ' on Google to find the hexadecimal color coding table.
The order of overriding gnuplot default colors is:
Backgroundborder xyplotting Lines
The hexadecimal representation of light gray is: C9C9C9
.
You can use the following command to generate a light gray background graphic:
Set terminal PNG XC9C9C9
Note that the TrueColor option for the terminal type PNG must be replaced in the command above, which overrides the default color used by Gnuplot. Because I didn't specify a different color to override the default settings, Gnuplot still uses its own default color for other colors of the graphic.
It is also useful to use a grid as a reference point when viewing a drawing. The following command uses the grid option to have the gnuplot add a grid to the graph:
Set grid
You can set the x and Y coordinates implicitly, but you cannot draw a graphic if you want to ensure that the specified range is not less than the range of the data. For the sample data in Listing 1, the x-coordinate (time) range is from 14:10:50 to 18:10:40.
The y-coordinates (columns 2nd and 3rd) range from 14 to 50.
You can specify your own scope based on this information. The following example uses an X range from 14:00 to 18:15,y from 10 to 50.
Set xrange ["14:00:00": "18:15:00"]set yrange ["10:00": "50:00"]
Using these modifications in Listing 3, the resulting graphical output is shown in Figure 2.
Listing 3. Sarx2.conf#sarx2. Confset terminal png xc9c9c9set output "sarimage.png" set Autoscaleset xdata timeset timefmt "%h:%m:%s" set Ylabel "Performance" set Xlabel "time" set title "Sar Output Example" Set xrange ["14:00:00": "18:15:00"]set yrange ["10:00": "50:00"]set gridset style data Linesplot "Sarx1.txt" using 1:2 title "%user", "Using 1:3 title"%sys "
Figure 2. Sarimage2Back to top of page
Gnuplot and column chartsYou can also use a histogram or block diagram to represent data, which is sometimes more appropriate than using static data. Listing 4 is the total number of user group users obtained from an AIX machine. The 1th column contains the AIX group name, and the 2nd column contains the total number of members.
Listing 4. Grpdata.txtStaff 54apps 22sybgrp 12db2grp1 29dasdm 8dstage 21dsgrp 8batch 28DB2PRD 1
To use a histogram, just tell gnuplot that you should use a histogram to generate the graph:
Set style data histograms
The box for the default generated histogram does not have a thick border, and there is no fill color in the box. However, if you specify a border, the bottom edge of the box may have two thick lines along the x-axis (this looks less beautiful).
Gnuplot draws the border of the box in the following order: Top, bottom, left, and right, with values of 1, 2, 4, 8, respectively. To delete one or more border lines, simply provide the corresponding values. In this example, the bottom border line is removed using the-1 option. Specifying the Fill option fills the box with the default color:
Set style fill solid 1.00 border-1
For x-coordinates, the time is not used here, but the group name is used. Use the Xtic option to allow gnuplot to place tic and data labels along the x-axis (column 1th). Here is the group name. However, sometimes the label contains many characters, or the xtic time format does not fit between the tic on the graph. You will see the labels overlap each other. To avoid this problem, rotate the labels 90 degrees (Find the right angle by experimenting) and let them show vertically. You can use the following command to achieve this effect:
Set Xtic rotate by 90
The data in column 2nd uses the 1th column (x data) as the reference:
2:xtic (1)
When creating a drawing, add the title "Apps groups Numbers" to the data:
Plot "Grpdata.txt" using 2:xtic (1) title "Apps Group Numbers"
Use the following command to generate the image grpimage.png:
$ Cat Grphist.conf | Gnuplot
Listing 5 contains the gnuplot command to generate the image, as shown in Figure 3.
Listing 5. Grphist.conf# grphist.confset terminal png truecolorset output "grpimage.png" set Gridset xtic rotate by 90set style data Histogramsset Style fill solid 1.00 border-1plot "Grpdata.txt" using 2:xtic (1) title "Apps Group Numbers"
Figure 3. GrpimageOf course, you can plot multiple sets of data in a histogram like the SAR example. Now look at another data source. The data in Listing 6 reflects the increase or decrease in the data in the disk array within three months. The 1th column is the disk array name, the 2nd column is the disk usage for the first month, the 3rd column is the disk usage for the next one months, and the last one months of data are in the 4th column.
Listing 6. Disk.txtHdisk2 420 425 410hdisk3, 780 760HDISK4, 452HDISK5 680 702 690HDISK6 530 515 514
The configuration file for generating the graph is shown in Listing 7. In the plot command, you also use the xtic command to modify how the x-axis data is displayed. When a histogram is generated, columns 2nd, 3, and 4 use the x-axis as the reference point. Therefore, the 2nd column is specified using the X axis as the reference:
2:xtic (1)
Gnuplot assumes that the other columns to be drawn also refer to the x-axis, so you do not need to specify Xtic in the plot command:
Plot "Disk.txt" using 2:xtic (1) title "Oct-09 Data Growth (GB)", "Using 3 title" NOV-09 Data Growth (GB) "," Using 4 Title "Dec-09 Data Growth (GB)"
Use the following command to generate the image:
$ Cat Diskhist.conf | Gnuplot
The resulting image is shown in Figure 4.
Listing 7. Diskhist.conf# diskhist.confset terminal png truecolorset output "diskimage.png" set Gridset style data Histogramsset style fill solid 1 . xx border-1plot "Disk.txt" using 2:xtic (1) title "Oct-09 Data Growth (GB)", "Using 3 title" NOV-09 Data Growth (GB) " , ' using 4 title ' DEC-09 Data Growth (GB) "
Figure 4. DiskImageBack to top of page
ConclusionYou can use Gnuplot to generate different graphs from a variety of data. To automate graphical generation using shell scripting, I recommend using the ' here ' documentation method, which allows you to dynamically generate graphics using Gnuplot. To prevent the WEB server from caching graphics, you should use the appropriate HTML META tags (such as the ' No-cache ' statement) in your script.
Reprint to display data in a Web page using gnuplot