Use the tick locator/labeler methods. There are two common approaches:
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.
set_xticks
set_xticklabels
labels = [l.get_text() for l in ax.get_xticklabels()] labels[1] = 'replaced' ax.set_xticklabels(labels)
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.
matplotlib.dates.DateFormatter
FuncFormatter
ax.set_xticks(...)
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.
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