Data Visualization
Horizontal Bar Graphs
Display Values
Chart Design
Visual Data Representation

How to display the value on horizontal bars

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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.

python
1import matplotlib.pyplot as plt
2
3categories = ["A", "B", "C"]
4values = [12, 28, 7]
5
6fig, ax = plt.subplots()
7bars = ax.barh(categories, values, color="#4C78A8")
8ax.bar_label(bars, fmt="%.0f", padding=3)
9
10ax.set_xlabel("Value")
11ax.set_title("Scores by Category")
12plt.show()

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.

python
1import matplotlib.pyplot as plt
2
3categories = ["A", "B", "C"]
4values = [12, 28, 7]
5
6fig, ax = plt.subplots()
7ax.barh(categories, values, color="#72B7B2")
8
9for i, value in enumerate(values):
10    ax.text(value + 0.5, i, str(value), va="center")
11
12ax.set_xlim(0, 35)
13plt.show()

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.

python
for i, value in enumerate(values):
    ax.text(value - 1.0, i, str(value), va="center", ha="right", color="white")

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.

python
ax.bar_label(bars, fmt="$%.2f", padding=3)

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_label is the quickest solution for standard cases.
  • Use ax.text when 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.

Course illustration
Course illustration

All Rights Reserved.