Tick Control¶
This document covers the XAxis/YAxis objects, tick locators, and tick formatters for fine-grained control over axis behavior.
XAxis and YAxis Objects¶
Access axis objects via the axes:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2*np.pi, 2*np.pi, 100)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
# Access axis objects
xaxis = ax.xaxis
yaxis = ax.yaxis
print(type(xaxis)) # <class 'matplotlib.axis.XAxis'>
print(type(yaxis)) # <class 'matplotlib.axis.YAxis'>
set_ticks_position¶
Control where ticks appear:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 500)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(12, 3))
ax.plot(x, y)
ax.xaxis.set_ticks_position('top')
ax.yaxis.set_ticks_position('right')
plt.show()
Options: 'top', 'bottom', 'left', 'right', 'both', 'none', 'default'
Axis Visibility¶
Hide an entire axis:
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
get_ticklocs and get_ticklabels¶
Get current tick positions and labels:
print(ax.xaxis.get_ticklocs())
print(ax.yaxis.get_ticklocs())
print(ax.xaxis.get_ticklabels()) # List of Text objects
Tick Locators¶
Tick locators determine where tick marks appear on an axis.
Using Locators¶
import matplotlib as mpl
ax.xaxis.set_major_locator(locator)
ax.yaxis.set_major_locator(locator)
ax.xaxis.set_minor_locator(locator)
ax.yaxis.set_minor_locator(locator)
MultipleLocator¶
Place ticks at multiples of a base value:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.linspace(-2*np.pi, 2*np.pi, 500)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(12, 3))
ax.plot(x, y)
ax.xaxis.set_major_locator(mpl.ticker.MultipleLocator(5))
ax.yaxis.set_major_locator(mpl.ticker.MultipleLocator(0.5))
ax.xaxis.set_minor_locator(mpl.ticker.MultipleLocator(1))
ax.yaxis.set_minor_locator(mpl.ticker.MultipleLocator(0.1))
plt.show()
FixedLocator¶
Place ticks at specific locations:
ax.xaxis.set_major_locator(mpl.ticker.FixedLocator([-1, 0, 1]))
ax.yaxis.set_major_locator(mpl.ticker.FixedLocator([-0.2, 0, 0.2]))
MaxNLocator¶
Automatically choose up to N tick locations:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
fig, axes = plt.subplots(2, 3, sharex=True, sharey=True, figsize=(12, 3))
for ax in axes.flatten():
ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(3))
ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(3))
ax.plot(np.random.randn(10))
plt.show()
NullLocator¶
Remove all ticks:
ax.xaxis.set_major_locator(mpl.ticker.NullLocator())
ax.yaxis.set_major_locator(mpl.ticker.NullLocator())
Useful for image displays:
import matplotlib.pyplot as plt
import matplotlib as mpl
from sklearn.datasets import fetch_olivetti_faces
faces = fetch_olivetti_faces().images
fig, ax = plt.subplots(5, 5, figsize=(5, 5))
fig.subplots_adjust(hspace=0, wspace=0)
for i in range(5):
for j in range(5):
ax[i, j].xaxis.set_major_locator(mpl.ticker.NullLocator())
ax[i, j].yaxis.set_major_locator(mpl.ticker.NullLocator())
ax[i, j].imshow(faces[10 * i + j], cmap="bone")
plt.show()
Locators Summary¶
| Locator | Description |
|---|---|
MultipleLocator(base) |
Ticks at multiples of base |
FixedLocator(locs) |
Ticks at specified locations |
MaxNLocator(n) |
At most n ticks |
NullLocator() |
No ticks |
AutoLocator() |
Automatic (default) |
LogLocator() |
For log scales |
LinearLocator(n) |
Exactly n evenly spaced |
IndexLocator(base, offset) |
Ticks at base*i + offset |
Tick Formatters¶
Tick formatters control how tick labels are displayed.
Using Formatters¶
import matplotlib as mpl
ax.xaxis.set_major_formatter(formatter)
ax.yaxis.set_major_formatter(formatter)
ax.xaxis.set_minor_formatter(formatter)
ax.yaxis.set_minor_formatter(formatter)
NullFormatter¶
Remove tick labels while keeping ticks:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.linspace(-2*np.pi, 2*np.pi, 500)
y = np.sin(x) * np.exp(-x**2/20)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.xaxis.set_major_formatter(mpl.ticker.NullFormatter())
plt.show()
FuncFormatter¶
Custom formatting with a function:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
def format_func(value, tick_number):
# Convert to multiples of pi/2
N = int(np.round(2 * value / np.pi))
if N == 0:
return "0"
elif N == 1:
return "$\\pi/2$"
elif N == -1:
return "$-\\pi/2$"
elif N == 2:
return "$\\pi$"
elif N == -2:
return "$-\\pi$"
elif N % 2 > 0:
return f"${N}\\pi/2$"
else:
return f"${N // 2}\\pi$"
x = np.linspace(-2*np.pi, 2*np.pi, 500)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(12, 3))
ax.plot(x, y)
ax.xaxis.set_major_locator(mpl.ticker.MultipleLocator(np.pi / 2))
ax.xaxis.set_minor_locator(mpl.ticker.MultipleLocator(np.pi / 4))
ax.xaxis.set_major_formatter(mpl.ticker.FuncFormatter(format_func))
plt.show()
DateFormatter¶
Format datetime tick labels:
import matplotlib.pyplot as plt
import matplotlib.dates as mpl_dates
import yfinance as yf
df = yf.Ticker('AAPL').history(start='2020-07-01', end='2020-12-31')
fig, ax = plt.subplots(figsize=(15, 3))
ax.plot(df.index, df['Close'])
# Date formatting
date_format = mpl_dates.DateFormatter('%b, %d %Y')
ax.xaxis.set_major_formatter(date_format)
fig.autofmt_xdate()
plt.show()
Date Format Codes¶
| Code | Meaning | Example |
|---|---|---|
%Y |
Year (4 digit) | 2024 |
%y |
Year (2 digit) | 24 |
%m |
Month (number) | 01-12 |
%b |
Month (abbrev) | Jan |
%B |
Month (full) | January |
%d |
Day | 01-31 |
%H |
Hour (24h) | 00-23 |
%M |
Minute | 00-59 |
%S |
Second | 00-59 |
StrMethodFormatter¶
Format using string format specification:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.linspace(0, 1, 100)
y = x ** 2 * 1000
fig, ax = plt.subplots()
ax.plot(x, y)
# Format with commas and 0 decimals
ax.yaxis.set_major_formatter(mpl.ticker.StrMethodFormatter('{x:,.0f}'))
plt.show()
PercentFormatter¶
Format as percentages:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.linspace(0, 1, 100)
y = x ** 2
fig, ax = plt.subplots()
ax.plot(x, y)
# xmax=1.0 means 1.0 = 100%
ax.yaxis.set_major_formatter(mpl.ticker.PercentFormatter(xmax=1.0))
plt.show()
ScalarFormatter¶
Default formatter with scientific notation control:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.linspace(0, 1, 100)
y = x * 1e6
fig, ax = plt.subplots()
ax.plot(x, y)
formatter = mpl.ticker.ScalarFormatter(useMathText=True)
formatter.set_scientific(True)
formatter.set_powerlimits((-2, 2))
ax.yaxis.set_major_formatter(formatter)
plt.show()
Formatters Summary¶
| Formatter | Description |
|---|---|
NullFormatter() |
No labels |
FuncFormatter(func) |
Custom function |
StrMethodFormatter(fmt) |
String format |
PercentFormatter(xmax) |
Percentage |
ScalarFormatter() |
Default with options |
LogFormatter() |
For log scales |
DateFormatter(fmt) |
Date/time |
Complete Example: Stock Chart¶
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.dates as mpl_dates
import numpy as np
import pandas as pd
# Create sample time series
dates = pd.date_range('2024-01-01', periods=100, freq='D')
values = np.cumsum(np.random.randn(100)) * 1000 + 50000
fig, ax = plt.subplots(figsize=(12, 4))
ax.plot(dates, values)
# Date formatter for x-axis
ax.xaxis.set_major_formatter(mpl_dates.DateFormatter('%b %Y'))
ax.xaxis.set_major_locator(mpl_dates.MonthLocator())
# Currency formatter for y-axis
ax.yaxis.set_major_formatter(mpl.ticker.StrMethodFormatter('${x:,.0f}'))
ax.set_title('Portfolio Value')
fig.autofmt_xdate()
plt.show()
Complete Example: Math Plot with Grid¶
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.linspace(-2*np.pi, 2*np.pi, 500)
y = np.sin(x) * np.exp(-x**2/20)
fig, ax = plt.subplots(figsize=(10, 4))
ax.plot(x, y)
# Configure x-axis
ax.xaxis.set_major_locator(mpl.ticker.MultipleLocator(np.pi))
ax.xaxis.set_minor_locator(mpl.ticker.MultipleLocator(np.pi/4))
ax.xaxis.set_ticks_position('bottom')
# Configure y-axis
ax.yaxis.set_major_locator(mpl.ticker.MultipleLocator(0.2))
ax.yaxis.set_minor_locator(mpl.ticker.MultipleLocator(0.05))
ax.yaxis.set_ticks_position('left')
# Add grid for both major and minor ticks
ax.grid(which='major', linestyle='-', linewidth=0.5)
ax.grid(which='minor', linestyle=':', linewidth=0.5, alpha=0.5)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Damped Sine Wave')
plt.show()
Key Takeaways¶
- Access axis objects via
ax.xaxisandax.yaxis - Locators control where ticks appear
- Formatters control how tick labels are displayed
set_ticks_position()controls tick placement- Apply locators/formatters to major and minor ticks separately
- Use
MultipleLocatorfor regular intervals - Use
FuncFormatterfor custom label formatting - Use
DateFormatterfor time series data