0
0
Matplotlibdata~10 mins

LineCollection and PolyCollection for speed in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the correct collection class for drawing multiple lines efficiently.

Matplotlib
from matplotlib.collections import [1]

lines = LineCollection([[(0, 0), (1, 1)], [(1, 0), (0, 1)]])
Drag options to blanks, or click blank then click option'
ACircleCollection
BPolyCollection
CPatchCollection
DLineCollection
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing PolyCollection instead of LineCollection.
Using PatchCollection which is for patches, not lines.
2fill in blank
medium

Complete the code to create a PolyCollection from a list of polygons.

Matplotlib
from matplotlib.collections import PolyCollection

polygons = [[(0, 0), (1, 0), (0.5, 1)], [(1, 1), (2, 1), (1.5, 2)]]
collection = PolyCollection([1])
Drag options to blanks, or click blank then click option'
Alines
Bpatches
Cpolygons
Dpoints
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'lines' or 'points' instead of 'polygons'.
Passing an undefined variable.
3fill in blank
hard

Fix the error in the code to add a LineCollection to the axes.

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

fig, ax = plt.subplots()
lines = [[(0, 0), (1, 1)], [(1, 0), (0, 1)]]
collection = LineCollection(lines)
ax.[1](collection)
plt.show()
Drag options to blanks, or click blank then click option'
Aadd_collection
Badd_patch
Cadd_artist
Dadd_line
Attempts:
3 left
💡 Hint
Common Mistakes
Using add_line which does not exist.
Using add_patch which is for patches, not collections.
4fill in blank
hard

Fill both blanks to create a PolyCollection with face colors and add it to the axes.

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection

fig, ax = plt.subplots()
polygons = [[(0, 0), (1, 0), (0.5, 1)], [(1, 1), (2, 1), (1.5, 2)]]
colors = ['red', 'blue']
collection = PolyCollection([1], facecolors=[2])
ax.add_collection(collection)
plt.show()
Drag options to blanks, or click blank then click option'
Apolygons
Blines
Ccolors
Dpatches
Attempts:
3 left
💡 Hint
Common Mistakes
Passing lines instead of polygons.
Passing patches instead of colors for facecolors.
5fill in blank
hard

Fill all three blanks to create a LineCollection with linewidths and add it to the axes.

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

fig, ax = plt.subplots()
lines = [[(0, 0), (1, 1)], [(1, 0), (0, 1)]]
widths = [2, 4]
collection = LineCollection([1], linewidths=[2], colors='green')
ax.[3](collection)
plt.show()
Drag options to blanks, or click blank then click option'
Apolygons
Blines
Cadd_collection
Dwidths
Attempts:
3 left
💡 Hint
Common Mistakes
Using polygons instead of lines.
Using add_patch instead of add_collection.