How to display percentage above a bar chart in Matplotlib?

To display percentage above a bar chart in Matplotlib, you can iterate through the bar patches and use the text() method to add percentage labels. This is commonly used to show data proportions in visualizations.

Basic Example

Here's how to add percentage labels above each bar ?

import matplotlib.pyplot as plt
import numpy as np

# Set figure size
plt.figure(figsize=(8, 5))

# Data
categories = ['A', 'B', 'C', 'D', 'E']
values = [25, 40, 15, 35, 20]

# Create bar chart
bars = plt.bar(categories, values, color='skyblue')

# Add percentage labels above bars
for bar in bars:
    height = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2, height + 1,
             f'{height}%',
             ha='center', va='bottom')

plt.ylabel('Percentage')
plt.title('Bar Chart with Percentage Labels')
plt.show()

Advanced Example with Actual Percentages

When your data represents counts, you can calculate and display actual percentages ?

import matplotlib.pyplot as plt
import numpy as np

# Sample data (counts)
products = ['Product A', 'Product B', 'Product C', 'Product D']
sales = [120, 85, 95, 140]

# Calculate percentages
total_sales = sum(sales)
percentages = [(sale/total_sales) * 100 for sale in sales]

# Create bar chart
plt.figure(figsize=(10, 6))
bars = plt.bar(products, sales, color=['#ff9999', '#66b3ff', '#99ff99', '#ffcc99'])

# Add percentage labels
for i, bar in enumerate(bars):
    height = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2, height + 2,
             f'{percentages[i]:.1f}%',
             ha='center', va='bottom', fontweight='bold')

plt.ylabel('Sales Count')
plt.title('Product Sales with Percentage Distribution')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Customizing Label Appearance

You can customize the font size, color, and positioning of percentage labels ?

import matplotlib.pyplot as plt

# Data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
growth = [12.5, 18.7, 9.3, 25.1, 15.8]

# Create horizontal bar chart
plt.figure(figsize=(8, 6))
bars = plt.barh(months, growth, color='lightcoral')

# Add customized percentage labels
for bar in bars:
    width = bar.get_width()
    plt.text(width + 0.5, bar.get_y() + bar.get_height()/2,
             f'{width}%',
             ha='left', va='center', 
             fontsize=12, fontweight='bold', color='darkred')

plt.xlabel('Growth Percentage')
plt.title('Monthly Growth Rate')
plt.xlim(0, max(growth) + 5)
plt.show()

Key Parameters

Parameter Description Example Values
ha Horizontal alignment 'center', 'left', 'right'
va Vertical alignment 'bottom', 'center', 'top'
fontsize Text size 10, 12, 'large', 'small'
fontweight Text weight 'normal', 'bold'

Conclusion

Use plt.text() with bar coordinates to add percentage labels above bars. Calculate actual percentages from raw data when needed, and customize label appearance using font parameters for better visualization.

Updated on: 2026-03-25T21:41:00+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements