In Python matplotlib how to change a tick label?
Oct 13, 3:30pm

Use the tick locator/labeler methods. There are two common approaches:

1. Set labels at fixed positions

python
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 4])

ax.set_xticks([0, 1, 2])
ax.set_xticklabels(['start', 'mid', 'end'])

plt.show()

Note: you must call set_xticks before set_xticklabels, otherwise the labels get dropped in recent matplotlib versions.

2. Change a single existing tick label

python
labels = [l.get_text() for l in ax.get_xticklabels()]
labels[1] = 'replaced'
ax.set_xticklabels(labels)

3. Format all labels with a function (cleanest for numbers)

python
from matplotlib.ticker import FuncFormatter

ax.yaxis.set_major_formatter(FuncFormatter(lambda x, _: f'{x:.0%}'))

This avoids hardcoding labels and keeps working when limits change.

Tips

  • If labels look cut off or misaligned after changing them, the tick positions and label count must match.
  • For dates, use matplotlib.dates.DateFormatter instead of FuncFormatter.
  • If ticks disappear, call ax.set_xticks(...) again or set the locator via ax.xaxis.set_major_locator.

For your case, approach 1 is best if you know the positions; approach 3 if you just want to reformat values.

Oct 13, 3:30pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Vibe can make mistakes. Check answers. Learn more