AtomLearn
DashboardGoalsGraphAchievementsReviewSign In
DashboardData VisualizationAnatomy of a matplotlib Figure
Data VisualizationNot Started

Anatomy of a matplotlib Figure

Figure, Axes, axis — understand the structure before you plot

0%

Knowledge Debt detected

You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.

Explanation

Understanding matplotlib's hierarchy prevents confusion when customizing plots.

The three levels:

  • Figure — the entire canvas (the window or image file)
  • Axes — a single plot area within the figure (what you actually draw on)
  • Axis — the x or y axis line with ticks and labels
python
import matplotlib.pyplot as plt

# Create figure and axes explicitly (recommended) fig, ax = plt.subplots() ax.plot([1, 2, 3], [4, 5, 6]) ax.set_title('My Plot') ax.set_xlabel('X axis') ax.set_ylabel('Y axis') plt.show()

# Multiple subplots fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4)) ```

Two styles — prefer the explicit (ax) style:

```python # Implicit (plt.*) — OK for quick plots plt.plot([1,2,3], [4,5,6]) plt.title('Quick') plt.show()

# Explicit (ax.*) — better for customization and multiple plots fig, ax = plt.subplots() ax.plot([1,2,3], [4,5,6]) ```

Examples

Figure with two subplots

tight_layout() prevents labels from overlapping

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2*np.pi, 100)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))

ax1.plot(x, np.sin(x))
ax1.set_title('Sine')
ax1.set_xlabel('x')

ax2.plot(x, np.cos(x), color='red')
ax2.set_title('Cosine')
ax2.set_xlabel('x')

plt.tight_layout()
plt.show()

Next in Data Visualization

Line Plots

Continue

Unlocks