Gnuplot drawing A

Source: Internet
Author: User

Gnuplot is a tool for generating trend charts and other graphics. It is typically used to collect data based on time, but not limited to this, or to use static data. Gnuplot can be run or run dynamically in batch mode, and the results are displayed by a graphics viewer or a Web browser. This article demonstrates how to use Gnuplot and batch files to generate graphics for data collected from SAR or other data sources. Gnuplot has a number of command options that you can use with the set operator. However, to use a line or box to generate graphics, you need to know something about the document.

In this article, I'll use a WEB server to provide graphics.

Gnuplot Overview

Gnuplot converts the original form data into a graphics file. The popular formats are PNG, PDF, and JPEG, which can then be passed either dynamically or directly to the X terminal, Web page, or generic graphics viewer. You can also import images into a document. Use the command interface to interact with gnuplot and use the "set" command to specify how the image is formatted and displayed. This usually includes the size of the image, the colors used, the scale, the x, y coordinates, and the output image name. The plot command is then used to actually generate the image through the gnuplot engine. You can also use the Splot command to draw a 3D image. Although you can use the command interface to interactively execute commands, the best approach is to use a configuration file so that you can reuse the configuration file by using the variables in the file (if needed) through the shell ' here ' document method. The configuration file is then connected to the gnuplot through a pipeline to generate an image file. If there are errors in the configuration file, these errors are displayed in this procedure and the location of the error is highlighted. After you generate the image files, you can display them.

As with any data collection process used to generate graphics, you must filter the data collected before passing it to gnuplot. This may include deleting information that is not needed at the ends of the data file, and sed and awk can help meet any text-filtering requirements.

Install Gnuplot

Can I get from AIX? 5L Source Packages Web site to download the rpm form of gnuplot 4.2:http://www.perzl.org/aix/index.php.

For the example in this article, you will need a running HTTP server in addition to gnuplot.

You can also download the required dependent libraries and HTTP servers from the back of this article or from the AIX Toolbox Web site.

Before installing Gnuplot, be sure to install the following RPM library because gnuplot needs these libraries:

fontconfig-2.7.2-1.aix5.1.ppc.rpm
expat-2.0.1-2.aix5.1.ppc.rpm 
freetype2-2.3.9-1.aix5.1.ppc.rpm
zlib-1.2.3-5.aix5.1.ppc.rpm 
libpng-1.2.40-1.aix5.1.ppc.rpm
gd-2.0.35-4.aix5.1.ppc.rpm
libjpeg-7-1.aix5.1.ppc.rpm
libxpm-3.5.7-2.aix5.1.ppc.rpm 
 gettext-0.17-1.aix5.1.ppc.rpm
glib2-2.20.5-1.aix5.1.ppc.rpm

Use the following command to list the installed RPM:

# RPM–QA

Finally, install the Gnuplot package:

# RPM-IVH gnuplot-4.2.4-1.aix5.1.ppc.rpm 

Run gnuplot to test it. This displays the Gnuplot command interface (exit the interface with the QUIT command):

$ gnuplot
      G N U P L O T
        Version 4.2 patchlevel 4 Last
        modified SEP 2008
        system:5.3
        Copyright (C) 1986 -1993, 1998, 2007, 2008
        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 

Creating Graphics with SAR

SAR is probably the most common method of collecting performance data, so let's use it as an example. Listing 1 (sarx.txt) is the data collected by running the SAR, the data collection time is 5 hours, collected once per hour, the output has been filtered.


Listing 1. Sarx.txt

    
14:10:50    4      4.00
15:10:50    3      4.00
16:10:50    1      4.00
17:10:49    2      4.00
18:10:40    3     km      4.00

Next, create a file to contain all the gnuplot commands required to generate the graphic. 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 and how it should be displayed. Listing 2 (sarx.conf) contains the configuration file that performs the set operation. The line that starts 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 the generated image should take. This article uses the PNG (Portable Network Graphics) format.

Set Output "Sarimage.png"

Next, tell gnuplot the actual output image filename. Here, the file name is sarimage.png.

Set Autoscale

When you generate a graphic, you specify the x and Y axis ranges for the data. This example lets gnuplot compute the range values themselves. However, you can modify this option (shown later).

Set xdata time
set 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

Depending on the UNIX date notation, the date variable should be placed in double quotes. Other formats that are commonly used include:

%d  -day of month 1-31%m-month of the year
1-12
%y-year  0-99
%b  -three character of month NA Me, Ie:jan, Feb
%B  -name of month

If the date column is in hour-minute format, it is represented by: set Timefmt″%h-%m″.

Set style data lines

When this graphic is displayed, the data drawn should be a smooth data line. Other common drawing formats include dots, boxes, errorbars, and candlesticks.

Plot "Sarx1.txt" using 1:2 title "%user", ' using 1:3 title '%sys '

Next, you use the plot command to actually draw or generate graphics. First, specify the data entry file name, and then tell gnuplot which columns to draw. In this example, use the 1th column as the x-axis, draw the 2nd column of data, titled "%user", and then draw the 3rd column, titled "%sys." The title (that is, the label) is displayed 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 you should use two single quotes.

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 is seen in the Web browser using the sample data provided.


Listing 2. sarx.conf

    
#sarx. conf
set terminal PNG truecolor
set Output "Sarimage.png"
set Autoscale
set xdata time
set Timefmt "%h:%m:%s"
set style data lines
plot "Sarx.txt" using 1:2 title "%user", ' using 1:3 title '%sys '



Figure 1. Sarimage

Representation of drawing options

Abbreviations can be used in the plot command. For example, after the initial plot command, other plot command options can be abbreviated, represented by the first letter of the option. However, for this article, I only represent the input file with an abbreviation, 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 abbreviated 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 displaying graphics to other users, you should sometimes include explicit labels and captions so that users can understand the meaning of the data. To include x and y tags, you should use the Xlabel and Ylabel commands and enclose the label text in quotation marks:

Set Ylabel "Y line info,"
set Xlabel "x line Info"

To add a caption for a graphic by using the title command:

Set title "Main Title info here"

When you generate a graphic, 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 use color encoding to specify any color, with the color encoding beginning with the letter x and in hexadecimal. The hexadecimal-encoded format is:

Xrrggbb

Search for ' hex color codes ' on Google to find hexadecimal color-coded tables.

The order of overriding gnuplot default colors is:

Background
border 
X
Y
plotting lines

The light gray hexadecimal representation 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 above command, which overrides the default color used by Gnuplot. Because I didn't specify a different color to override the default, Gnuplot still uses its own default color for other colors of the graphic.

Using the grid as a reference point is also useful when viewing graphics. The following command uses the grid option to let gnuplot add a grid to the graphic:

Set grid

You can set the x and Y coordinates implicitly, but you cannot draw a graphic 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 range 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. conf
set terminal PNG  xc9c9c9
Set Output "Sarimage.png"
set Autoscale
set XData
time Set timefmt "%h:%m:%s"
set Ylabel "Performance"
set Xlabel "Time"
set title "Sar Output Example"
set XR Ange ["14:00:00": "18:15:00"]
set yrange ["10:00": "50:00"]
set grid
set style data lines
plot "Sarx 1.txt "using 1:2 title"%user "," Using 1:3 title "%sys"



Figure 2. Sarimage2

Gnuplot and columnar graphs

You 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 column 2nd contains the total number of members.


Listing 4. Grpdata.txt

    
Staff
Apps
sybgrp db2grp1 dasdm   8
dstage  8
Batch
DB2PRD 1

To use a histogram, just tell gnuplot you should use a histogram to generate graphics:

Set style data histograms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.