Adding value labels on a Matplotlib Bar Chart (original) (raw)

Last Updated : 22 Mar, 2025

A **Bar Chart is a graphical representation of data using bars of different heights. It is widely used for comparing different datasets visually. However, by default, Matplotlib does not display value labels on each bar, making it difficult to analyze the exact values represented by individual bars. In this article, we will explore how to add value labels on aMatplotlib bar chart to improve readability and make data interpretation easier.

Methods to add value labels on a bar chart

**Matplotlib provides multiple methods for adding value labels to a bar chart. We will primarily use the following two:

Creating a Bar Chart

**plt.bar() function is used to plot a bar chart.

**Syntax:

plt.bar(x, height, color)

**Parameters:

Adding value labels

To add text labels on top of the bars, we use the plt.text() function.

**Syntax:

plt.text(x, y, s, ha, bbox)

**Parameters:

**Steps to add value labels

Step 1. Import the necessary libraries

To begin, import the required Matplotlib library:

import matplotlib.pyplot as plt

Step 2. Define a function to add value labels

To place numerical values above each bar, a helper function is created. This function:

Step 3. Create the Main Function

In this step:

Example 1: Adding value labels on top of bars(default setting).

Python `

import matplotlib.pyplot as plt

Function to add value labels on top of bars

def add_labels(x, y): for i in range(len(x)): plt.text(i, y[i], y[i]) # Placing text slightly above the bar

Data for the bar chart

x = ["Engineering", "BSc", "MBA", "Bcom", "BBA", "MSc"] y = [9330, 4050, 3030, 5500, 8040, 4560]

Creating bar chart

plt.bar(x, y)

Adding value labels

add_labels(x, y)

Adding title and labels

plt.title("College Admission") plt.xlabel("Courses") plt.ylabel("Number of Admissions")

Display the chart

plt.show()

`

**Output:

Output13

**Explanation: add_labels(x, y) function adds numerical labels above each bar by iterating through the data and using **plt.text(i, y[i], y[i]) to position values at their respective heights. By default, labels are center-aligned for clarity.

Example 2: Centering Value Labels on Top of Bars

Python `

import matplotlib.pyplot as plt

Function to add centered value labels

def add_labels(x, y): for i in range(len(x)): plt.text(i, y[i], y[i], ha='center') # Aligning text at center

x = ["Engineering", "BSc", "MBA", "Bcom", "BBA", "MSc"] y = [9330, 4050, 3030, 5500, 8040, 4560]

Setting figure size

plt.figure(figsize=(10, 5))

Creating bar chart

plt.bar(x, y)

Adding value labels

add_labels(x, y)

Adding title and labels

plt.title("College Admission") plt.xlabel("Courses") plt.ylabel("Number of Admissions")

plt.show()

`

**Output:

Output14

**Explanation: add_labels(x, y) centers numerical labels above bars using ha=’center’ for readability. **plt.figure(figsize=(10, 5)) sets chart size and **plt.bar(x, y) plots the bars. Labels are positioned at their heights via plt.text(i, y[i], y[i]), ensuring clarity.

Example 3: Displaying Value Labels Inside a Rectangular Box

Python `

import matplotlib.pyplot as plt

Function to add labels inside a box

def add_labels(x, y): for i in range(len(x)): plt.text(i, y[i], y[i], ha='center', bbox=dict(facecolor='red', alpha=0.8)) # Adding a red box around text

x = ["Engineering", "BSc", "MBA", "Bcom", "BBA", "MSc"] y = [9330, 4050, 3030, 5500, 8040, 4560]

Setting figure size

plt.figure(figsize=(10, 5))

Creating bar chart

plt.bar(x, y)

Adding value labels

add_labels(x, y)

Adding title and labels

plt.title("College Admission") plt.xlabel("Courses") plt.ylabel("Number of Admissions")

plt.show()

`

**Output:

Output15

**Explanation: add_labels(x, y) adds numerical labels in **red-bordered boxes for visibility. **plt.bar(x, y) creates the chart and **plt.figure(figsize=(10, 5)) adjusts its size. Labels are centered with ha=’center’.

Example 4: Placing Labels at the Center of Each Bar

Python `

import matplotlib.pyplot as plt

Function to place labels at the center of each bar

def add_labels(x, y): for i in range(len(x)): plt.text(i, y[i] // 2, y[i], ha='center') # Placing text at half the bar height

x = ["Engineering", "BSc", "MBA", "Bcom", "BBA", "MSc"] y = [9330, 4050, 3030, 5500, 8040, 4560]

Setting figure size

plt.figure(figsize=(10, 5))

Creating bar chart

plt.bar(x, y)

Adding value labels

add_labels(x, y)

Adding title and labels

plt.title("College Admission") plt.xlabel("Courses") plt.ylabel("Number of Admissions")

plt.show()

`

**Output:

Output16

**Explanation: add_labels(x, y) positions numerical labels at half the bar height using **y[i] // 2 for better alignment. plt.bar(x, y) generates the chart and plt.figure(figsize=(10, 5)) adjusts its size. Labels are centered with **ha=’center’.

Example 5: Centered Labels Inside a Rectangular Box

Python `

import matplotlib.pyplot as plt

Function to add centered labels inside a box

def add_labels(x, y): for i in range(len(x)): plt.text(i, y[i] // 2, y[i], ha='center', bbox=dict(facecolor='white', alpha=0.5)) # White box with slight transparency

x = ["Engineering", "BSc", "MBA", "Bcom", "BBA", "MSc"] y = [9330, 4050, 3030, 5500, 8040, 4560]

Setting figure size

plt.figure(figsize=(10, 5))

Creating bar chart with red bars

plt.bar(x, y, color='blue')

Adding value labels

add_labels(x, y)

Adding title and labels

plt.title("College Admission") plt.xlabel("Courses") plt.ylabel("Number of Admissions")

plt.show()

`

**Output:

Output17

**Explanation: add_labels(x, y) centers labels at half bar height in a semi-transparent white box. **plt.bar(x, y, color=’blue’) creates blue bars and **plt.figure(figsize=(10, 5)) adjusts the chart size.