Bird
0
0

Identify the error in this code using PolyCollection:

medium📝 Debug Q14 of 15
Matplotlib - Performance and Large Data
Identify the error in this code using PolyCollection:
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection

polys = [[(0, 0), (1, 0), (0.5, 1)]]
pc = PolyCollection(polys, facecolors='green')
fig, ax = plt.subplots()
ax.add_collection(pc)
plt.show()
AThe <code>facecolors</code> parameter should be <code>color</code>.
BThe variable name <code>pc</code> is used before assignment.
CNo error; the code will plot a green triangle.
DThe <code>PolyCollection</code> requires polygons with at least 4 points.
Step-by-Step Solution
Solution:
  1. Step 1: Check variable usage

    Variable pc is assigned before use in ax.add_collection(pc).
  2. Step 2: Validate PolyCollection parameters

    facecolors='green' is a valid parameter to color polygons.
  3. Step 3: Confirm polygon points

    Polygon has 3 points forming a triangle, which is valid for PolyCollection.
  4. Final Answer:

    The variable pc is used before assignment. -> Option B
  5. Quick Check:

    Variable pc used before assignment = A [OK]
Quick Trick: Check variable names carefully; pc must be defined before use [OK]
Common Mistakes:
  • Thinking polygons need 4+ points
  • Confusing facecolors with color parameter
  • Assuming variable pc is undefined

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes