0
0
Matplotlibdata~20 mins

Error bars on bar charts in Matplotlib - Practice Problems & Coding Challenges

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

x = ['A', 'B', 'C']
values = [5, 7, 3]
errors = [0.5, 1.0, 0.3]

plt.bar(x, values, yerr=errors, capsize=5)
plt.show()
AA bar chart with three bars labeled A, B, C, each with vertical error bars of lengths 0.5, 1.0, and 0.3 respectively, and caps on error bars.
BA line chart connecting points A, B, C with error bars.
CA bar chart with three bars but no error bars shown.
DA scatter plot with error bars but no bars.
Attempts:
2 left
💡 Hint
Look at the parameters yerr and capsize in plt.bar.
data_output
intermediate
1:30remaining
Resulting error bar lengths in bar chart
Given the following data and error arrays, what are the lengths of the error bars shown on the bar chart?
Matplotlib
values = [10, 15, 8]
errors = [2, 3, 1]
# These are used as yerr in plt.bar
A[2, 3, 1]
B[10, 15, 8]
C[12, 18, 9]
D[1, 2, 3]
Attempts:
2 left
💡 Hint
yerr parameter controls the error bar lengths.
🔧 Debug
advanced
2:00remaining
Identify the error in error bar plotting code
What error will this code raise when trying to plot error bars on a bar chart?
Matplotlib
import matplotlib.pyplot as plt

x = ['X', 'Y', 'Z']
values = [4, 6, 5]
errors = [0.2, 0.3]

plt.bar(x, values, yerr=errors)
plt.show()
ATypeError: yerr must be a scalar or array of same length as values
BValueError: shape mismatch between values and errors
CNo error, plot shows correctly
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Check if the length of errors matches the length of values.
visualization
advanced
2:00remaining
Effect of capsize parameter on error bars
Which option best describes the visual difference when changing the capsize parameter in plt.bar with error bars?
Matplotlib
import matplotlib.pyplot as plt

x = ['P', 'Q', 'R']
values = [7, 9, 6]
errors = [0.5, 0.7, 0.4]

plt.bar(x, values, yerr=errors, capsize=10)
plt.show()
AError bars are removed completely.
BError bars are thicker but have no caps.
CError bars are colored red instead of black.
DError bars have horizontal caps at the ends, making them look like a 'T' shape with length 10 pixels.
Attempts:
2 left
💡 Hint
capsize controls the width of the horizontal line at the top of error bars.
🚀 Application
expert
3:00remaining
Calculate and plot error bars from raw data
You have three groups of measurements: group1 = [5, 6, 7], group2 = [8, 9, 10], group3 = [4, 5, 6]. Calculate the mean and standard deviation for each group, then plot a bar chart with error bars representing the standard deviation. Which code snippet correctly performs this task?
A
import matplotlib.pyplot as plt
import numpy as np

groups = [ [5,6,7], [8,9,10], [4,5,6] ]
means = [np.mean(g) for g in groups]
stds = [np.std(g, ddof=0) for g in groups]
labels = ['G1', 'G2', 'G3']
plt.bar(labels, means, yerr=stds, capsize=5)
plt.show()
B
import matplotlib.pyplot as plt
import numpy as np

groups = [ [5,6,7], [8,9,10], [4,5,6] ]
means = [np.mean(g) for g in groups]
stds = [np.std(g) for g in groups]
labels = ['G1', 'G2', 'G3']
plt.bar(labels, means, yerr=stds)
plt.show()
C
import matplotlib.pyplot as plt
import numpy as np

groups = [ [5,6,7], [8,9,10], [4,5,6] ]
means = [np.mean(g) for g in groups]
stds = [np.std(g, ddof=1) for g in groups]
labels = ['G1', 'G2', 'G3']
plt.bar(labels, means, yerr=stds, capsize=5)
plt.show()
D
import matplotlib.pyplot as plt
import numpy as np

groups = [ [5,6,7], [8,9,10], [4,5,6] ]
means = [np.mean(g) for g in groups]
stds = [np.std(g, ddof=2) for g in groups]
labels = ['G1', 'G2', 'G3']
plt.bar(labels, means, yerr=stds, capsize=5)
plt.show()
Attempts:
2 left
💡 Hint
Use sample standard deviation (ddof=1) for error bars representing variability.