Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to display the count over the bar in Matplotlib histogram?
To display the count over the bar in matplotlib histogram, we can iterate each patch and use text() method to place the values over the patches.
Steps
- Set the figure size and adjust the padding between and around the subplots.
- Make a list of numbers to make a histogram plot.
- Use
hist()method to make histograms. - Iterate the patches and calculate the mid-values of each patch and height of the patch to place a text.
- To display the figure, use
show()method.
Example
Here's how to add count labels above each bar in a histogram ?
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
data = [3, 5, 1, 7, 9, 5, 3, 7, 5]
_, _, patches = plt.hist(data, align="mid")
for pp in patches:
x = (pp._x0 + pp._x1)/2
y = pp._y1 + 0.05
plt.text(x, y, int(pp._y1))
plt.show()
How It Works
The hist() method returns three values: counts, bin edges, and patches. We iterate through the patches to find each bar's position and height ?
import matplotlib.pyplot as plt
# Sample data
data = [1, 2, 2, 3, 3, 3, 4, 4, 5]
counts, bins, patches = plt.hist(data, bins=5, edgecolor='black')
# Add count labels on top of each bar
for i, patch in enumerate(patches):
# Get the x-coordinate (center of the bar)
x = patch.get_x() + patch.get_width() / 2
# Get the y-coordinate (height of the bar)
height = patch.get_height()
# Add text label
plt.text(x, height + 0.1, str(int(height)),
ha='center', va='bottom', fontweight='bold')
plt.title('Histogram with Count Labels')
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.show()
Alternative Method Using Annotations
You can also use annotate() for more control over the text positioning ?
import matplotlib.pyplot as plt
data = [2, 3, 3, 4, 4, 4, 5, 5, 6]
counts, bins, patches = plt.hist(data, bins=4, alpha=0.7, color='skyblue')
for i, (count, patch) in enumerate(zip(counts, patches)):
# Get bar center and height
x = patch.get_x() + patch.get_width() / 2
y = patch.get_height()
# Annotate with count
plt.annotate(str(int(count)),
xy=(x, y),
xytext=(0, 5), # 5 points offset
textcoords='offset points',
ha='center', va='bottom',
fontsize=12, fontweight='bold')
plt.title('Histogram with Annotated Counts')
plt.show()
Key Points
-
patch.get_x()andpatch.get_width()help find the bar's center position -
patch.get_height()gives the bar's height (count value) - Use
ha='center'andva='bottom'to center-align text above bars - Add small offset to y-coordinate to prevent text from touching the bar
Conclusion
Use plt.text() or plt.annotate() to display counts above histogram bars. Calculate the center position of each bar and place the text slightly above the bar height for clear visibility.
Advertisements
