How to create a Matplotlib bar chart with a threshold line?

To create a Matplotlib bar chart with a threshold line, we use the axhline() method to draw a horizontal reference line across the chart. This visualization helps identify which data points exceed a specific threshold value.

Basic Bar Chart with Threshold Line

Here's how to create a simple bar chart with a threshold line ?

import numpy as np
import matplotlib.pyplot as plt

# Sample data
categories = ['A', 'B', 'C', 'D', 'E']
values = [8, 15, 12, 6, 18]
threshold = 10

# Create bar chart
plt.figure(figsize=(8, 5))
bars = plt.bar(categories, values, color='lightblue', edgecolor='navy')

# Add threshold line
plt.axhline(y=threshold, color='red', linestyle='--', linewidth=2, label=f'Threshold: {threshold}')

# Customize chart
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Chart with Threshold Line')
plt.legend()
plt.grid(axis='y', alpha=0.3)

plt.tight_layout()
plt.show()

Color-Coded Bars Based on Threshold

We can color bars differently based on whether they exceed the threshold ?

import numpy as np
import matplotlib.pyplot as plt

# Data setup
values = np.array([8.0, 10.0, 15.0, 9.0, 12.0])
categories = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E']
threshold = 10

# Create colors based on threshold
colors = ['red' if val < threshold else 'green' for val in values]

# Create the chart
fig, ax = plt.subplots(figsize=(10, 6))

# Plot bars
bars = ax.bar(categories, values, color=colors, alpha=0.7, edgecolor='black')

# Add threshold line
ax.axhline(y=threshold, color='blue', linestyle='-', linewidth=2, 
           label=f'Threshold: {threshold}')

# Customize chart
ax.set_xlabel('Products')
ax.set_ylabel('Performance Score')
ax.set_title('Product Performance vs Threshold')
ax.legend()
ax.grid(axis='y', alpha=0.3)

# Add value labels on bars
for bar, value in zip(bars, values):
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width()/2., height + 0.1,
            f'{value}', ha='center', va='bottom')

plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Stacked Bars with Threshold

For more complex visualization, we can create stacked bars showing portions above and below the threshold ?

import numpy as np
import matplotlib.pyplot as plt

# Data
threshold = 10
values = np.array([8.0, 10.0, 15.0, 9.0, 12.0])
categories = range(len(values))

# Calculate portions above and below threshold
below_threshold = np.minimum(values, threshold)
above_threshold = np.maximum(values - threshold, 0)

# Create stacked bar chart
fig, ax = plt.subplots(figsize=(10, 6))

# Plot below threshold (blue bars)
bars1 = ax.bar(categories, below_threshold, 0.6, 
               color="lightblue", label="Below Threshold")

# Plot above threshold (orange bars, stacked on top)
bars2 = ax.bar(categories, above_threshold, 0.6, 
               color="orange", bottom=below_threshold, 
               label="Above Threshold")

# Add threshold line
ax.axhline(y=threshold, color='red', linestyle='--', 
           linewidth=2, label=f'Threshold: {threshold}')

# Customize chart
ax.set_xlabel('Data Points')
ax.set_ylabel('Values')
ax.set_title('Stacked Bar Chart with Threshold Line')
ax.legend()
ax.grid(axis='y', alpha=0.3)

plt.tight_layout()
plt.show()

Key Parameters

Parameter Description Example
y Y-coordinate of the horizontal line y=10
color Line color color='red'
linestyle Line style linestyle='--'
linewidth Line thickness linewidth=2

Conclusion

Use axhline() to add threshold lines to bar charts for clear reference points. Color-code bars based on threshold values to enhance data interpretation and highlight performance metrics.

Updated on: 2026-03-25T21:35:33+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements