31 GNUPLOT

Mr. Hardik Joshi

epgp books

 

Overview of GNUPLOT

 

In this module, we will learn a plotting software that is used to create 2D and 3D plots. As you are aware that there are wide range of GUI software and spreadsheet packages that help us to create graphs and plots using a graphical interface. However, if we want to use mathematical expressions to plot charts, such kind of software will be of less help to us. Plotting features are available with various software like R, Python, etc. but they require programming knowledge of these languages. In this module, we will learn an open source tool GNUPLOT that does not require programming knowledge, by writing small scripts, we can generate efficient plots that can be of 2D or 3D.

 

Gnuplot is an open source command-line program, it is widely used by Engineers, Scientists, Data Analysts and Educators. It is used to generate 2D and 3D plots, these plots can be generated with the help of data or by using functions and mathematical expressions. It is distributed under GNU GPL and intially released in 1986. GNUPLOT runs on major OS like Linux, Unix, Windows, MacOS etc. Gnuplot can produce output directly on screen, or in many formats of graphics files, including Portable Network Graphics (PNG), Encapsulated PostScript (EPS), Scalable Vector Graphics (SVG), JPEG and many others. It is also capable of producing LaTeX code that can be included directly in LaTeX documents. Gnuplot can be used both interactively and in batch mode using scripts

 

Installation

Gnuplot can be downloaded from the website :https://sourceforge.net/projects/gnuplot/.The software is available in binary form for Windows and is also available as open source or as repository for various linux distributions. We can install gnuplot for Redhat base distributions or for Ubuntu based distributions using the following command:

 

Interface of Gnuplot

 

Gnuplot has its own command line interface. We can see the interface by issuing gnuplot command on $ prompt in linux. The following screen illustrates the interface of Gnuplot:

 

Figure 1: Interface of GNUPLOT

 

Gnuplot comes with its own default settings. For instance, when we plot data, it will be displayed with points and the functions are plotted with lines. There are default colors used for different functions. These default settings can be seen by issuing the following command:

There is always a default working directory associated so that the scripts can be read from the default directory. This default directory can be displayed using the following pwd command. The following screen shows output of “pwd” and “show style” commands:

.

Figure 2: Output of pwd and show style commands

 

Plotting functions

 

We can plot mathematical expressions and functions using gnuplot. For single function, we can simply call the function from command prompt. However, if we want to configure the output in desired format, there is a facility of writing scripts to configure the details of the output. Let us explore the commands and its related output using the gnuplot software.

 

Writing Scripts

 

When we want to plot a more customization plot where different functions can be shown with different colors and styles, we write small scripts to manage desirable plots. In the following script, we have configured the border style, line style and titles. We have used two plots, each represented by f(x) and g(x) functions.

 

 

The output of above mentioned script is shown in figure 10. It must be noted that in the output, we have given title to f(x) plot and the linestyle is distinct from that of g(x) plot.

 

 

Plotting data from file

 

We can plot data residing in file using gnuplot software. Let us seen an example of ploting some sample data where the data is used from a file. In the following example, our data is stored in a file “data1.dat”. By issuing the following command, the data will be displayed in the desired line format. The output of the command is shown in figure 12 and the data is shown in figure 11.

While plotting data from a file, the data must be in columnar format as shown in above figure. We must also take care that the comments are used with a # sign.

 

Plotting Histogram

 

Let us see an example where we plot a histogram using the gnuplot software. Although various types of charts can be plot, we will see a sample example of plotting histogram from some data file.

 

Using the above script, we have plot a histogram as shown in figure 14. The histogram is plotted using data from “data.dat” file. The content of data file is shown in figure 13. As you can observe in the script, we have made labeling of x-axis in customized format. In the above script, we have used labels of x-axis as one of the columns that is the date. When we plot the histogram, the data is chosen from columns 2 to columns 4 and the labels are taken from column 1. These details are mentioned using the plot command. As shown in the script, the plot command takes filename, and labels of x-axis to generate the histogram.

A variety of 2D and 3D plots can be generated using the gnuplot tool. There is a dedicated website that demonstrates different plots, the website www.gnuplot.info lists many scripts and their corresponding plots. Gnuplot can be integrated with other software like Latex, Octave, etc. to generate plots with the software environment.

Let us summarize the key concepts covered in this module

  • Overview of Gnuplot
  • Plotting functions
  • Modifying styles while plotting
  • Plotting from data file
  • Plotting a histogram using script
you can view video on GNUPLOT

References for GNUPLOT:

  1. GNUPlot Home Page. http://gnuplot.info/
  2. Lee. Philipps. 2012. Gnuplot cookbook : over 80 recipes to visually explore the full range of features of the world’s preeminent open source graphing system, Packt Publishing.
  3. Jeroen Janssens. Data science at the command line,
  4. Thomas Williams and Colin Kelley. 2015. Gnuplot 5.0 Reference manual, Samurai Media Limited.
  5. Philipp K. Janert. Gnuplot in action : understanding data with graphs, Packt Publishing