Error bars in Matplotlib
Categories: matplotlib

We can add error bars to our chart, to indicate the degree of variation in the data. Here is what it looks like:
Here is the code to plot this chart:
import matplotlib.pyplot as plt
import csv
with open("2009-temp-monthly-list.csv") as csv_file:
csv_reader = csv.reader(csv_file, quoting=csv.QUOTE_NONNUMERIC)
temperature_lists = list(csv_reader)
month_names = ["J", "F", "M", "A", "M", "J",
"J", "A", "S", "O", "N", "D"]
months = range(12)
temperature = [sum(k)/len(k) for k in temperature_lists]
errors = [(max(k)-min(k))/2 for k in temperature_lists]
plt.title("Temperature bar chart 2009")
plt.xlabel("Month")
plt.ylabel("Temperature")
plt.bar(months, temperature, yerr=errors)
plt.xticks(months, month_names)
plt.show()
This code is available on github as barchart_error_bars.py.
We will look at the important new features used in this code.
The data
Since we want to plot tha variance of the data, we cannot use the 2009-temp-monthly.csv data. That data only contains one value for each month, the average value.
Instead, we use the 2009-temp-monthly-list.csv data. This is a list of 12 sublists. Each sublist contains the temperatures of each day in the month. The first sublist contains 31 values corresponding to the temperatures of the 31 days in January. Yje second contains 28 values for the days in February, and so on. This is read into temperature_lists
in the code above.
See the description of the data sets.
Processing the data
We need to calculate 2 sets of values:
- The average temperature for each month. This creates a list
temperature
of 12 values that determine the height of each bar in the graph. - The error range of temperatures for each month. This creates a list
errors
of 12 values that determine the length of each error bar.
Here are the calculations:
temperature = [sum(k)/len(k) for k in temperature_lists]
errors = [(max(k)-min(k))/2 for k in temperature_lists]
To calculate the average temperature
, we loop over every sublist k
. We divide the sum of the elements in the sublist (given by sum(k)
) by the number of the elements in the sublist (given by len(k)
), which of course tells us the mean average. This will be the same value that is present in the 2009-temp-monthly.csv data.
We also calculate the error range errors
for each month. There are various ways to calculate this, and you can use whichever one you prefer. To keep things simple, we just take the difference between the maximum value and the minimum value, divided by 2.
Plotting the error bars
Here is how we plot the error bars:
plt.bar(months, temperature, yerr=errors)
We simply pass in the errors
list as the yerr
parameter.
See also
Join the PythonInformer Newsletter
Sign up using this form to receive an email when new content is added:
Popular tags
2d arrays abstract data type alignment and angle animation arc array arrays bar chart bar style behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data science data types decorator design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop formula function function composition function plot functools game development generativepy tutorial generator geometry gif global variable gradient greyscale higher order function hsl html image image processing imagesurface immutable object in operator index inner function input installing iter iterable iterator itertools join l system lambda function latex len lerp line line plot line style linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map marker style matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations pie chart pil pillow polygon pong positional parameter print product programming paradigms programming techniques pure function python standard library radial gradient range recipes rectangle recursion reduce regular polygon repeat rgb rotation roundrect scaling scatter plot scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template tex text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip zip_longest