Multiple Axes

Loading concept...

🎨 Figure Organization: Multiple Axes in Matplotlib

The Story of the Helpful Picture Frame

Imagine you have a special picture frame. But this isn’t a normal frame—it can hold many pictures at once, all in the same space!

That’s exactly what Multiple Axes in Matplotlib does. It lets you show different information on the same chart, making it super easy to compare things!


🪞 Twin Axes: Two Stories, One Picture

What Are Twin Axes?

Think of twin axes like having twin siblings who share a bedroom but have their own side of the room.

  • One twin sleeps on the left side (left Y-axis)
  • The other twin sleeps on the right side (right Y-axis)
  • But they share the same floor (X-axis)!

Why Use Twin Axes?

Sometimes you want to show two different things that happen at the same time:

  • Temperature AND ice cream sales 🍦
  • Time AND distance traveled 🚗
  • Age AND height of a plant 🌱

Simple Example

import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()

# Left Y-axis: Temperature
ax1.plot([1,2,3,4], [20,25,30,28])
ax1.set_ylabel('Temperature °C')

# Right Y-axis: Ice cream sales
ax2 = ax1.twinx()
ax2.plot([1,2,3,4], [100,150,200,180])
ax2.set_ylabel('Ice Cream Sales')

plt.show()

Magic Word: twinx()

The twinx() function creates a twin brother axis that:

  • Shares the same X-axis (bottom)
  • Has its own Y-axis (right side)

There’s also twiny() which shares Y but has different X!

graph TD A[Original Axis] --> B[twinx] A --> C[twiny] B --> D[Same X, New Y on Right] C --> E[Same Y, New X on Top]

🤝 Sharing Axes: Friends Who Share

What Is Sharing Axes?

Imagine you and your friend are drawing pictures. You both agree to use the same ruler so your drawings match perfectly!

Sharing axes means multiple charts use the same scale. When one chart zooms in, the other zooms in too!

Why Share Axes?

  • Makes comparing things super easy
  • Everything lines up perfectly
  • No confusing different scales

Two Ways to Share

Method 1: When Creating Subplots

fig, (ax1, ax2) = plt.subplots(
    2, 1,
    sharex=True  # Share X-axis!
)

ax1.plot([1,2,3,4], [10,20,15,25])
ax2.plot([1,2,3,4], [5,15,10,20])

plt.show()

Method 2: After Creating

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

ax2.sharex(ax1)  # Now they share!
ax2.sharey(ax1)  # Share Y too!

Share Options

Option What It Does
sharex=True Same X-axis scale
sharey=True Same Y-axis scale
Both Same scale everywhere
graph TD A[Main Chart] -->|sharex| B[Chart 2] A -->|sharey| C[Chart 3] B -->|Zoom X| D[Both zoom together!] C -->|Zoom Y| E[Both zoom together!]

📊 Multiple Y-Axes: Three Friends, One Chart

What Are Multiple Y-Axes?

Remember our twin siblings? Now imagine they have a third sibling!

Multiple Y-axes let you show three or more different measurements on the same chart.

Real World Example

Think about weather:

  • 🌡️ Temperature (left)
  • 💧 Humidity (right)
  • 💨 Wind speed (far right)

All happening at the same time!

How to Add More Y-Axes

import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()

# First Y-axis (left)
ax1.plot([1,2,3], [20,25,22])
ax1.set_ylabel('Temperature')

# Second Y-axis (right)
ax2 = ax1.twinx()
ax2.plot([1,2,3], [60,70,65])
ax2.set_ylabel('Humidity')

# Third Y-axis (further right)
ax3 = ax1.twinx()
ax3.spines['right'].set_position(
    ('outward', 60)
)
ax3.plot([1,2,3], [10,15,12])
ax3.set_ylabel('Wind Speed')

plt.show()

The Secret: Moving the Spine

ax3.spines['right'].set_position(
    ('outward', 60)
)

This pushes the third axis 60 pixels away from the second one!

graph TD A[One Chart] --> B[Y-Axis 1: Left] A --> C[Y-Axis 2: Right] A --> D[Y-Axis 3: Far Right] B --> E[Temperature] C --> F[Humidity] D --> G[Wind Speed]

🔄 Secondary Axis: The Helper Axis

What Is a Secondary Axis?

A secondary axis is like a translator. It shows the same data but in a different language!

Examples:

  • Celsius AND Fahrenheit 🌡️
  • Kilometers AND Miles 🛣️
  • Dollars AND Euros 💰

The Cool Part

The secondary axis automatically converts for you! Change one, the other updates!

Simple Example: Celsius to Fahrenheit

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

# Plot temperature in Celsius
ax.plot([1,2,3,4], [0,10,20,30])
ax.set_ylabel('Celsius')

# Add Fahrenheit scale
def c_to_f(c):
    return c * 9/5 + 32

def f_to_c(f):
    return (f - 32) * 5/9

secax = ax.secondary_yaxis(
    'right',
    functions=(c_to_f, f_to_c)
)
secax.set_ylabel('Fahrenheit')

plt.show()

Key Parts

Part What It Does
secondary_yaxis() Creates helper Y-axis
secondary_xaxis() Creates helper X-axis
functions= How to convert
'right' or 'left' Where to put it

The Conversion Functions

You need TWO functions:

  1. Forward: Convert from main to secondary
  2. Backward: Convert from secondary to main
functions=(forward, backward)
graph TD A[Main Axis] -->|Conversion Function| B[Secondary Axis] B -->|Reverse Function| A C[Celsius: 20] -->|×9/5+32| D[Fahrenheit: 68] D -->|−32×5/9| C

🎯 Quick Summary

Feature What It Does When to Use
Twin Axes Two Y-axes, same X Compare different units
Sharing Axes Same scale across charts Easy comparison
Multiple Y-Axes 3+ Y-axes Many measurements
Secondary Axis Auto-converting axis Show unit conversions

🌟 Remember This!

  1. twinx() = New Y-axis on right, same X
  2. twiny() = New X-axis on top, same Y
  3. sharex=True = Charts zoom together (X)
  4. sharey=True = Charts zoom together (Y)
  5. secondary_yaxis() = Auto-converting helper axis
  6. spines['right'].set_position() = Move axis further out

🎨 Visual Guide

graph TD subgraph "Multiple Axes Family" A[Twin Axes] --> E[2 Y-axes] B[Sharing Axes] --> F[Same scale] C[Multiple Y-Axes] --> G[3+ Y-axes] D[Secondary Axis] --> H[Auto-convert] end

Now you’re ready to create amazing charts with multiple axes!

Remember: Multiple axes are like having multiple helpers in your picture—each one shows something special! 🎉

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.