0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Set Ticks in Matplotlib: Simple Guide with Examples

In matplotlib, you set ticks on axes using set_xticks() for x-axis and set_yticks() for y-axis. You provide a list of positions where you want the ticks to appear, and optionally customize their labels with set_xticklabels() or set_yticklabels().
๐Ÿ“

Syntax

To set ticks on the x-axis or y-axis, use these methods on an Axes object:

  • ax.set_xticks(ticks): sets positions of ticks on the x-axis.
  • ax.set_yticks(ticks): sets positions of ticks on the y-axis.
  • ax.set_xticklabels(labels): sets labels for the x-axis ticks.
  • ax.set_yticklabels(labels): sets labels for the y-axis ticks.

Here, ticks is a list of numbers where ticks appear, and labels is a list of strings for those ticks.

python
ax.set_xticks([0, 1, 2, 3])
ax.set_yticks([10, 20, 30])
ax.set_xticklabels(['zero', 'one', 'two', 'three'])
ax.set_yticklabels(['ten', 'twenty', 'thirty'])
๐Ÿ’ป

Example

This example shows how to set custom ticks and labels on both axes of a simple plot.

python
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1, 2, 3], [10, 20, 25, 30])

# Set custom ticks on x and y axes
ax.set_xticks([0, 1, 2, 3])
ax.set_yticks([10, 20, 25, 30])

# Set custom labels for those ticks
ax.set_xticklabels(['zero', 'one', 'two', 'three'])
ax.set_yticklabels(['ten', 'twenty', 'twenty-five', 'thirty'])

plt.show()
Output
A line plot with x-axis ticks labeled 'zero', 'one', 'two', 'three' and y-axis ticks labeled 'ten', 'twenty', 'twenty-five', 'thirty'.
โš ๏ธ

Common Pitfalls

  • Forgetting to use the Axes object (ax) and calling set_xticks() or set_yticks() on plt directly can cause no effect.
  • Setting ticks outside the data range will show empty space or no ticks.
  • When setting labels, the number of labels must match the number of ticks exactly.
  • Using plt.xticks() or plt.yticks() is a shortcut but works on the current axes only.
python
import matplotlib.pyplot as plt

plt.plot([0, 1, 2], [10, 20, 30])

# Wrong: setting ticks on plt without current axes reference
plt.xticks([0, 1, 2])  # Correct usage on current axes

# Right way:
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [10, 20, 30])
ax.set_xticks([0, 1, 2])
plt.show()
๐Ÿ“Š

Quick Reference

Summary of key methods to set ticks in Matplotlib:

MethodPurposeExample
ax.set_xticks(ticks)Set positions of x-axis ticksax.set_xticks([0,1,2])
ax.set_yticks(ticks)Set positions of y-axis ticksax.set_yticks([10,20,30])
ax.set_xticklabels(labels)Set labels for x-axis ticksax.set_xticklabels(['a','b','c'])
ax.set_yticklabels(labels)Set labels for y-axis ticksax.set_yticklabels(['x','y','z'])
plt.xticks(ticks, labels)Shortcut to set x ticks and labels on current axesplt.xticks([0,1], ['zero','one'])
โœ…

Key Takeaways

Use ax.set_xticks() and ax.set_yticks() to set tick positions on axes.
Match the number of labels exactly to the number of ticks when setting labels.
Always call tick-setting methods on the Axes object, not on plt directly.
plt.xticks() and plt.yticks() are shortcuts for current axes but less flexible.
Ticks outside the data range may not display as expected.