How to adjust 'tick frequency' in Matplotlib for string X-axis?

To adjust tick frequency for string X-axis in Matplotlib, you need to control which string labels appear on the axis. This is useful when you have many categorical labels that would overlap or clutter the display.

Basic Tick Frequency Adjustment

Here's how to adjust tick frequency when working with string labels on the X-axis ?

import matplotlib.pyplot as plt
import numpy as np

# Sample string labels for X-axis
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
          'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
sales = [120, 135, 148, 162, 158, 175, 180, 165, 142, 138, 155, 170]

plt.figure(figsize=(10, 6))
plt.plot(months, sales, marker='o', color='blue')

# Show every 3rd tick (every 3 months)
freq_x = 3
plt.xticks(range(0, len(months), freq_x), 
           [months[i] for i in range(0, len(months), freq_x)])

plt.title('Sales Data with Adjusted Tick Frequency')
plt.ylabel('Sales')
plt.show()
# Displays a line plot showing only Jan, Apr, Jul, Oct on X-axis

Using numpy.arange() for Tick Control

You can also use numpy.arange() to select specific tick positions ?

import matplotlib.pyplot as plt
import numpy as np

# Days of the week
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
temperatures = [22, 25, 28, 26, 24, 30, 32]

plt.figure(figsize=(8, 5))
plt.bar(days, temperatures, color='orange')

# Show every 2nd day
freq_x = 2
tick_positions = np.arange(0, len(days), freq_x)
tick_labels = [days[i] for i in tick_positions]

plt.xticks(tick_positions, tick_labels)
plt.title('Weekly Temperature with Every 2nd Day Shown')
plt.ylabel('Temperature (°C)')
plt.show()
# Displays a bar chart showing only Mon, Wed, Fri, Sun on X-axis

Advanced Tick Control with Rotation

For longer string labels, combine frequency adjustment with rotation for better readability ?

import matplotlib.pyplot as plt

# Product names (longer strings)
products = ['Laptop Computer', 'Desktop Monitor', 'Wireless Mouse', 
           'Mechanical Keyboard', 'USB Webcam', 'Bluetooth Speaker', 
           'External Hard Drive', 'Graphics Tablet']
quantities = [45, 32, 78, 56, 23, 67, 34, 19]

plt.figure(figsize=(12, 6))
plt.bar(products, quantities, color='green')

# Show every 2nd product with rotation
freq_x = 2
selected_indices = list(range(0, len(products), freq_x))
selected_products = [products[i] for i in selected_indices]

plt.xticks(selected_indices, selected_products, rotation=45, ha='right')
plt.title('Product Sales with Adjusted Tick Frequency')
plt.ylabel('Quantity Sold')
plt.tight_layout()
plt.show()
# Displays a bar chart with rotated labels showing every 2nd product

Comparison of Approaches

Method Best For Advantage
range(0, len(labels), freq) Simple regular intervals Easy to understand and implement
np.arange(0, len(labels), freq) Numerical control More flexible with numpy arrays
Manual index selection Irregular intervals Complete control over which labels show

Conclusion

Use plt.xticks() with index slicing to control which string labels appear on your X-axis. Combine with rotation for better readability when dealing with longer text labels.

Updated on: 2026-03-25T23:57:01+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements