reducing number of plot ticks
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Too many axis ticks make a chart look busy before the reader has even looked at the data. Reducing the number of ticks is usually not about cosmetics alone; it improves legibility, prevents overlapping labels, and makes the scale easier to scan.
Let the Locator Do the Work
In Matplotlib, tick placement is controlled by locators. For most linear axes, MaxNLocator is one of the most useful tools when you want fewer ticks without hard-coding every label.
nbins means "at most about this many intervals," not "guarantee exactly this many printed labels." That distinction matters because Matplotlib still tries to choose nice round values.
Use Fixed Spacing When the Scale Has a Natural Step
Sometimes the data has a natural increment such as every 10 units or every hour. In those cases, a fixed locator can be clearer than asking Matplotlib to guess.
This is a good fit when you want predictable tick spacing rather than adaptive spacing.
Manual Tick Selection
For presentation charts, manual ticks are sometimes the simplest answer:
This gives full control, but it is best only when the axis range is stable. If the data limits change a lot, manual ticks become brittle and may stop matching the plotted range well.
Reduce Labels Without Removing All Reference Points
You do not always need to remove every extra visual marker. Another option is to keep major ticks sparse and use minor ticks or grid lines for context.
This works well when you want a clean label set but still want the eye to follow the scale. The chart remains readable without becoming empty or overly simplified.
Match the Tick Strategy to the Chart
A small sparkline and a large analytical figure should not use the same tick density. Good practice depends on:
- figure size
- font size
- label length
- data range
- whether the chart is exploratory or presentation-ready
If the x-axis contains dates or long category names, reducing count matters even more because label overlap becomes the real problem.
Avoid Fighting Autoscaling Blindly
Matplotlib's default locators already try to avoid extreme clutter. If the output still looks crowded, the issue may be that:
- the figure is too small
- the labels are too long
- the axis range is too wide for the chosen layout
Reducing tick count helps, but rotating labels, enlarging the figure, or changing the formatter may be equally important.
Common Pitfalls
One common mistake is expecting MaxNLocator(nbins=5) to always print exactly five labels. It does not promise that. It chooses a reasonable set up to that limit based on the axis range.
Another issue is hard-coding ticks for data that changes between runs. Manual values look fine once, then become misleading later when the dataset grows or shrinks.
People also remove so many ticks that the scale becomes hard to interpret. The goal is fewer ticks, not no scale information.
Summary
- Reducing tick count improves readability and often prevents label collisions.
- '
MaxNLocatoris a strong default when you want fewer ticks but still want automatic placement.' - '
MultipleLocatorworks well when the axis has a natural fixed interval.' - Manual ticks are useful for stable presentation charts, but fragile for dynamic data.
- If a chart still looks crowded, consider figure size, label formatting, and layout as well as tick count.
Related reading
- regarding the correct way to understand the result of tf.pad
- Regarding the use of tf.train.shuffle_batch to create batches
- Regression Tests on Arbitrary Number Sequences
- Relational Fisher Kernel Implementation
- Relationship between SciPy and NumPy
- Remap values in pandas column with a dict, preserve NaNs
- Remove duplicates by columns A, keeping the row with the highest value in column B
- Remove pandas rows with duplicate indices
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.