How do I convert (or scale) axis values and redefine the tick frequency in Matplotlib?

In Matplotlib, you can convert or scale axis values and redefine tick frequency using the xticks() and yticks() methods. This allows you to customize how your axis labels appear and control the spacing between tick marks.

Basic Axis Scaling and Tick Customization

Here's how to create custom axis scales and redefine tick frequency ?

import numpy as np
import matplotlib.pyplot as plt

# Set figure size
plt.figure(figsize=(10, 6))

# Create data
n = 10
x = np.linspace(-2, 2, n)
y = np.exp(x)

# Plot the data
plt.plot(x, y, marker='o')

# Create custom tick positions and labels
xticks = [i for i in range(int(n/2))]
xtick_labels = ["x"+str(i) for i in range(int(n/2))]

# Apply custom ticks
plt.xticks(xticks, xtick_labels)

plt.title('Custom X-axis Ticks and Labels')
plt.grid(True, alpha=0.3)
plt.show()

Scaling Axis Values

You can scale axis values by multiplying or transforming the tick positions ?

import numpy as np
import matplotlib.pyplot as plt

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

plt.figure(figsize=(10, 6))
plt.plot(x, y)

# Scale x-axis: convert radians to degrees
x_ticks = np.arange(0, 11, 2)  # Original positions
x_labels = [f'{int(tick * 180/np.pi)}°' for tick in x_ticks]  # Convert to degrees

plt.xticks(x_ticks, x_labels)
plt.title('X-axis Scaled from Radians to Degrees')
plt.ylabel('sin(x)')
plt.grid(True, alpha=0.3)
plt.show()

Controlling Tick Frequency

Use numpy.arange() or range() to control tick spacing ?

import numpy as np
import matplotlib.pyplot as plt

# Create data
x = np.linspace(0, 100, 1000)
y = x**2

plt.figure(figsize=(10, 6))
plt.plot(x, y)

# Set custom tick frequency
# X-axis: every 20 units
plt.xticks(np.arange(0, 101, 20))

# Y-axis: every 2000 units
plt.yticks(np.arange(0, 10001, 2000))

plt.title('Custom Tick Frequency')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.grid(True, alpha=0.3)
plt.show()

Advanced Tick Formatting

You can format ticks with units, scientific notation, or custom formats ?

import numpy as np
import matplotlib.pyplot as plt

# Create data representing time series
days = np.arange(0, 365, 30)
temperature = 20 + 15 * np.sin(2 * np.pi * days / 365)

plt.figure(figsize=(12, 6))
plt.plot(days, temperature, marker='o')

# Custom x-axis with month names
month_names = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
               'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
plt.xticks(days, month_names[:len(days)])

# Custom y-axis with temperature units
y_ticks = np.arange(0, 41, 10)
y_labels = [f'{temp}°C' for temp in y_ticks]
plt.yticks(y_ticks, y_labels)

plt.title('Temperature Throughout the Year')
plt.xlabel('Month')
plt.ylabel('Temperature')
plt.grid(True, alpha=0.3)
plt.show()

Using Tick Locators for Automatic Scaling

For more advanced control, use matplotlib's tick locators ?

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

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

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

# Left plot: Default ticks
ax1.plot(x, y)
ax1.set_title('Default Tick Frequency')
ax1.grid(True, alpha=0.3)

# Right plot: Custom tick frequency using locators
ax2.plot(x, y)
ax2.xaxis.set_major_locator(MultipleLocator(2))  # Major ticks every 2 units
ax2.xaxis.set_minor_locator(MultipleLocator(0.5))  # Minor ticks every 0.5 units
ax2.yaxis.set_major_locator(MultipleLocator(0.5))
ax2.set_title('Custom Tick Frequency with Locators')
ax2.grid(True, alpha=0.3)
ax2.grid(True, which='minor', alpha=0.1)

plt.tight_layout()
plt.show()

Key Methods Summary

Method Purpose Example
plt.xticks() Set x-axis ticks and labels plt.xticks([0, 1, 2], ['A', 'B', 'C'])
plt.yticks() Set y-axis ticks and labels plt.yticks(np.arange(0, 11, 2))
MultipleLocator Automatic tick spacing ax.xaxis.set_major_locator(MultipleLocator(5))

Conclusion

Use xticks() and yticks() for basic tick customization and scaling. For advanced control over tick frequency and formatting, combine these methods with numpy arrays and matplotlib's tick locators to create professional-looking plots.

Updated on: 2026-03-25T21:19:25+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements