Instagram
youtube
Facebook
Twitter

Use Error Bars in a Bar Chart

Description:
This program creates a bar chart and adds error bars to represent uncertainty or variability (e.g., standard deviation or confidence intervals) in the data.



Code Explanation:

  • Error bars show the uncertainty or variation in data.

  • yerr parameter is used in bar() to add error values.

  • Vertical lines on each bar represent possible data range.

  • Useful in experiments or surveys where variation is expected.


Program:

import matplotlib.pyplot as plt

# Sample data
categories = ['A', 'B', 'C', 'D']
values = [20, 35, 30, 25]
errors = [2, 3, 4, 2]  # error margins for each bar

# Plotting the bar chart with error bars
plt.bar(categories, values, yerr=errors, capsize=5, color='skyblue')

# Adding title and labels
plt.title('Bar Chart with Error Bars')
plt.xlabel('Category')
plt.ylabel('Value')

# Show grid
plt.grid(axis='y')

# Show the plot
plt.show()



Output: