Skillversitas

Plotting

Biopython's built-in plotting centers on Bio.Phylo (drawing phylogenetic trees) and on using matplotlib directly for sequence and alignment statistics. The pattern is the same everywhere: compute the data in Biopython, plot it in matplotlib, and let the library handle the biology.

Drawing Phylogenetic Trees (Bio.Phylo)

from Bio import Phylo
import matplotlib.pyplot as plt

tree = Phylo.read("tree.nwk", "newick")
fig, ax = plt.subplots(figsize=(8, 6))
Phylo.draw(tree, ax=ax, do_labels=True)
ax.set_title("Example tree")
plt.show()

# or save directly:
Phylo.draw(tree, do_labels=True)
plt.savefig("tree.png")

Tree Plotting Options

  • Phylo.draw with ax for full matplotlib control (fonts, colors, branch widths).
  • Phylo.draw_graphviz for layout by Graphviz (dot) - good for large, dense trees.
  • Color branches or tips by metadata (e.g., species or clade) via the color parameter or per-branch styling.

Alignment and Sequence Plots

from Bio import AlignIO
import matplotlib.pyplot as plt

aln = AlignIO.read("family.clustal", "clustal")

# per-column diversity heatmap-style (simple version)
diversity = []
for col in aln.columns:
    unique = set(col) - {"-"}
    diversity.append(len(unique))

plt.plot(diversity)
plt.xlabel("alignment column"); plt.ylabel("unique residues")
plt.title("Column diversity across the MSA")
plt.show()

Expression-Style Heatmaps

import numpy as np
import matplotlib.pyplot as plt

# z-score each row (gene) of a matrix, then plot as image
matrix = np.array([[1.0, 2.0, 1.5], [5.0, 4.8, 5.1], [0.1, 0.2, 0.05]])
z = (matrix - matrix.mean(axis=1, keepdims=True)) / matrix.std(axis=1, keepdims=True)
plt.imshow(z, aspect="auto", cmap="RdBu_r")
plt.colorbar(label="z-score")
plt.show()

Plotting Conventions for Papers

  • Save figures as PDF/SVG for vector quality in manuscripts (plt.savefig("fig.pdf")).
  • Set a consistent style once: matplotlib.rcParams for fonts, sizes, and DPI.
  • Label axes with units; title figures descriptively ("GC content across windows").

Summary

Plot biology in Python with Bio.Phylo.draw for trees (with matplotlib axes or Graphviz layout) and matplotlib for everything else: alignment diversity profiles, GC windows, expression heatmaps. Compute in Biopython, draw in matplotlib, and export vector formats for publication-quality figures.