How to Visualize MD Trajectories in VMD and PyMOL: Complete Tutorial
Running an MD simulation without visualizing the trajectory is like developing film without looking at the photos. This tutorial covers everything you need to load, navigate, clean up, and render your trajectory in both VMD and PyMOL — the two programs every MD researcher uses.
VMD vs PyMOL: which should you use?
The practical answer is: use both. VMD for trajectory exploration and dynamic analysis; PyMOL for creating the figures that go in your paper. They serve different purposes and complement each other well. This tutorial covers both.
VMD is free and available at ks.uiuc.edu/Research/vmd. The open-source PyMOL version is installable via conda: conda install -c conda-forge pymol-open-source.
Before you visualize: prepare the trajectory
Raw GROMACS trajectories cannot be loaded directly into VMD or PyMOL without preparation. The periodic boundary conditions cause atoms to “jump” across box boundaries, splitting molecules across opposite faces of the simulation box. You must center and wrap the trajectory first.
If you haven’t already done this from the analysis tutorial, run:
# Step 1: fix PBC — make molecules whole across boundaries
gmx trjconv \
-s md.tpr \
-f md.xtc \
-o md_whole.xtc \
-pbc whole
# Select: System
# Step 2: center protein in box
gmx trjconv \
-s md.tpr \
-f md_whole.xtc \
-o md_centered.xtc \
-center \
-pbc mol \
-ur compact
# Center group: Protein
# Output group: System
You also need a GRO or PDB structure file to load alongside the trajectory — use your energy-minimized structure em.gro or convert it: gmx editconf -f em.gro -o structure.pdb. VMD and PyMOL both use this as the topology reference when reading the trajectory.
md.xtc directly will show molecules splitting apart and teleporting across the box. This looks dramatic but is entirely an artifact. Always load md_centered.xtc. If the protein still looks fragmented after centering, run the two-step trjconv procedure above — single-step centering sometimes misses certain cases.
Visualizing in VMD
Loading the trajectory
Open VMD. Go to File → New Molecule. Browse to your structure file (structure.pdb) and click Load. Then — with the same molecule selected in the Molecule Browser — click Load files for, browse to md_centered.xtc, set the file type to GROMACS XTC, and click Load.
Alternatively, load everything from the command line which is faster for repeat work:
mol new structure.pdb
mol addfile md_centered.xtc type xtc waitfor all
Changing the representation
The default VDW (ball-and-stick) representation is overwhelming for a full protein. In the VMD Main window, go to Graphics → Representations. In the Drawing Method dropdown, common useful representations are:
- NewCartoon — smooth ribbon diagram. Best for showing protein secondary structure and overall fold changes
- Licorice — sticks for specific residues or ligands
- QuickSurf — molecular surface. Useful for showing binding pockets
- VDW — van der Waals spheres. Good for small molecules
A clean standard setup for protein-ligand visualization: Create two representations. Set the first to NewCartoon with selection protein. Add a second representation, set it to Licorice with selection resname LIG (substitute your ligand residue name). Color the ligand by element for clarity.
Navigating the trajectory
Use the VMD Main window bottom controls to play, pause, and scrub through frames. The slider moves through the trajectory — each position corresponds to one saved frame. For a 1 ns simulation saved every 10 ps, you have 100 frames.
To loop a specific portion of the trajectory, set Start and End frame numbers in the animation controls and enable looping. This is useful for showing a specific conformational event without playing through the entire trajectory.
Visualizing in PyMOL
Loading a trajectory in PyMOL
PyMOL handles GROMACS trajectories directly. Load the structure file first, then add the trajectory as additional states:
# Load structure and trajectory
load structure.pdb, protein
load_traj md_centered.xtc, protein, interval=10
# interval=10 loads every 10th frame — adjust to manage file size
# Each loaded frame becomes a 'state' in PyMOL
The trajectory frames load as states of the protein object. Navigate between frames using the state controls at the bottom right of the PyMOL window, or use the play button to animate.
Setting up a publication-quality view
# Clean base representation
hide everything
show cartoon, protein
show sticks, resname LIG
color gray80, protein
color cyan, resname LIG
# Show binding site residues as sticks
select binding_site, protein and (byres resname LIG around 5)
show sticks, binding_site
color wheat, binding_site
# Remove water from view (if still present)
hide everything, resname SOL HOH
# Set background and render settings
bg_color white
set ray_shadows, 0
set ambient, 0.4
set spec_reflect, 0.2
Saving a specific frame as a figure
# Navigate to a specific frame (state number)
set state, 50 # frame 50
# Render high-resolution PNG
ray 2400, 1800 # width x height in pixels
png figure_frame50.png, dpi=300
gmx rmsf -oq bfactors.pdb to get the B-factor-weighted average structure, then load that as your figure reference.
Measuring distances over time
One of the most common things to measure during trajectory visualization is how the distance between two specific atoms or residues changes over time — for example, the distance between a ligand carboxylate oxygen and a protein arginine nitrogen, or the distance across a binding site.
In VMD
VMD can plot distances as a function of frame number using the Timeline plugin or the Distance label tool. The most reliable method for publication is the Tcl console:
# Measure distance between two atoms over all frames
# Replace 1234 and 5678 with actual atom indices from atom selection
set sel1 [atomselect top "resid 120 and name NH2"]
set sel2 [atomselect top "resname LIG and name O1"]
set outfile [open distances.dat w]
set nframes [molinfo top get numframes]
for {set i 0} {$i < $nframes} {incr i} {
$sel1 frame $i
$sel2 frame $i
set d [measure bond [list [$sel1 get index] [$sel2 get index]]]
puts $outfile "$i $d"
}
close $outfile
puts "Distances written to distances.dat"
In PyMOL
# Quick distance label between two atoms (updates with state changes)
distance d1, /protein//A/ARG`120/NH2, /protein//A/LIG/O1
# For distance over all states using Python in PyMOL
python
distances = []
for state in range(1, cmd.count_states('protein') + 1):
d = cmd.get_distance('/protein//A/ARG`120/NH2',
'/protein//A/LIG/O1',
state=state)
distances.append((state, d))
with open('pymol_distances.txt', 'w') as f:
for s, d in distances:
f.write(f'{s} {d:.3f}\n')
python end
Creating publication movies
In VMD
VMD’s Movie Maker plugin (Extensions → Visualization → Movie Maker) provides a GUI for rendering trajectory movies. Key settings for a publication-quality output:
- Renderer: Tachyon — ray-traced, significantly better quality than the default OpenGL renderer
- Format: MPEG-4 or a sequence of TIFF frames (assemble with ffmpeg for more control)
- Resolution: 1920×1080 minimum for journal submission; 3840×2160 for supplementary material
For fine-grained control, render frames individually from the Tcl console and assemble with ffmpeg:
ffmpeg \
-framerate 30 \
-i frame%04d.tga \
-c:v libx264 \
-pix_fmt yuv420p \
-crf 18 \
movie.mp4
In PyMOL
# Render all frames as a movie
set ray_trace_frames, 1 # ray trace each frame (slower but higher quality)
set cache_frames, 0 # don't cache — saves memory for long trajectories
mpng frames/frame_, ray=1 # saves frame_0001.png, frame_0002.png, etc.
Assemble the PNG frames with ffmpeg using the same command as above.
Common visualization mistakes
-
Loading the raw trajectory without PBC correctionThe most common mistake. Atoms appear to teleport across the box, molecules split in half, and the protein looks like it’s falling apart. This is entirely a visualization artifact — the simulation is fine.Always run gmx trjconv with -pbc whole followed by -center -pbc mol before loading in VMD or PyMOL.
-
Using the first frame as the publication figureThe first frame is the starting crystal structure. It doesn’t represent the equilibrated simulation state. Reviewers and readers may reasonably ask how the figure frame was chosen.Choose a representative frame from the stable portion of the trajectory — typically after RMSD has plateaued. Describe how the representative frame was selected in the methods or figure caption.
-
Showing water molecules in publication figuresDisplaying explicit water clutters the figure and makes it impossible to see the protein. There are almost no contexts in which showing water in a publication figure improves understanding.In VMD: use the selection
not water. In PyMOL:hide everything, resname SOL HOH. -
Not smoothing the trajectory before visual inspectionRaw MD trajectories are jerky — each frame shows thermal fluctuations. Watching the raw trajectory makes it hard to see genuine conformational events.In VMD, use Extensions → Analysis → Trajectory Smoother to apply a running average. In PyMOL, load every Nth frame with the interval parameter in load_traj. For movies, smoothing 5–10 frames produces much more interpretable visualization.
-
Measuring distances in the unprocessed trajectoryA distance measurement between a protein residue and a ligand on opposite sides of a periodic boundary will give a large, meaningless value (the box dimension rather than the true distance).Always measure distances in the centered, PBC-corrected trajectory. For critical measurements, also verify with gmx distance on the processed trajectory — it handles PBC correctly regardless.
Two tools, two purposes
Use VMD for trajectory exploration — it handles large trajectories efficiently, makes distance-over-time analysis straightforward, and produces good movies with the Tachyon renderer. Use PyMOL for publication figures — its rendering is cleaner, the Python API gives you precise control over every visual element, and its output format is what reviewers and journals expect. Always prepare your trajectory with GROMACS trjconv before loading in either program, and never use the raw unwrapped trajectory for any visualization or measurement.