Changing the color of a single X-axis tick label in Matplotlib

To change the color of a single X-axis tick label in matplotlib, you can use the tick_params() method to change all tick labels, or target specific labels using get_xticklabels() for individual customization.

Method 1: Changing All X-axis Tick Labels

Use tick_params() to change the color of all X-axis tick labels at once ?

import numpy as np
import matplotlib.pyplot as plt

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

# Create figure and subplot
fig = plt.figure()
ax = fig.add_subplot(111)

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

# Plot the data
plt.plot(x, y)

# Change color of all X-axis tick labels
ax.tick_params(axis='x', colors='red')

plt.show()

Method 2: Changing a Single Tick Label

To change the color of only one specific tick label, access the tick labels and modify individual ones ?

import numpy as np
import matplotlib.pyplot as plt

# Create figure
fig, ax = plt.subplots(figsize=(8, 4))

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

# Plot the data
ax.plot(x, y)

# Get all tick labels
tick_labels = ax.get_xticklabels()

# Change color of the 3rd tick label (index 2) to red
if len(tick_labels) > 2:
    tick_labels[2].set_color('red')
    tick_labels[2].set_fontweight('bold')

plt.show()

Method 3: Targeting Specific Values

You can also target tick labels by their actual values rather than index position ?

import numpy as np
import matplotlib.pyplot as plt

# Create figure
fig, ax = plt.subplots(figsize=(8, 4))

# Create data
categories = ['A', 'B', 'C', 'D', 'E']
values = [23, 45, 56, 78, 32]

# Create bar plot
ax.bar(categories, values)

# Change color of specific tick label
for label in ax.get_xticklabels():
    if label.get_text() == 'C':
        label.set_color('red')
        label.set_fontsize(12)
        label.set_fontweight('bold')

plt.show()

Comparison

Method Use Case Complexity
tick_params() All tick labels Simple
get_xticklabels()[index] Single label by position Medium
Loop through labels Labels by value/condition Advanced

Conclusion

Use tick_params() for changing all X-axis tick label colors. For individual labels, use get_xticklabels() with indexing or loop through labels to target specific values.

Updated on: 2026-03-25T23:55:38+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements