Open In App

Adding Labels to Histogram Bars in Matplotlib

Last Updated : 16 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In Matplotlib, a histogram displays data distribution across defined intervals, known as bins. Each bar in the histogram represents the frequency or density of data points within these bins. To communicate these values, you can add labels to the bars by placing text directly on or above them, showing each bin’s exact frequency or density.

Let’s learn how to add labels to histogram bars in Matplotlib.

Adding Labels to Histogram Bars using bar_label() Function

The bar_label() function simplifies the process of adding labels by automatically placing them on all bars at once. This method is significant for its efficiency and ease of use with consistent formatting.

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)
counts, bins, patches = plt.hist(data, bins=30, color='skyblue', edgecolor='black')

plt.bar_label(patches, labels=[f'{int(c)}' for c in counts], label_type='center')

plt.title('Histogram with Centered Labels')
plt.xlabel('Value')
plt.ylabel('Frequency')

plt.show()

Output:

Screenshot-2024-12-05-182734

Labeled Histogram plotted using bar_label() function

Adding Labels to Multiple Histogram Bars using bar_label()

When plotting multiple histograms together, careful positioning and color coding are essential for clarity. This method is significant for distinguishing between different datasets within a single plot.

import matplotlib.pyplot as plt
import numpy as np

data1 = np.random.randn(1000)
data2 = np.random.randn(1000)

counts1, bins1, patches1 = plt.hist(data1, bins=10, alpha=0.5, label='Data 1', color='blue', edgecolor='black')
counts2, bins2, patches2 = plt.hist(data2, bins=10, alpha=0.5, label='Data 2', color='green', edgecolor='black')

plt.bar_label(patches1)
plt.bar_label(patches2)

plt.title('Histogram with bar_label() Example')
plt.xlabel('Value')
plt.ylabel('Frequency')

plt.legend()
plt.show()

Output:

Screenshot-2024-12-05-183824

Multiple Histogram Labeled using plt.bar_label()

Adding Labels to Histogram Bars Using Conditional Formatting

Conditional formatting can be applied to histogram labels to differentiate data points based on specific conditions, such as surpassing a certain threshold. This approach is useful for emphasizing particular data ranges or values.

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)
counts, bins, patches = plt.hist(data, bins=30, color='skyblue', edgecolor='black')
threshold = 10

for count, bin in zip(counts, bins):
    color = 'red' if count > threshold else 'blue'
    plt.text(bin, count, f'{count:.0f}', color=color, ha='center')

plt.title('Histogram with Color-Coded Labels')
plt.xlabel('Value')
plt.ylabel('Frequency')

plt.show()

Output:

Screenshot-2024-12-05-183134

Histogram with color coded labels

Adding Labels to Histogram Bars using annotate()

The annotate() function in Matplotlib allows for more detailed labeling of histogram bars, including the option to add arrows pointing to specific bars, making it ideal for detailed and informative visualizations.

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)
counts, bins, patches = plt.hist(
    data, bins=30, color='skyblue', edgecolor='black')

for count, bin in zip(counts, bins):
    plt.annotate(f'{count:.0f}',
                 xy=(bin, count),
                 xytext=(bin + 0.1, count + 5),
                 arrowprops=dict(facecolor='black', shrink=0.05))

plt.title('Histogram with Annotations and Arrows')
plt.xlabel('Value')
plt.ylabel('Frequency')

plt.show()

Output:

Screenshot-2024-12-05-183522

Labeled Histogram plotted using annotate() function



Practice Tags :

Similar Reads

three90RightbarBannerImg