How to add third level of ticks in Python Matplotlib?

Adding a third level of ticks in Matplotlib allows you to create more granular visual references on your plots. This is achieved by creating twin axes and using different tick locators with varying tick lengths.

Understanding the Approach

The key concept involves using twiny() to create a twin axis sharing the y-axis, then configuring different tick levels with FixedLocator and tick_params().

Complete Example

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker

# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create sample data
t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01)

# Create figure and plot
fig, ax = plt.subplots()
plt.plot(t, s)

# Create twin axis for third level ticks
ax1 = ax.twiny()
ax1.plot(t, s)
ax1.xaxis.set_ticks_position('bottom')

# Define three levels of tick positions
majors = np.linspace(0, 100, 6)   # Major ticks (every 20 units)
minors = np.linspace(0, 100, 11)  # Minor ticks (every 10 units)
thirds = np.linspace(0, 100, 101) # Third level ticks (every 1 unit)

# Set tick locators
ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(majors))
ax.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(minors))
ax1.xaxis.set_major_locator(matplotlib.ticker.FixedLocator([]))
ax1.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(thirds))

# Configure tick lengths for visual hierarchy
ax1.tick_params(which='minor', length=2)  # Third level (shortest)
ax.tick_params(which='minor', length=4)   # Minor ticks (medium)
ax.tick_params(which='major', length=6)   # Major ticks (longest)

# Add grid and reference line
ax.grid(which='both', axis='x', linestyle='--')
plt.axhline(color='gray')

plt.show()

How It Works

The technique uses these key components:

  • Twin Axis: twiny() creates a second x-axis sharing the same y-axis
  • Tick Locators: FixedLocator places ticks at specific positions
  • Tick Hierarchy: Different length parameters create visual distinction
  • Position Control: set_ticks_position('bottom') places twin axis ticks at the bottom

Key Parameters

Parameter Purpose Values
majors Primary tick positions 6 ticks (0, 20, 40, 60, 80, 100)
minors Secondary tick positions 11 ticks (every 10 units)
thirds Tertiary tick positions 101 ticks (every 1 unit)

Customization Options

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker

# Create data
x = np.linspace(0, 10, 100)
y = np.sin(x)

fig, ax = plt.subplots(figsize=(10, 4))
ax.plot(x, y)

# Create twin axis
ax2 = ax.twiny()
ax2.xaxis.set_ticks_position('bottom')

# Custom tick levels with different densities
major_ticks = np.arange(0, 11, 2)      # Every 2 units
minor_ticks = np.arange(0, 11, 1)      # Every 1 unit  
micro_ticks = np.arange(0, 11, 0.5)    # Every 0.5 units

# Apply tick locators
ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(major_ticks))
ax.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(minor_ticks))
ax2.xaxis.set_major_locator(matplotlib.ticker.FixedLocator([]))
ax2.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(micro_ticks))

# Style the ticks with different colors and lengths
ax2.tick_params(which='minor', length=3, color='red', width=0.5)
ax.tick_params(which='minor', length=5, color='blue', width=1)
ax.tick_params(which='major', length=8, color='black', width=2)

plt.show()

Conclusion

Creating three levels of ticks requires twin axes and careful configuration of tick locators. Use different tick lengths to establish visual hierarchy, with major ticks being longest and third-level ticks being shortest for optimal readability.

Updated on: 2026-03-25T23:45:12+05:30

383 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements