Error Bars

Given a series representing the average monthly temperatures of a city and another series representing the standard deviations for those averages, plot a line chart with error bars representing the standard deviations.

 

Example Output:

Use Matplotlib’s errorbar method for plotting a line chart with error bars.

import matplotlib.pyplot as plt

def error_bars_plot(averages, std_devs):
    months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    plt.errorbar(months, averages, yerr=std_devs, color='blue', capsize=5, marker='o')
    plt.title("Average Monthly Temperatures with Standard Deviations")
    plt.xlabel("Months")
    plt.ylabel("Temperature (°C)")
    plt.grid(True)
    plt.show()

# Example usage
average_temperatures = [5, 6, 9, 12, 16, 19, 22, 21, 18, 14, 10, 6]
std_dev_temperatures = [2, 2.1, 2.2, 2.3, 2.5, 2.7, 2.8, 2.6, 2.4, 2.2, 2.1, 2]
error_bars_plot(average_temperatures, std_dev_temperatures)

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!