Anatomy of a Plot
Matplotlib anatomy
A typical plot has:
- Title
- X label / Y label
- Axes limits
- Ticks
- Legend (if multiple series)
- Grid (optional)
The Figure + Axes model
Figure + Axes
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot([1, 2, 3, 4], [10, 6, 8, 4])
ax.set_title("Anatomy")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.grid(True, alpha=0.3)
plt.show()Figure + Axes
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot([1, 2, 3, 4], [10, 6, 8, 4])
ax.set_title("Anatomy")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.grid(True, alpha=0.3)
plt.show()Limits and ticks
Limits and ticks
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot([1, 2, 3, 4], [10, 6, 8, 4])
ax.set_xlim(0, 5)
ax.set_ylim(0, 12)
ax.set_xticks([1, 2, 3, 4])
ax.set_yticks([0, 4, 8, 12])
ax.set_title("Limits and ticks")
plt.tight_layout()
plt.show()Limits and ticks
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot([1, 2, 3, 4], [10, 6, 8, 4])
ax.set_xlim(0, 5)
ax.set_ylim(0, 12)
ax.set_xticks([1, 2, 3, 4])
ax.set_yticks([0, 4, 8, 12])
ax.set_title("Limits and ticks")
plt.tight_layout()
plt.show()Tip
Use the object-oriented approach for consistent, reusable plotting.
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
