Bird
0
0

Identify the error in this code for before-after bar plot:

medium📝 Debug Q14 of 15
Matplotlib - Real-World Visualization Patterns
Identify the error in this code for before-after bar plot:
import matplotlib.pyplot as plt
before = [3, 4]
after = [5, 6]
plt.bar([0, 1], before)
plt.bar([0, 1], after)
plt.show()
Aplt.show() is missing
BBars for before and after overlap at same positions
Cbefore and after lists must be same length
Dplt.bar requires three arguments
Step-by-Step Solution
Solution:
  1. Step 1: Check bar positions

    Both before and after bars are plotted at positions 0 and 1, causing them to overlap and hide one another.
  2. Step 2: Identify correct fix

    To avoid overlap, after bars should be shifted to different x positions, e.g., [0.3, 1.3].
  3. Final Answer:

    Bars for before and after overlap at same positions -> Option B
  4. Quick Check:

    Same x positions cause bar overlap [OK]
Quick Trick: Shift bars on x-axis to avoid overlap [OK]
Common Mistakes:
  • Thinking plt.bar needs 3 arguments
  • Ignoring bar overlap issue
  • Assuming plt.show() is missing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes