How to display the value on horizontal bars
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
Displaying numeric values directly on horizontal bars makes a chart easier to read because the user does not need to estimate values from the axis alone. The implementation is usually simple, but good label placement matters so the numbers stay readable even when bars are short or tightly packed.
Example with Matplotlib
In Matplotlib, create the horizontal bars with barh and then add labels for each bar. Modern Matplotlib makes this easier with bar_label.
bar_label automatically places text near the end of each bar, which is the simplest option for many charts.
Manual Placement with ax.text
If you need more control, place labels manually.
This approach is useful when labels need custom alignment, formatting, or conditional positioning.
Decide Whether Labels Go Inside or Outside
There is no universal best position. A good rule is:
- place labels outside the bar when bars are short or the text would not fit
- place labels inside the bar when bars are long and contrast is good
For inside labels, set the text color so it remains readable.
This works only if the bars are wide enough to hold the text comfortably.
Format the Labels Intentionally
The label text should match the meaning of the chart. Use integers, percentages, or currency formatting as appropriate instead of printing raw values blindly.
Formatting matters because the label is part of the data communication, not just decoration.
Handling Negative Values
Horizontal bars can also be negative. In that case, label placement needs to account for which side of zero the bar extends toward. Manual positioning with ax.text is often clearer than relying on default placement when the sign matters.
The general rule is simple: position the label just beyond the visual end of the bar, whether that end lies to the right or the left.
Dense Charts Need Restraint
If a chart has many categories, labeling every bar can create clutter instead of clarity. In those cases, it can be better to label only the most important bars, shorten the number format, or enlarge the figure so the labels have room to breathe. The goal is better comprehension, not maximum text density.
Common Pitfalls
- Placing labels where they overlap the bar edge or the next category line.
- Using one placement rule for all bars even when some bars are too short for inside labels.
- Forgetting to format values according to their business meaning, such as currency or percentages.
- Choosing a text color with poor contrast against the bar color.
- Assuming the chart is readable on screen without checking how the labels behave in export or on smaller figures.
Summary
- Value labels on horizontal bars improve chart readability immediately.
- In Matplotlib,
bar_labelis the quickest solution for standard cases. - Use
ax.textwhen you need custom positioning or formatting. - Decide deliberately whether labels should appear inside or outside the bars.
- Treat label formatting and contrast as part of the chart design, not as an afterthought.
Related reading
- How to do a scatter plot with empty circles in Python?
- How to do exponential and logarithmic curve fitting in Python? I found only polynomial fitting
- How to do multi class classification using Support Vector Machines SVM
- How to do recursive feature elimination with SVM in R
- How to divide a set into two sets such that the difference of the average is minimum?
- How to divide an array into 3 parts with the sum of each part roughly equal
- How to download graphs from tensorboard?
- How to draw vertical lines on a given plot

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the 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.