Section B.14 Optional Plotting
This optional section helps you read optional visualization code. It is not part of the core mathematical computation.
import matplotlib.pyplot as plt
plt.figure(figsize=(5, 4))
plt.imshow(A)
plt.colorbar(label="attention weight")
plt.title("Attention weights")
plt.xlabel("Key index")
plt.ylabel("Query index")
plt.xticks(range(len(tokens)), tokens)
plt.yticks(range(len(tokens)), tokens)
plt.show()
Some optional geometric transformation pictures use line plots instead of image plots.
plt.figure(figsize=(4, 4))
plt.plot(X_square[0], X_square[1], "o-", label="original")
plt.plot(Y_square[0], Y_square[1], "o-", label="transformed")
plt.axis("equal")
plt.grid(True)
plt.legend()
plt.show()
fig, ax = plt.subplots(figsize=(5, 4))
ax.plot(xs, ys)
plt.show()
Figure setup.
plt.figure(...)
Read as. Start a figure.
Output. A figure.
Used for. Plotting setup.
Figure and axes setup.
plt.subplots(...)
Read as. Start a figure together with an axes object.
Output. A figure and axes pair.
Used for. Optional plot customization.
Matrix image.
plt.imshow(A)
Read as. Display a matrix as an image.
Output. An image plot.
Used for. Attention weights and matrices.
Color scale.
plt.colorbar(...)
Read as. Add a scale bar.
Output. A colorbar.
Used for. Matrix image interpretation.
Labels.
Read as. Add labels.
Output. Text labels.
Used for. Readable plots.
Tick labels.
Read as. Add tick labels.
Output. Axis labels.
Used for. Token labels.
Line plot.
plt.plot(...)
Read as. Draw connected points or a curve.
Output. A line plot.
Used for. Geometry pictures.
Equal scales.
plt.axis("equal")
Read as. Use equal scales.
Output. A plot setting.
Used for. Geometric accuracy.
Grid.
plt.grid(True)
Read as. Show a grid.
Output. A plot setting.
Used for. Optional geometry plots.
Legend.
plt.legend()
Read as. Show labels.
Output. A legend.
Used for. Comparing curves.
Display figure.
plt.show()
Read as. Display the figure.
Output. A rendered plot.
Used for. Notebooks.
