6 Visualization of computed data – Computational Technologies

Ivan K. Sirditov

6 Visualization of computed data

Abstract: In scientific and engineering computations, after performing calculations, we need to visualize numerical results for understanding and analysis. In doing so, we face the problem of choosing an efficient tool for visualization that would be easy-to-learn and convenient to use. Presently gnuplot27 is probably the most well-known program for plotting and visualizing data. gnuplot is based on a system of commands which can be employed both interactively and in scripts. In this chapter, we consider plotting and preparing data for visualization as well as the processing of results of calculations using gnuplot. We also provide a brief description of the main features of gnuplot.

6.1 Basics

General information and the basics of working with gnuplot is discussed below. Creating scripts for gnuplot is also considered.

6.1.1 Essentials of gnuplot

The gnuplot program is a convenient and fairly simple tool for data visualization. It complies fully with the style and spirit of UNIX, i.e. it can do one particular thing well.

The gnuplot program is a portable open source program. It is not related to the GNU project; gnuplot uses its own license, which implies the possibility of free use and code completion in the form of plug-ins. The gnuplot project has been supported and under developed since 1986. The gnuplot program was initially created to help users visualize mathematical functions and data. Now gnuplot is used for many non-interactive tasks such as web scripting and is the visualization engine in a number of third-party applications (see e.g. Octave28).

The primary advantages of gnuplot are:

  • – actively maintained;
  • – stability in work;
  • – free and open source code;
  • – portability (Linux, Windows, and Mac OS);
  • – interactive mode or scripts;
  • – large set of built-in functions;
  • – many examples;
  • – flexible and efficient programming language;
  • – support of many different types of output.

Before beginning a more detailed study, we should highlight that gnuplot has a flexible and convenient help system. To get help for any command, just type help <command> in an interactive session. A help topic provides complete information about the command and example of its use.

6.1.2 How to install and run gnuplot

The gnuplot program can be installed from the standard repository of Linux using the following command-line:

The source code can also be downloaded from the official web-site29.

To launch gnuplot in interactive mode, we type the following command:

The program displays information about itself. Now all lines entered in the terminal are considered as commands. To quit, we can use both the quit and exit commands, or the hotkey ctrl−D.

The welcome information of gnuplot is as follows:

6.1.3 Simple plot

We will now present a simple plotting using gnuplot. Note that the gnuplot syntax is case sensitive, i.e. plot and PLOT are not the same.

The main command of gnuplot is plot; it generates two-dimensional plots. It can be applied to graph functions or to visualize data from a file.

Let us begin with the simple command:

This command displays the graph of the function cos (x) , as shown in Figure 6.1.

Fig. 6.1. Cosine.

Next, we add more functions and display them with the cosine. We redo the last command (using the up-arrow key or ctrl−P) and change it to

This command plots the cosine with the linear function x and the nonlinear function x2x (see Figure 6.2). The syntax of gnuplot for mathematical expressions is similar to the basic programming languages. The only exception is the sign ✼ ✼;in gnuplot, this denotes the exponentiation operator.

We see that the range of y values is larger than the previous plot. Therefore, the visibility of the cosine is lost. gnuplot automatically selects a value range so that all values will fit. For the latter graph, we can specify new range of y performing the command

Fig. 6.2. Graphs of functions.

This command displays the graph with the range from -2 to 2 for y axis, as depicted in Figure 6.3. The range is set immediately after the plot command in square brackets. The range of x is specified in the first bracket (here, we left it blank, since the values of x are appropriate for displaying), whereas the range of y is set in the second brackets. In addition, we can specify ranges using the commands set yrange [ymin : ymax] and set xrange [xmin:xmax]. Ranges must be specified before invoking the command plot. Thus, we can write the previous command as

The visualization of mathematical functions deserves a separate detailed consideration. But our main object in this study is the visualization of calculated data, and therefore, let us consider data plotting.

The gnuplot program reads data from a text file and the file extension may be anything. Data must be numeric and separated by tabs or spaces. If a line begins with #, then it is a comment and ignored. Let us start with a simple example. Assume that we have the file data. txt that contains the following:

Fig. 6.3. Graphs with ranges.

The first column is usually treated as the X value, whereas the second one is the Y value. This format is best suited for gnuplot. But gnuplot would not be so popular if it could only read this kind of data. Below we consider working with more complex file structures.

For the given file, we can visualize data using the following command:

Fig. 6.4. Data visualization.

This command generates a plot (see Figure 6.4) using the first two columns, but we can specify the X and Y values using the using option of plot.

This command means that gnuplot uses the first column for X value and the second column for Y value. If the using option is not specified, then the program takes the option using 1: 2 by default, and the first and second command are identical. Next, we plot the following draw:

This command generates two plots together (see Figure 6.5); for the second plot, the X and Y values are taken from the first and third column, respectively. We cannot say that this plot is clear and informative. By default, data points from the file are plotted using unconnected points with different types. We can change the plot style using the with option. This option specifies what style to use for plots. Consider the two most common styles, linespoints and lines. The style linespoints plots points connected by lines. The style lines plots lines without points. Now we change the style of the first plot to lines, the style of the second plot to linespoints. This results in the plots depicted in Figure 6.6.

Fig. 6.5. Two graphs.

Fig. 6.6. Styles.

6.1.4 Abbreviations

The gnuplot program has a set of commands and options for each command. In addition to plot, gnuplot uses other commands (set, show, splot, load, save).

The set command can be used to specify the settings of gnuplot. The show command is employed with set and shows these settings. For example, show yrange shows the value of this option. All the settings can be shown via show all. We can reset all the settings using the reset command. To specify default values or delete an option, we need to apply unset <option>.

The name of the command and the option can be reduced to a shorter unique string. For instance, p for plot, sp for splot, t for title of plot. We cannot use s for splot because it would interfere with set.

6.1.5 Scripts

Next, we will operate with longer commands or with multiple commands. For this we need to use scripts for gnuplot. The gnuplot program executes scripts written in a file. To declare that a file contains a script, we should write at the beginning of the file

Further, we write gnuplot commands. For example,

We save this script into the file ex1. gnu (the gnu extension is used only to make it clear that this script is for gnuplot; any extension may be used). The script can be executed by means of the command

The script must be in the same directory where the command is invoked. If we run this script, it immediately opens a window with the plot and then immediately closes it. The window stays open if we apply the command pause -1 after plot.

Working in interactive mode, we can save commands used earlier in a file performing the command save “ex1. gnu”. In this case, all default commands will also be written to the file. To load commands from the file, we use the command load “ex1. gnu”. The name of the file must be inside quotes.

6.2 Data handling

The gnuplot program is well-suited for data processing. Now we will discuss commands to handle files with large data sets. Also we consider commands for changing plots.

6.2.1 Data blocks

Data sets may be more complex in comparison with the examples considered above. There are two basic cases: files with data blocks, and files containing records with multiple lines.

Consider the following case. A program performs some complicated calculations. Sometimes, it provides a summary of results, appends it to an output file and continues calculations. The resulting file is not a single data set; it is data blocks with many lines. Blank lines play an important role in reading data. Double blank lines indicate a separation of data blocks; a single blank line shows a break in the data.

Suppose that the resulting file has the following structure:

There are 3 data blocks in the above example. Each time value has its own x and y values. The index option of plot indicates a data block.

If we use the standard plot command, then it plots all 3 blocks using the same style. With index, we can get a required block, e.g.

This command plots the first data block (counting from 0). The index option is employed to plot several blocks. For example, plot “data” index 2 : 5 plots 4 data blocks from 3 to 6.

6.2.2 Data selection

Let us consider a file that contains different records with multiple lines:

In this case, gnuplot provides the every options of plot. It allows selection of certain lines or blocks of data. The general syntax of the option seems as follows:

Here are some examples:

6.2.3 Data changing

To change data and perform simple calculations with them, there is the command using. To do this, the column number is preceded with the $ sign; this column will be changed using some mathematical operation. Such expressions must be enclosed in parentheses. The expression using 1: sqrt ($2) does not work; there are no parentheses. It is possible to perform calculations with several columns. For example, using 1: 2 : () gives the product of the second and third columns. In the following example, we perform four simultaneous calculations: with y values themselves, doubled y values, the square root and the logarithm of y values (see Figure 6.7).

Fig. 6.7. Data changing.

This method is useful when we need to translate results from one system to another, e.g. to translate temperature from Celsius to Fahrenheit.

6.3 Data interpolation

The gnuplot program can perform data interpolation and approximation using the smooth option of the plot command. It should be used with one of unique, frequency, bezier, sbezier, csplines, acsplines parameters.

6.3.1 Sorting and averaging data

The smooth option with the unique parameter allows us to plot unsorted data and also find the average of a set of values. If several y values exist for one x, then this option finds the average value (the frequency option finds the sum). We consider sorting the following data:

Using the standard plot command, we get the graph presented in Figure 6.8, where the points are connected according to their order in the file. Applying smooth unique, we obtain the plot depicted in Figure 6.9 with the points sorted by the first column x:

Now we will show how to find the average using smooth unique. We have a file:

Here the data is not divided into blocks. We can obtain the average value of Y values for the entire period of time (see Figure 6.10) employing the following command:

Fig. 6.8. Unsorted points.

Fig. 6.9. Sorted points.

6.3.2 Smoothing data

The bezier, sbezier, csplines, acsplines parameters allow for smoothing data:

  • – be z i er connects points using a Bezier curve;
  • – sbezier first uses unique and then plots a Bezier curve;
  • – csplines first uses unique and then connects points using cubic splines;
  • – acsplines plots csplines with weights.

Fig. 6.10. The average values.

Fig. 6.11. Using Bezier and csplines.

Figure 6.11 shows an example of using bezier and csplines:

The following is an example of csplines with weights:

In the resulting graph (see Figure 6.12), we observe that if the weights are large, then the graph tends to an interpolation curve, whereas if the weights are small, then the graph is smoother.

Fig. 6.12. Weights for csplines.

6.4 Using styles

We can prepare high-quality illustrations for scientific articles and presentations using gnuplot.

6.4.1 Output

To show results, gnuplot uses a terminal, which can be a device or a file. By default, gnuplot displays the results. gnuplot supports many formats for output files (ps, fig, jpeg, LaTeX, metafont, pbm, pdf, png, postscript, svg). It is possible to change the type of the terminal using the following command:

A list of possible types:

  • – windows displays in the terminal of Windows OS;
  • – X11 displays in the terminal of Linux OS;
  • – png displays in a png file (raster format);
  • – jpeg displays in a j peg file (raster format);
  • – postscript displays in the vector format;
  • – latex displays in the LATEXformat.

We can see the full list of available terminals via the command

Let us study the postscript format, which is a vector format and is better suited for printing and presentation in scientific publications. By default, this terminal sets to monochrome coloring, which is suitable for black and white printing. For color graphs, we can employ the postscript color option or the png and jpeg formats. If we specify a terminal, the default options of the terminal are printed, e.g.

Here the font is Helvetica at 14pt, the orientation is landscape, color is monochrome, the text is without superscripts and subscripts (noenhanced), etc.

The terminal capabilities can be seen using the test command. For example, Figure 6.13 is the test window of the postscript terminal with the color parameter:

Here we observe the new command set output. This command specifies the file to which we want to output the results.

The available styles for lines and points are shown at the right of Figure 6.13. Styles may be different for each terminal.

Fig. 6.13. Capabilities of postscript.

6.4.2 Styles

A graph can be displayed using one of the following styles: lines, points, linespoints, impulses, dots, steps (fsteps, histeps), errorbars (or yerrorbars), xerrorbars, xyerrorbars, boxes, boxerrorbars, or boxxyerrorbars, histograms.

The styles can be set directly for a function plot or globally using the following commands:

The lines style displays points connected by lines. The line width and type are specified by the options linewidth and linetype, respectively.

The points style displays unconnected symbols. The symbol type is specified by the pointtype option (or pt); its size is given by the pointsize option (or ps).

The linespoints style is the combination of lines and points and displays symbols connected by lines. The parameters of this style are linewidth, linetype, pointtype, and pointsize.

The impulses style displays vertical lines from x axis to points, as shown in Figure 6.14.

The boxes style is necessary for two-dimensional plots. It draws a box from the x axis to the given point (see Figure 6.15).

The dots style plots a small dot at each point.

The steps style connects consecutive points with horizontal and vertical line segments, i.e. we obtain ladder (see Figure 6.16).

Fig. 6.14. Impulses.

Fig. 6.15. Boxes.

Fig. 6.16. Steps.

The fsteps style is similar to the previous one. The difference is in the order of a ladder drawing (Figure 6.17 presents this case).

The histeps style draws ladder so that the middle of step is a point. We can observe this situation in Figure 6.18.

The histograms style obviously draws a histogram. In this case, we can specify additional options. Histograms can be built in different ways: box or impulses. But histograms is most convenient.

The standard histogram (histograms clustered) view shows several columns next to each other (see Figure 6.19).

Fig. 6.17. Fsteps.

Fig. 6.18. Histeps.

Fig. 6.19. Histogram.

For example, histogram rowstacked displays values in one column (see Figure 6.20).

Fig. 6.20. Stacked histogram.

6.4.3 Line style

To set the style of lines, we use inline styles using the option lt <style_number> (linetype), e.g.:

Figure 6.21 presents numbering, lines, and symbols styles.

Fig. 6.21. Lines and symbols in postscript.

To specify options of lines, we apply two-letter abbreviations which begin with 1. For example, lw is linewidth. A list of abbreviations can be seen using the following command:

This command prints the options supported by the command set style line. Note that the options separated be a dash | are equivalent, i.e. the linecolor option can be written as the lc abbreviation.

For manual tuning, we use the following options:

6.5 Graph decoration

For greater clarity, it is sometimes necessary to somehow decorate graphs. Here we consider how to modify and customize legends, titles, labels, and axes.

6.5.1 Legend

We have already discussed how to change the legend using title “ name”. Here we discuss how to remove the legend and change its location.

There are two ways to remove the legend. The first way is implemented by the command

The second method is to apply the notitle option of the plot command. In the following command, file data has a legend, but the legend is cancelled:

The legend is usually displayed at the upper right of a graph. Its location can be changed using the command set key. If we employ the command then the legend appears at the lower left of the graph. We can also set the location of the legend directly. If we want to place it at the position (X, Y) = (100, 100), then we write

The coordinate system is defined by the X and Y axes. To customize the coordinate system, it is necessary to work with the help help coordinates.

6.5.2 Axes

Sometimes we need to place several graphs in one figure, where x and y values may differ. Using gnuplot, it is easy to specify values of each axis independently. By default, the Y2 axis is the same as the Y axis. For example, we make them different and plot cos (x) and the square of this function in the same graph. The axis can be selected using x1y1, x1y2, x2y1, x2y2. Here axis x1y2 means that the function will be scaled to fit the primary X axis and the secondary (right) Y axis. To customize the secondary Y axis, we apply the nomirror option for set ytics. We specify set y2tics to control tics on the Y2 axis:

Fig. 6.22. The secondary Y axis.

This command draws the graph shown in Figure 6.22.

To control tic marks on axes, we use the commands set xtics, set ytics, set mxtics, set mytics. For example,

By default, gnuplot draws all four axes, but sometimes we need to make some axes invisible. To remove undesired axes, the set border n command is employed. Each axis is represented by an integer number. The bottom axis is 1, the left axis is 2, the top axis is 4 and the right axis is 8, n is the sum of numbers assigned to the axes which will be built.

To disable tic marks, we use the commands set no{x|y}tics or set {x | y} tics nomirror.

For example, if we want to draw a graph without the top and right axes, then we perform the following commands:

An example of such a graph is depicted in Figure 6.23.

Fig. 6.23. Graph without the right and top axes.

If we want to have an axis passing through the zero coordinates, then we use the command set {x|y} zeroaxis. By default, the zero axis is drawn by points, but it is easy to redefine this via the commands line_style, line_type, line_width. For instance, we can do this as follows:

This command draws the graph presented in Figure 6.24.

Fig. 6.24. Zero axis.

6.5.3 Labels

It is possible to change the axis labels and append additional text in a graph. Also, we can rotate labels as well as set their font and size.

The following is an example of employing these commands:

To put the label at any place, we apply the command

This command puts “ text ” 0 at position 0 , 0 with respect to the X and Y axes.

6.5.4 Font

By default, gnuplot uses the Helvetica font at 14pt. But sometimes, we need graphs with different fonts. To change the font in the axis labels and legend, we use the command

The quotation marks around the name of the font are required. The size is specified in pixels after the font.

We can change the font in the title of a graph using the font option of the command set title:

Note that this syntax is quite different. The font and size are enclosed in quotes and separated by the comma. We can similarly change the font of the axis labels:

Now a graph will be drawn with specified fonts.

6.5.5 Grid

We can draw grid lines for minor tic marks specifying

Here we should follow how gnuplot defines minor tic marks. If they are determined wrong, then they should be directly specified. For example,

On the other hand, if we need to remove grid lines for minor or major tic marks, we employ the following command: