Challenge - 5 Problems
Line and Polygon Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of LineCollection plot segments count
What is the number of line segments drawn by this code using LineCollection?
Matplotlib
import matplotlib.pyplot as plt from matplotlib.collections import LineCollection segments = [ [(0, 0), (1, 1)], [(1, 1), (2, 0)], [(2, 0), (3, 1)]] line_collection = LineCollection(segments) fig, ax = plt.subplots() ax.add_collection(line_collection) ax.autoscale() plt.close(fig) num_segments = len(line_collection.get_segments()) print(num_segments)
Attempts:
2 left
💡 Hint
Count how many pairs of points are in the segments list.
✗ Incorrect
LineCollection takes a list of line segments. Each segment is a pair of points. Here, there are 3 segments, so the output is 3.
❓ data_output
intermediate1:30remaining
Number of polygons in PolyCollection
How many polygons does this PolyCollection contain?
Matplotlib
from matplotlib.collections import PolyCollection polygons = [ [(0, 0), (1, 0), (0.5, 1)], [(1, 1), (2, 1), (1.5, 2)]] poly_collection = PolyCollection(polygons) num_polygons = len(poly_collection.get_paths()) print(num_polygons)
Attempts:
2 left
💡 Hint
Each polygon is a list of points; count how many polygons are given.
✗ Incorrect
PolyCollection holds multiple polygons. Here, two polygons are passed, so the output is 2.
❓ visualization
advanced2:00remaining
Visual difference between LineCollection and multiple plot calls
Which option shows the fastest way to plot 1000 line segments using matplotlib?
Matplotlib
import matplotlib.pyplot as plt from matplotlib.collections import LineCollection import numpy as np segments = [ [(i, 0), (i, 1)] for i in range(1000)] # Option A: Using LineCollection fig, ax = plt.subplots() lc = LineCollection(segments) ax.add_collection(lc) ax.autoscale() plt.close(fig) # Option B: Using 1000 plot calls fig2, ax2 = plt.subplots() for seg in segments: ax2.plot([seg[0][0], seg[1][0]], [seg[0][1], seg[1][1]]) plt.close(fig2)
Attempts:
2 left
💡 Hint
Think about how matplotlib handles many lines internally.
✗ Incorrect
LineCollection batches all line segments into one object, reducing overhead and speeding up rendering compared to many individual plot calls.
🔧 Debug
advanced1:30remaining
Error when passing wrong data to PolyCollection
What error does this code raise when creating a PolyCollection with incorrect polygon data?
Matplotlib
from matplotlib.collections import PolyCollection polygons = [ [(0, 0), (1, 0)], [(1, 1), (2, 1), (1.5, 2)]] poly_collection = PolyCollection(polygons)
Attempts:
2 left
💡 Hint
A polygon needs at least 3 points to be valid.
✗ Incorrect
PolyCollection requires each polygon to have at least 3 vertices. The first polygon has only 2 points, causing a ValueError.
🚀 Application
expert2:30remaining
Efficiently plotting thousands of polygons with PolyCollection
You want to plot 5000 polygons efficiently. Which approach is best?
Matplotlib
import matplotlib.pyplot as plt from matplotlib.collections import PolyCollection import numpy as np polygons = [np.random.rand(4, 2) for _ in range(5000)] # Option A: Create one PolyCollection with all polygons # Option B: Plot each polygon with plt.fill() # Option C: Use multiple PolyCollections each with 100 polygons # Option D: Use scatter plot instead
Attempts:
2 left
💡 Hint
Batching many polygons reduces overhead and speeds up rendering.
✗ Incorrect
One PolyCollection with all polygons reduces the number of draw calls and is the most efficient way to plot many polygons.