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")