Figure Axes and Axis

Loading concept...

🎨 Matplotlib Foundations: Figure, Axes & Axis

The Art Gallery Analogy 🖼️

Imagine you’re setting up an art gallery. Before you hang any paintings, you need:

  1. A Wall → This is the Figure (the entire canvas)
  2. Picture Frames → These are the Axes (where plots actually live)
  3. Frame Rulers → These are the Axis (the numbered lines on edges)

Let’s explore each one!


1. The Figure Object 📋

What is a Figure?

Think of a Figure as a blank wall in your art gallery.

  • It’s the entire window that holds everything
  • You can’t paint directly on the wall—you need frames first!
  • One wall can hold many picture frames

Simple Example

import matplotlib.pyplot as plt

# Create your "wall" (Figure)
fig = plt.figure()

# Set wall size (width, height in inches)
fig = plt.figure(figsize=(8, 6))

What Can You Do With a Figure?

Action Code What It Does
Create plt.figure() Makes a new blank wall
Size it figsize=(8, 6) Sets width & height
Save it fig.savefig('art.png') Saves to a file
Close it plt.close(fig) Removes from memory

Real Life Example 🏠

import matplotlib.pyplot as plt

# Create a wall (Figure)
fig = plt.figure(figsize=(6, 4))

# Wall is empty right now!
# We need frames (Axes) to draw on

plt.show()

Result: An empty window appears. It’s like walking into an empty gallery!


2. The Axes Object 🖼️

What is Axes?

Axes is the picture frame where your actual drawing lives.

  • This is where you put your lines, bars, scatter points
  • One Figure can have many Axes (like a wall with many frames)
  • The “s” in Axes is NOT plural—it’s just the name!

The Magic Trick ✨

import matplotlib.pyplot as plt

# Create wall + frame together!
fig, ax = plt.subplots()

# Now draw on the frame
ax.plot([1, 2, 3], [1, 4, 9])
plt.show()

Multiple Frames on One Wall

Want 2 charts side by side? Easy!

import matplotlib.pyplot as plt

# 1 row, 2 columns of frames
fig, (ax1, ax2) = plt.subplots(1, 2)

ax1.plot([1, 2, 3], [1, 2, 3])
ax1.set_title("Line Going Up")

ax2.bar([1, 2, 3], [3, 1, 2])
ax2.set_title("Some Bars")

plt.show()

What Can Axes Do?

You Want To… Code
Draw a line ax.plot(x, y)
Make bars ax.bar(x, y)
Scatter dots ax.scatter(x, y)
Add title ax.set_title("Hi!")
Label x-axis ax.set_xlabel("Time")
Label y-axis ax.set_ylabel("Money")

3. Figure vs Axes: The Big Difference 🎯

This confuses everyone! Let’s clear it up:

graph TD A[Figure = The Wall] --> B[Axes 1<br/>Picture Frame] A --> C[Axes 2<br/>Picture Frame] A --> D[Axes 3<br/>Picture Frame] B --> E[Your Plot Lives Here!] C --> F[Another Plot Here!] D --> G[And Another Here!]

Side-by-Side Comparison

Figure Axes
The whole window A single plot area
Contains Axes Contains your drawing
One per chart Can have many
Like a wall Like a picture frame
plt.figure() fig.add_subplot() or plt.subplots()

The Golden Rule 🌟

Figure = Container (the wall)

Axes = Where you actually draw (the frame)

Visual Story

Imagine you want to show your friend 4 different charts:

import matplotlib.pyplot as plt

# One wall, 4 frames (2 rows × 2 cols)
fig, axes = plt.subplots(2, 2)

# axes is now a 2×2 grid!
axes[0, 0].plot([1, 2], [1, 2])
axes[0, 1].bar([1, 2], [2, 1])
axes[1, 0].scatter([1, 2], [2, 1])
axes[1, 1].pie([30, 70])

plt.tight_layout()  # Prevent overlap
plt.show()

Result: 4 charts in one window—like hanging 4 paintings on one wall!


4. The Axis Object 📏

What is Axis?

Axis (no “e” at the end!) is the ruler on the edge of your frame.

  • The X-axis runs left-to-right (horizontal)
  • The Y-axis runs bottom-to-top (vertical)
  • Each Axes has two Axis objects

Quick Picture

        Y-Axis (vertical ruler)
           ↑
           │
           │    [Your Plot]
           │
           └──────────────→ X-Axis (horizontal ruler)

Accessing Axis Objects

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])

# Get the rulers
x_axis = ax.xaxis
y_axis = ax.yaxis

# Change tick marks
ax.set_xticks([1, 2, 3])
ax.set_yticks([0, 5, 10])

plt.show()

Common Axis Tweaks

Want To… Code
Set tick positions ax.set_xticks([0, 5, 10])
Custom tick labels ax.set_xticklabels(['A', 'B', 'C'])
Hide ticks ax.tick_params(bottom=False)
Limit range ax.set_xlim(0, 100)
Grid lines ax.grid(True)

Example: Customizing Your Rulers

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [10, 20, 15, 25])

# X-axis customization
ax.set_xlabel("Months")
ax.set_xticks([1, 2, 3, 4])
ax.set_xticklabels(['Jan', 'Feb', 'Mar', 'Apr'])

# Y-axis customization
ax.set_ylabel("Sales ($)")
ax.set_ylim(0, 30)

plt.show()

🎯 Putting It All Together

Here’s the complete picture:

graph TD F[FIGURE<br/>The Wall] --> A1[AXES 1<br/>Picture Frame] F --> A2[AXES 2<br/>Picture Frame] A1 --> X1[X-AXIS<br/>Bottom Ruler] A1 --> Y1[Y-AXIS<br/>Left Ruler] A1 --> P1[YOUR PLOT<br/>Lines & Dots] A2 --> X2[X-AXIS<br/>Bottom Ruler] A2 --> Y2[Y-AXIS<br/>Left Ruler] A2 --> P2[YOUR PLOT<br/>Bars & Pies]

The Complete Code Story

import matplotlib.pyplot as plt

# 1. Create Figure (the wall)
fig = plt.figure(figsize=(10, 4))

# 2. Add Axes (picture frames)
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)

# 3. Draw on Axes
ax1.plot([1, 2, 3], [1, 4, 9])
ax2.bar(['A', 'B', 'C'], [5, 3, 7])

# 4. Customize Axis (rulers)
ax1.set_xlabel("X Values")
ax1.set_ylabel("Y Values")
ax2.set_xlabel("Categories")
ax2.set_ylabel("Counts")

# 5. Add titles
ax1.set_title("My Line Plot")
ax2.set_title("My Bar Chart")

plt.tight_layout()
plt.show()

🚀 Quick Reference

Term What It Is Analogy
Figure The entire window Art gallery wall
Axes A single plot area Picture frame
Axis The numbered lines Rulers on frame
X-Axis Horizontal ruler Left-to-right
Y-Axis Vertical ruler Bottom-to-top

💡 Pro Tips

  1. Use plt.subplots() — It creates Figure AND Axes together!
  2. Remember: ax.plot() not fig.plot() — You draw on frames, not walls
  3. tight_layout() — Prevents your frames from overlapping
  4. Axes ≠ Axis — Axes is the frame, Axis is the ruler

🎉 You Did It!

Now you understand the three building blocks of every Matplotlib chart:

  1. Figure — Your canvas (the wall)
  2. Axes — Your drawing area (the frame)
  3. Axis — Your measurement guides (the rulers)

Every chart you’ll ever make uses these three pieces. You’ve just unlocked the foundation of data visualization!

Happy plotting! 📊✨

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.