Creating 2D Line Plot using GNU Plot Software

2D Graph Plot

A simple Gnuplot script designed to generate a customized 2D line plot of potential energy data (presumably vs. simulation steps or time), using data from a file named PE.txt (available in GitHub). The script applies several visual formatting settings to make the plot visually appealing and informative.

This script produces a clean, stylized 2D plot of potential energy vs. simulation steps with (1) custom fonts, colors, and line styles, (2) no legend, (3) margins around the x-axis, and (4) automatic y-axis scaling.

Source Code

set title '{/Times-New-Roman=14:Bold Potential Energy Plot}' tc rgb '#167116'
set xlabel '{/Arial:Italic Number of steps}' tc rgb 'red'
set ylabel '{/Arial:Italic Potential energy}' tc rgb 'red'
set style line 1 lt 1 lc rgb '#f70453' lw 0.5
set grid layerdefault lt 0 lc rgb 'blue' lw 0.5
set border lt 1 lc rgb 'blue' lw 1
unset key
plot 'PE.txt' with lines ls 1
set xrange [GPVAL_DATA_X_MIN-0.5:GPVAL_DATA_X_MAX+0.5]
set yrange [*:*]
replot

Line-by-line explanation of the script is below:

1. Plot Title

set title '{/Times-New-Roman=14:Bold Potential Energy Plot}' tc rgb '#167116'
  • Title Text: "Potential Energy Plot"
  • Font: Times New Roman, size 14, bold
  • Text Color: RGB color #167116 (a shade of green)
  • tc = text color

2. X and Y Axis Labels

set xlabel '{/Arial:Italic Number of steps}' tc rgb 'red'
set ylabel '{/Arial:Italic Potential energy}' tc rgb 'red'
  • X-axis Label: "Number of steps", italic, Arial font, red
  • Y-axis Label: "Potential energy", same formatting

3. Line Style Configuration

set style line 1 lt 1 lc rgb '#f70453' lw 0.5
  • Defines line style 1:
    • lt 1: Line type 1 (solid)
    • lc rgb '#f70453': Custom RGB color (hot pink/red)
    • lw 0.5: Line width 0.5 (thin line)

4. Grid Lines

set grid layerdefault lt 0 lc rgb 'blue' lw 0.5
  • Adds a blue grid to the plot:
  • lt 0: Line type 0 (dotted or dashed, depending on terminal)
  • lw 0.5: Thin line

5. Border Styling

set border lt 1 lc rgb 'blue' lw 1
  • Sets the border color to blue
  • Line type 1 and width 1 for visibility

6. Hide Legend (Key)

unset key
  • Removes the legend from the plot (useful if only plotting one dataset)

7. Plot Command

plot PE.txt with lines ls 1
  • Plots the data from file PE.txt
  • Uses line style 1, as defined earlier (thin hot pink/red line)

8. Set X Range with Margins

set xrange [GPVAL_DATA_X_MIN-0.5:GPVAL_DATA_X_MAX+0.5]
  • Dynamically adjusts the X-axis limits:
    • GPVAL_DATA_X_MIN and GPVAL_DATA_X_MAX are automatic Gnuplot variables holding the min/max X values from the last plot.
    • Adds a margin of 0.5 units on both sides

9. Set Y Range (Auto)

set yrange [*:*]
  • Automatically scales the Y-axis based on the data (no manual limits)

10. Replot

replot
  • Re-draws the plot with the updated axis ranges

Comments

Most Popular Posts

TNEB Bill Calculator

TNEB Bill Calculator (New)

Technical Questions