Error Bar Chart
Urban Noise Level Measurements
Environmental noise measurements across urban locations with safety thresholds.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
# Noise measurement data
locations = ['Subway\nStation', 'City\nStreet', 'Office\nOpen Plan', 'Library', 'Park', 'Concert\nVenue']
peak_db = np.array([102, 85, 68, 42, 55, 115])
avg_db = np.array([88, 72, 58, 38, 48, 105])
peak_err = np.array([8, 6, 5, 4, 5, 10])
avg_err = np.array([5, 4, 4, 3, 4, 8])
fig, ax = plt.subplots(figsize=(10, 6), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
x = np.arange(len(locations))
width = 0.35
# Color based on safety (85 dB threshold)
colors_peak = ['#F5276C' if p > 85 else '#6CF527' for p in peak_db]
colors_avg = ['#F54927' if a > 85 else '#27D3F5' for a in avg_db]
for i, (p, pe, c) in enumerate(zip(peak_db, peak_err, colors_peak)):
ax.bar(x[i] - width/2, p, width, yerr=pe, color=c, edgecolor='#1f2937',
capsize=4, error_kw={'ecolor': '#374151', 'elinewidth': 1.5})
for i, (a, ae, c) in enumerate(zip(avg_db, avg_err, colors_avg)):
ax.bar(x[i] + width/2, a, width, yerr=ae, color=c, edgecolor='#1f2937',
capsize=4, error_kw={'ecolor': '#374151', 'elinewidth': 1.5})
ax.axhline(y=85, color='#F5B027', linestyle='--', linewidth=2,
label='OSHA Limit (85 dB)', alpha=0.8)
ax.axhline(y=70, color='#27D3F5', linestyle=':', linewidth=1.5,
label='WHO Recommended (70 dB)', alpha=0.7)
from matplotlib.patches import Patch
legend_elements = [Patch(facecolor='#F5276C', label='Peak (Hazardous)'),
Patch(facecolor='#6CF527', label='Peak (Safe)'),
Patch(facecolor='#F54927', label='Avg (Hazardous)'),
Patch(facecolor='#27D3F5', label='Avg (Safe)')]
ax.set_xlabel('Location', fontsize=11, color='#374151', fontweight='500')
ax.set_ylabel('Sound Level (dB)', fontsize=11, color='#374151', fontweight='500')
ax.set_title('Urban Noise Level Measurements', fontsize=14,
color='#1f2937', fontweight='bold', pad=15)
ax.set_xticks(x)
ax.set_xticklabels(locations)
ax.legend(handles=legend_elements, facecolor='#f8fafc', edgecolor='#d1d5db',
fontsize=8, loc='upper left', ncol=2)
ax.tick_params(colors='#6b7280', labelsize=9)
ax.set_ylim(0, 135)
ax.grid(True, axis='y', alpha=0.3, color='#d1d5db')
for spine in ax.spines.values():
spine.set_color('#d1d5db')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More Error Bar Chart examples
☕