Tick marks and tick labels help us read values on a graph easily. They show where numbers or categories are on the axes.
0
0
Tick marks and tick labels in Matplotlib
Introduction
When you want to show exact values on the x or y axis of a chart.
When you want to customize which numbers or labels appear on the axes.
When you want to make your graph easier to understand by adding or removing ticks.
When you want to change the style or format of the numbers on the axes.
When you want to label categories instead of numbers on a bar chart.
Syntax
Matplotlib
import matplotlib.pyplot as plt plt.xticks(ticks=None, labels=None, **kwargs) plt.yticks(ticks=None, labels=None, **kwargs)
ticks is a list of positions where tick marks appear.
labels is a list of text labels shown at those positions.
Examples
This sets custom labels A, B, C, D on the x-axis at positions 0, 1, 2, 3.
Matplotlib
import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4]) plt.xticks([0, 1, 2, 3], ['A', 'B', 'C', 'D']) plt.show()
This replaces y-axis numbers with descriptive labels.
Matplotlib
import matplotlib.pyplot as plt plt.plot([10, 20, 30, 40]) plt.yticks([10, 20, 30, 40], ['Low', 'Medium', 'High', 'Very High']) plt.show()
This sets tick marks at positions 0, 1, 2, 3 but uses default labels.
Matplotlib
import matplotlib.pyplot as plt plt.plot([1, 4, 9, 16]) plt.xticks(ticks=[0, 1, 2, 3]) # Only set tick positions plt.show()
Sample Program
This program plots a simple line chart and sets custom tick marks and labels on both x and y axes to make the graph easier to read.
Matplotlib
import matplotlib.pyplot as plt # Sample data x = [0, 1, 2, 3, 4] y = [10, 20, 25, 30, 40] plt.plot(x, y) # Set custom tick marks and labels on x-axis plt.xticks(ticks=[0, 1, 2, 3, 4], labels=['Zero', 'One', 'Two', 'Three', 'Four']) # Set custom tick marks and labels on y-axis plt.yticks(ticks=[10, 20, 30, 40], labels=['Ten', 'Twenty', 'Thirty', 'Forty']) plt.title('Custom Tick Marks and Labels') plt.xlabel('X Axis') plt.ylabel('Y Axis') plt.show()
OutputSuccess
Important Notes
If you set ticks but not labels, matplotlib uses the tick values as labels.
You can style tick labels with extra arguments like rotation to rotate text.
Use plt.xticks([]) or plt.yticks([]) to hide ticks.
Summary
Tick marks show positions on axes; tick labels show text for those positions.
You can customize ticks and labels to make graphs clearer and more meaningful.
Use plt.xticks() and plt.yticks() to control ticks and labels in matplotlib.