By Hemanta Sundaray on 2022-06-06
When we represent data as bars of different heights, sometimes, we need to visually communicate some sort of uncertainty in the heights of those bars.
Here are some examples:
To display error visually in a bar chart, we often use error bars to show where each bar could be, taking errors into account.
import pandas as pd
import matplotlib. pyplot as plt
subjects = ["Physics", "Chemistry", "Mathematics", "Botany", "Zoology"]
marks = [85, 76, 90, 84, 58]
error = [10, 7, 4, 6, 5]
plt.bar(range(len(subjects)), marks, yerr=error, capsize=6)
Each of the black lines is called an error bar. The taller the bar is, the more uncertain we are about the height of the blue bar. The horizontal lines at the top and bottom are called caps. They make it easier to read the error bars.
To make the caps wide and easy to read, we can increase the cap size.
plt.bar(range(len(subjects)), marks, yerr=error, capsize=15)