Bird
Raised Fist0
Matplotlibdata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the yerr parameter do when creating a bar chart in matplotlib?
easy
A. It changes the color of the bars.
B. It sets the width of the bars.
C. It adds vertical error bars to each bar showing uncertainty.
D. It adds labels to the bars.

Solution

  1. Step 1: Understand the role of yerr

    The yerr parameter in matplotlib bar charts specifies the size of vertical error bars to show uncertainty or variation.
  2. Step 2: Compare with other options

    Other options like color, width, or labels are controlled by different parameters, not yerr.
  3. Final Answer:

    It adds vertical error bars to each bar showing uncertainty. -> Option C
  4. Quick Check:

    yerr = vertical error bars [OK]
Hint: Remember: yerr means vertical error bars [OK]
Common Mistakes:
  • Confusing yerr with bar width
  • Thinking yerr changes bar color
  • Assuming yerr adds labels
2. Which of the following is the correct syntax to add error bars with size 0.2 to a bar chart using matplotlib?
easy
A. plt.bar(x, height, err=0.2)
B. plt.bar(x, height, error=0.2)
C. plt.bar(x, height, yerror=0.2)
D. plt.bar(x, height, yerr=0.2)

Solution

  1. Step 1: Recall the correct parameter name

    The correct parameter to add vertical error bars in matplotlib bar charts is yerr.
  2. Step 2: Check the syntax

    The syntax plt.bar(x, height, yerr=0.2) correctly adds error bars of size 0.2.
  3. Final Answer:

    plt.bar(x, height, yerr=0.2) -> Option D
  4. Quick Check:

    Correct parameter is yerr [OK]
Hint: Use exact parameter name yerr for error bars [OK]
Common Mistakes:
  • Using wrong parameter names like error or err
  • Misspelling yerr as yerror
  • Forgetting to assign a value to yerr
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
x = [1, 2, 3]
heights = [3, 5, 2]
errors = [0.5, 0.2, 0.3]
plt.bar(x, heights, yerr=errors, capsize=5)
plt.show()
medium
A. A syntax error because capsize is not a valid parameter.
B. A bar chart with bars at x=1,2,3 with heights 3,5,2 and vertical error bars with caps of length 5.
C. A bar chart with bars but no error bars visible.
D. A bar chart with horizontal error bars.

Solution

  1. Step 1: Understand the code

    The code creates bars at positions 1, 2, 3 with heights 3, 5, 2 respectively and adds vertical error bars sized by the list errors.
  2. Step 2: Role of capsize

    The capsize=5 parameter adds horizontal caps at the ends of error bars, making them visible and 5 points long.
  3. Final Answer:

    A bar chart with bars at x=1,2,3 with heights 3,5,2 and vertical error bars with caps of length 5. -> Option B
  4. Quick Check:

    yerr + capsize = vertical error bars with caps [OK]
Hint: capsize adds visible ends to error bars [OK]
Common Mistakes:
  • Thinking capsize disables error bars
  • Confusing vertical and horizontal error bars
  • Assuming capsize causes syntax error
4. Identify the error in this code that tries to add error bars to a bar chart:
import matplotlib.pyplot as plt
x = [1, 2, 3]
heights = [4, 6, 5]
errors = [0.3, 0.4, 0.2]
plt.bar(x, heights, yerror=errors)
plt.show()
medium
A. The parameter name should be yerr, not yerror.
B. The errors list length does not match heights length.
C. plt.bar does not support error bars.
D. The x values must be strings, not integers.

Solution

  1. Step 1: Check parameter names

    The parameter to add vertical error bars is yerr, not yerror.
  2. Step 2: Validate other parts

    The lengths of errors and heights match, and plt.bar supports error bars with yerr.
  3. Final Answer:

    The parameter name should be yerr, not yerror. -> Option A
  4. Quick Check:

    Correct parameter is yerr, not yerror [OK]
Hint: Use exact parameter yerr, spelling matters [OK]
Common Mistakes:
  • Using yerror instead of yerr
  • Ignoring parameter name case sensitivity
  • Assuming plt.bar can't have error bars
5. You want to create a bar chart with 4 bars and different error sizes for each bar. Which code correctly achieves this with visible error bar caps?
hard
A. plt.bar([1,2,3,4], [5,7,3,4], yerr=[0.2,0.3,0.1,0.4], capsize=3)
B. plt.bar([1,2,3,4], [5,7,3,4], yerr=0.3, capsize=0)
C. plt.bar([1,2,3,4], [5,7,3,4], yerr=[0.2,0.3], capsize=3)
D. plt.bar([1,2,3,4], [5,7,3,4], error=[0.2,0.3,0.1,0.4], capsize=3)

Solution

  1. Step 1: Check error bar sizes match bars

    There are 4 bars, so the yerr list must have 4 values. plt.bar([1,2,3,4], [5,7,3,4], yerr=[0.2,0.3,0.1,0.4], capsize=3) has 4 error sizes matching bars.
  2. Step 2: Verify capsize for visible caps

    plt.bar([1,2,3,4], [5,7,3,4], yerr=[0.2,0.3,0.1,0.4], capsize=3) uses capsize=3 which makes error bar caps visible. Other options fail: one uses a single yerr value with capsize=0 (no caps), another provides only two error sizes (mismatch), and one uses wrong parameter error.
  3. Final Answer:

    plt.bar([1,2,3,4], [5,7,3,4], yerr=[0.2,0.3,0.1,0.4], capsize=3) -> Option A
  4. Quick Check:

    yerr list length = bars count + capsize > 0 [OK]
Hint: Match yerr length to bars and set capsize > 0 [OK]
Common Mistakes:
  • Using single yerr value for multiple bars
  • Setting capsize to zero hides caps
  • Using wrong parameter name error instead of yerr
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard