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 can I display text over columns in a bar chart in Matplotlib?
To display text over columns in a bar chart, we can use the text() method to place text at specific coordinates above each bar. This is useful for showing values, percentages, or labels directly on the chart.
Basic Example
Here's how to add text labels above bar chart columns ?
import matplotlib.pyplot as plt
# Data for the chart
categories = ['A', 'B', 'C', 'D', 'E']
values = [1, 3, 2, 0, 4]
percentages = [10, 30, 20, 0, 40]
# Create bar chart
bars = plt.bar(categories, values)
# Add text above each bar
for i, (cat, val, pct) in enumerate(zip(categories, values, percentages)):
plt.text(i, val + 0.1, f'{pct}%', ha='center', va='bottom')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Chart with Text Labels')
plt.show()
Customizing Text Position and Style
You can customize the appearance and position of the text labels ?
import matplotlib.pyplot as plt
categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [250, 380, 420, 310, 460]
bars = plt.bar(categories, sales, color='skyblue', alpha=0.7)
# Add styled text labels
for i, (cat, sale) in enumerate(zip(categories, sales)):
plt.text(i, sale + 10, f'${sale}',
ha='center', va='bottom',
fontweight='bold', fontsize=10)
plt.xlabel('Month')
plt.ylabel('Sales ($)')
plt.title('Monthly Sales with Value Labels')
plt.ylim(0, max(sales) + 50) # Add space for text
plt.show()
Text Alignment Options
The ha (horizontal alignment) and va (vertical alignment) parameters control text positioning ?
| Parameter | Options | Description |
|---|---|---|
ha |
'left', 'center', 'right' | Horizontal text alignment |
va |
'bottom', 'center', 'top' | Vertical text alignment |
Adding Text Inside Bars
You can also place text inside the bars by adjusting the y-coordinate ?
import matplotlib.pyplot as plt
products = ['Product A', 'Product B', 'Product C', 'Product D']
revenue = [1200, 800, 1500, 600]
bars = plt.bar(products, revenue, color=['red', 'green', 'blue', 'orange'])
# Add text inside bars (at half height)
for i, (product, rev) in enumerate(zip(products, revenue)):
plt.text(i, rev/2, f'${rev}',
ha='center', va='center',
color='white', fontweight='bold')
plt.xlabel('Products')
plt.ylabel('Revenue ($)')
plt.title('Product Revenue with Internal Labels')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Key Points
- Use
plt.text(x, y, text)to add labels at specific coordinates - Set
ha='center'andva='bottom'for labels above bars - Add padding to y-coordinates (like
val + 0.1) to avoid overlap - Use
plt.ylim()to ensure text labels are visible
Conclusion
The text() method provides flexible control over label placement in bar charts. Use proper alignment and positioning to create clear, readable charts with informative text labels.
Advertisements
