import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

filename = "potential.xvg"

time = []
potential = []

with open(filename, "r") as f:
    for line in f:
        if line.startswith("#") or line.startswith("@"):
            continue
        
        parts = line.split()
        if len(parts) >= 2:
            time.append(float(parts[0]))
            potential.append(float(parts[1]))

plt.figure(figsize=(10, 6))
plt.plot(time, potential, color="navy", linewidth=1.5)

plt.xlabel("EM Step", fontsize=14)
plt.ylabel("Potential energy (kJ/mol)", fontsize=14)

plt.xticks(fontsize=14)
plt.yticks(fontsize=14)

formatter = ScalarFormatter(useMathText=True)
formatter.set_scientific(True)
formatter.set_powerlimits((0, 0))   
plt.gca().yaxis.set_major_formatter(formatter)
plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))

plt.gca().yaxis.get_offset_text().set_fontsize(14)

plt.grid(True, linestyle="--", alpha=0.5)

plt.tight_layout()
plt.savefig("potential_energy.png", dpi=300, bbox_inches="tight")

plt.show()