Open In App

Plotting Functions in Gnuplot

Improve
Improve
Like Article
Like
Save
Share
Report

Gnuplot is a portable, free, command-driven, interactive function, and data plotting program for Linux, OS/2, MS Windows, OSX, VMS, and many other platforms. Although copyrighted, the source code is freely available. It was initially developed to enable researchers and students to interactively visualize mathematical functions and data, but it has since expanded to cover a wide range of non-interactive purposes, including web scripting. Third-party programs like Octave also use it as a plotting engine. Numerous plot types are supported by Gnuplot in both 2D and 3D. It can draw utilizing a variety of associated text as well as lines, points, boxes, contours, vector fields, and surfaces. Additionally, it supports a number of specialty plot kinds. Gnuplot provides output in a variety of file formats like eps, emf, fig, jpeg, LaTeX, pdf, png, postscript, etc.

Installation:

  • Download Gnuplot software from here.
  • Double-click the downloaded executable file.
  • Follow the instructions on the installation wizard to install Gnuplot.
  • After successful installation, start Gnuplot and a screen like this will appear.
Gnuplot

 

Plotting One Function

1. Without setting xrange and yrange: Plotting function in gnuplot is easy. In gnuplot, the exponentiation ^ uses ** so to plot a simple function like 

f(x) = exp(-x^4 / 4)

In Gnuplot, the function becomes:

f(x) = exp(-x**4 / 4) 

Type the below command:

plot exp(-x**4 / 4) 

Plot 1

 

Output: 

plot exp(-x**4 / 4)

 

2. Setting xrange and yrange: One can opt to specify first the xrange and then the yrange in the form [minimum:maximum] before the function. One can also leave yrange or both. Below is the revised version of the above example:

plot [-4:4] exp(-x**4 / 4)

Output:

plot [-4:4] exp(-x**4 / 4)

 

Plotting More than One Function

To plot more than one function, list all the functions separated by comma. Below is the example to implement the concept:

plot [-4:4] exp(-x**2 / 2), x**2 / 16

Output:

plot [-4:4] exp(-x**2 / 2), x**2 / 16

 

Defining Function

It is possible to define the function once so that there is no need to retype it each time. Below is the example to implement the concept:

f(x) = exp(-x**4 / 4)

plot [t=-4:4] f(t), t**2 / 16

plot [t=-4:4] f(t), t**2 / 16

 

Output:

plot [t=-4:4] f(t), t**2 / 16

 

Plotting Trigonometric Function

Below is the example to plot a sine and cosine function:

a = 0.9
f(x) = a * sin(x)
g(x) = a * cos(x)
# Plot
plot f(x) title ‘sin(x)’ with lines linestyle 1, \
    g(x) notitle with lines linestyle 2

Here,
f(x) and g(x) is the function.
Statements starting with # are the comments.

Sine and cosine plot

 

Output:

sine and cosine plot

 


Last Updated : 28 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads