0
0
Matplotlibdata~20 mins

Error bar plots in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error Bar Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of error bar plot with asymmetric errors
What will be the output of this code snippet that creates an error bar plot with asymmetric errors?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3, 4])
y = np.array([2, 3, 5, 7])
yerr = [0.5, 0.2, 0.3, 0.4]
yerr_upper = [0.6, 0.3, 0.4, 0.5]

plt.errorbar(x, y, yerr=[yerr, yerr_upper], fmt='o')
plt.show()
AA plot with points at (1,2), (2,3), (3,5), (4,7) and error bars only on the upper side with lengths 0.6, 0.3, 0.4, 0.5.
BA plot with points at (1,2), (2,3), (3,5), (4,7) and symmetric error bars of length 0.5, 0.2, 0.3, 0.4 above and below each point.
CA plot with points at (1,2), (2,3), (3,5), (4,7) but no error bars because yerr is given as a list of two arrays.
DA plot with points at (1,2), (2,3), (3,5), (4,7) and asymmetric error bars: lower errors 0.5, 0.2, 0.3, 0.4 and upper errors 0.6, 0.3, 0.4, 0.5.
Attempts:
2 left
💡 Hint
Check how yerr is passed as a list of two arrays to specify asymmetric errors.
data_output
intermediate
1:30remaining
Number of error bars plotted
Given this code, how many error bars will be drawn on the plot?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y = np.array([1, 4, 9, 16, 25])
errors = np.array([0.1, 0.2, 0.3, 0.4, 0.5])

plt.errorbar(x, y, yerr=errors, fmt='s')
plt.show()
A1 error bar, because yerr is a single array.
B10 error bars, two for each point (upper and lower).
C5 error bars, one for each point.
D0 error bars, because fmt='s' disables error bars.
Attempts:
2 left
💡 Hint
Each point gets one error bar with symmetric upper and lower parts.
🔧 Debug
advanced
2:00remaining
Identify the error in error bar plot code
What error will this code raise when run?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3])
y = np.array([2, 4, 6])
yerr = np.array([0.1, 0.2])

plt.errorbar(x, y, yerr=yerr, fmt='o')
plt.show()
AValueError: x and yerr must have the same size.
BNo error, plot will show with truncated error bars.
CTypeError: yerr must be a scalar or array-like of same length as y.
DIndexError: yerr index out of range.
Attempts:
2 left
💡 Hint
Check if yerr length matches y length.
visualization
advanced
2:00remaining
Effect of capsize parameter in error bar plots
Which option best describes the visual effect of setting capsize=10 in this error bar plot code?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(4)
y = np.array([3, 5, 7, 9])
errors = np.array([0.5, 0.4, 0.3, 0.2])

plt.errorbar(x, y, yerr=errors, fmt='o', capsize=10)
plt.show()
AError bars are 10 times longer than the errors array values.
BError bars have horizontal caps 10 points wide at the ends of each vertical error bar.
CError bars have no caps because capsize=10 is ignored.
DError bars have vertical caps 10 points tall at the ends.
Attempts:
2 left
💡 Hint
Capsize controls the width of the horizontal line at the error bar ends.
🚀 Application
expert
3:00remaining
Calculate mean and plot with error bars showing standard error
You have three groups of data points: Group A: [5, 7, 8, 6] Group B: [6, 9, 7, 10] Group C: [8, 12, 11, 9] Which code correctly calculates the mean and standard error of the mean (SEM) for each group and plots them with error bars?
A
import numpy as np
import matplotlib.pyplot as plt

data = {'A': [5,7,8,6], 'B': [6,9,7,10], 'C': [8,12,11,9]}
means = [np.mean(data[g]) for g in data]
sems = [np.std(data[g], ddof=1)/np.sqrt(len(data[g])) for g in data]
plt.errorbar(['A','B','C'], means, yerr=sems, fmt='o')
plt.show()
B
import numpy as np
import matplotlib.pyplot as plt

data = {'A': [5,7,8,6], 'B': [6,9,7,10], 'C': [8,12,11,9]}
means = [np.mean(data[g]) for g in data]
sems = [np.std(data[g])/np.sqrt(len(data[g])) for g in data]
plt.errorbar(['A','B','C'], means, yerr=sems, fmt='o')
plt.show()
C
import numpy as np
import matplotlib.pyplot as plt

data = {'A': [5,7,8,6], 'B': [6,9,7,10], 'C': [8,12,11,9]}
means = [np.mean(data[g]) for g in data]
sems = [np.var(data[g])/np.sqrt(len(data[g])) for g in data]
plt.errorbar(['A','B','C'], means, yerr=sems, fmt='o')
plt.show()
D
import numpy as np
import matplotlib.pyplot as plt

data = {'A': [5,7,8,6], 'B': [6,9,7,10], 'C': [8,12,11,9]}
means = [np.mean(data[g]) for g in data]
sems = [np.std(data[g])/len(data[g]) for g in data]
plt.errorbar(['A','B','C'], means, yerr=sems, fmt='o')
plt.show()
Attempts:
2 left
💡 Hint
Use sample standard deviation (ddof=1) to calculate SEM correctly.