Skip to content

Gnuplot

What is Gnuplot?

Gnuplot is a portable command-line driven graphing utility for creating 2D and 3D plots from mathematical functions or data files. It is widely used in scientific computing for generating publication-quality graphs.

Base Environment

Gnuplot is provided via the environment modules system.

Loading Gnuplot

Load module

module load gnuplot/5.5

Best Practice

Always verify the loaded version:

module list
gnuplot --version

Example 1: Quick ASCII Plot (Interactive)

For quick visualization directly in the terminal:

ASCII terminal output

gnuplot -e "set terminal dumb; plot sin(x)"

Use Case

Perfect for quick data checks on login nodes or in scripts where you need immediate visual feedback without creating files.

Example 2: Function to Image File

Create a plot from a mathematical function and save as PNG.

plot_sin.gp

Plot sine function

set terminal png size 800,600
set output 'sin_wave.png'
set title "Sine Wave"
set xlabel "x"
set ylabel "sin(x)"
set xrange [0:2*pi]
set grid
plot sin(x) with lines linewidth 2 title "sin(x)"

Generate plot

gnuplot plot_sin.gp

Example 3: Plotting from Data Files

Basic data file format

data.txt

# x y
0.0 0.0
1.0 0.841
2.0 0.909
3.0 0.141
4.0 -0.757

Gnuplot script to plot from file

plot_data.gp

set terminal png
set output 'data_plot.png'
plot 'data.txt' using 1:2 with linespoints title "Data"

Run with:

gnuplot plot_data.gp

Example 4: Multiple Plots

Plot multiple functions

multi_plot.gp

set terminal png size 1000,600
set output 'multi_plot.png'
set title "Multiple Functions"
plot sin(x) title "sin(x)", \
     cos(x) title "cos(x)", \
     tan(x) title "tan(x)"

Run with:

gnuplot multi_plot.gp

Example 5: Complex 3D Data Visualization (SLURM)

This example demonstrates plotting the Lorenz attractor (chaotic system) in 3D from data file using a SLURM job.

Requirements

This example requires Python with NumPy for data generation. See the Conda documentation for setting up Python environments on PERUN.

lorenz_plot.sh

SLURM job for 3D Lorenz attractor

#!/bin/bash
#SBATCH --job-name=gnuplot_lorenz
#SBATCH --partition=CPU
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --mem=2G
#SBATCH --time=00:05:00
#SBATCH --output=lorenz_%j.out

module load gnuplot/5.5

# Generate Lorenz attractor data
python3 << 'PYTHON'
import numpy as np

def lorenz(x, y, z, s=10, r=28, b=2.667):
    x_dot = s*(y - x)
    y_dot = r*x - y - x*z
    z_dot = x*y - b*z
    return x_dot, y_dot, z_dot

dt = 0.01
num_steps = 10000

xs = np.empty(num_steps)
ys = np.empty(num_steps)
zs = np.empty(num_steps)

xs[0], ys[0], zs[0] = (0., 1., 1.05)

for i in range(num_steps - 1):
    x_dot, y_dot, z_dot = lorenz(xs[i], ys[i], zs[i])
    xs[i + 1] = xs[i] + (x_dot * dt)
    ys[i + 1] = ys[i] + (y_dot * dt)
    zs[i + 1] = zs[i] + (z_dot * dt)

with open('lorenz_data.txt', 'w') as f:
    f.write("# x y z\n")
    for i in range(num_steps):
        f.write(f"{xs[i]} {ys[i]} {zs[i]}\n")
PYTHON

# Create gnuplot script for 3D plot
cat > plot_lorenz.gp << 'GNUPLOT'
set terminal png size 1200,900
set output 'lorenz_attractor.png'
set title "Lorenz Attractor"
set xlabel "X"
set ylabel "Y"
set zlabel "Z"
splot 'lorenz_data.txt' using 1:2:3 with lines notitle linecolor rgb "blue"
GNUPLOT

# Generate plot
gnuplot plot_lorenz.gp

echo "Generated lorenz_attractor.png"
ls -lh lorenz_attractor.png

Submit job

sbatch lorenz_plot.sh

More Information

Documentation

Official Gnuplot documentation:

http://www.gnuplot.info/documentation.html