Challenge - 5 Problems
Path Simplification Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of path simplification with default tolerance
What is the output of the following code that plots a path and applies path simplification with default tolerance?
Matplotlib
import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as patches vertices = [(0, 0), (1, 0.1), (2, -0.1), (3, 5), (4, 6), (5, 7)] codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO] path = Path(vertices, codes) simplified_path = path.simplify() fig, ax = plt.subplots() patch = patches.PathPatch(path, facecolor='none', lw=2, label='Original') patch_s = patches.PathPatch(simplified_path, facecolor='none', lw=2, linestyle='--', label='Simplified') ax.add_patch(patch) ax.add_patch(patch_s) ax.legend() ax.set_xlim(-1, 6) ax.set_ylim(-1, 8) plt.show() len_original = len(path.vertices) len_simplified = len(simplified_path.vertices) (len_original, len_simplified)
Attempts:
2 left
💡 Hint
Check how many points are removed by default simplification in matplotlib Path.
✗ Incorrect
The original path has 6 points. The simplify() method removes points that are nearly collinear within a default tolerance, reducing the number of points to 4.
❓ data_output
intermediate2:00remaining
Number of vertices after increasing tolerance
Given a path with vertices [(0,0), (1,0.1), (2,-0.1), (3,5), (4,6), (5,7)], what is the number of vertices after simplification with tolerance=1.0?
Matplotlib
import matplotlib.pyplot as plt from matplotlib.path import Path vertices = [(0, 0), (1, 0.1), (2, -0.1), (3, 5), (4, 6), (5, 7)] codes = [Path.MOVETO] + [Path.LINETO]*5 path = Path(vertices, codes) simplified_path = path.simplify(tolerance=1.0) len(simplified_path.vertices)
Attempts:
2 left
💡 Hint
Higher tolerance removes more points.
✗ Incorrect
With tolerance=1.0, the simplification removes more points, leaving only 3 vertices.
❓ visualization
advanced3:00remaining
Visual difference between low and high tolerance simplification
Which plot correctly shows the original path and two simplified paths with tolerance=0.01 and tolerance=1.0 respectively?
Matplotlib
import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as patches vertices = [(0, 0), (1, 0.1), (2, -0.1), (3, 5), (4, 6), (5, 7)] codes = [Path.MOVETO] + [Path.LINETO]*5 path = Path(vertices, codes) simplified_low = path.simplify(tolerance=0.01) simplified_high = path.simplify(tolerance=1.0) fig, ax = plt.subplots() ax.plot(*zip(*vertices), 'o-', label='Original') ax.plot(*zip(*simplified_low.vertices), 's--', label='Simplified tol=0.01') ax.plot(*zip(*simplified_high.vertices), 'd-.', label='Simplified tol=1.0') ax.legend() plt.show()
Attempts:
2 left
💡 Hint
Higher tolerance removes more points, so the path looks simpler.
✗ Incorrect
The plot with original path as solid circles, low tolerance simplification close to original with squares, and high tolerance simplification with fewer points as diamonds matches the expected behavior.
🧠 Conceptual
advanced1:30remaining
Effect of tolerance parameter in Path.simplify()
What is the effect of increasing the tolerance parameter in matplotlib's Path.simplify() method?
Attempts:
2 left
💡 Hint
Think about what simplification means in terms of points.
✗ Incorrect
Increasing tolerance allows the simplification algorithm to remove points that deviate less from a straight line, thus reducing the number of vertices.
🔧 Debug
expert2:00remaining
Identify the error in path simplification code
What error does the following code raise?
import matplotlib.pyplot as plt
from matplotlib.path import Path
vertices = [(0, 0), (1, 1), (2, 2)]
codes = [Path.MOVETO, Path.LINETO]
path = Path(vertices, codes)
simplified = path.simplify()
print(len(simplified.vertices))
Attempts:
2 left
💡 Hint
Check the length of codes and vertices lists.
✗ Incorrect
The codes list has length 2 but vertices has length 3, causing a ValueError when creating the Path object.