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¶
Example 1: Quick ASCII Plot (Interactive)¶
For quick visualization directly in the terminal:
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
Example 3: Plotting from Data Files¶
Gnuplot script to plot from file
plot_data.gp
Run with:
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:
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