How to format tick labels in Matplotlib

Tick labels turn axis positions into text a chart reader can scan. In Matplotlib, tick locations and tick text belong to the x-axis or y-axis on an Axes object, so a plot can show quarter names, currency units, percentages, or other task-specific labels instead of raw numeric values.

Use fixed tick labels when each tick has a specific category or name, such as Q1 through Q4. Use a formatter when the data remains numeric but the visible label needs a unit, prefix, suffix, rounding rule, or other consistent string shape.

Keep tick positions and label strings paired. When labels are passed to set_xticks() or set_yticks(), Matplotlib expects the label list to have the same length as the tick location list; formatters avoid that mismatch by generating each label from the tick value.

Steps to format Matplotlib tick labels:

  1. Save the plotting script as format_tick_labels.py.
    format_tick_labels.py
    import matplotlib.pyplot as plt
    from matplotlib.ticker import FuncFormatter
     
     
    quarters = [1, 2, 3, 4]
    revenue = [12500, 18100, 23600, 31200]
     
    fig, ax = plt.subplots(layout="constrained")
    ax.plot(quarters, revenue, marker="o", linewidth=2)
     
    ax.set_xticks(quarters, labels=["Q1", "Q2", "Q3", "Q4"])
    ax.set_yticks([0, 10000, 20000, 30000, 40000])
    ax.yaxis.set_major_formatter(
        FuncFormatter(lambda value, position: f"${value / 1000:.0f}k")
    )
     
    ax.set_xlabel("Quarter")
    ax.set_ylabel("Revenue")
    ax.set_title("Quarterly revenue with formatted tick labels")
     
    fig.savefig("formatted-revenue-ticks.png", dpi=160)
    fig.canvas.draw()
     
    print("xlabels=" + ", ".join(tick.get_text() for tick in ax.get_xticklabels()))
    print("ylabels=" + ", ".join(tick.get_text() for tick in ax.get_yticklabels()))
    print("saved=formatted-revenue-ticks.png")

    The labels list in set_xticks() must match the number of tick locations. For simple numeric labels, ax.yaxis.set_major_formatter("${x:,.0f}") creates a StrMethodFormatter automatically; use FuncFormatter when label logic needs Python code.

  2. Run the script to save the figure and print the generated tick labels.
    $ python format_tick_labels.py
    xlabels=Q1, Q2, Q3, Q4
    ylabels=$0k, $10k, $20k, $30k, $40k
    saved=formatted-revenue-ticks.png
  3. Open the saved PNG and confirm the visible tick labels.

    The x-axis should show Q1 through Q4, and the y-axis should show dollar values in thousands. If longer labels collide or get clipped in a real figure, keep layout="constrained" or adjust the layout before export.
    Related: How to fix overlapping labels in Matplotlib