0
0
Matplotlibdata~10 mins

Rasterization for complex plots 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 enable rasterization for a scatter plot in matplotlib.

Matplotlib
import matplotlib.pyplot as plt
x = range(1000)
y = [i**0.5 for i in x]
plt.scatter(x, y, rasterized=[1])
plt.show()
Drag options to blanks, or click blank then click option'
AFalse
BTrue
CNone
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using False disables rasterization, so the plot remains vector.
Using None or 0 does not enable rasterization.
2fill in blank
medium

Complete the code to rasterize only the line plot, not the entire figure.

Matplotlib
fig, ax = plt.subplots()
line, = ax.plot([1, 2, 3], [4, 5, 6], [1]=True)
plt.show()
Drag options to blanks, or click blank then click option'
Arasterize_plot
Brasterize
Crasterized
Draster
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'rasterize' instead of 'rasterized' causes an error.
Using non-existent properties like 'raster' or 'rasterize_plot'.
3fill in blank
hard

Fix the error in the code to rasterize a scatter plot with many points.

Matplotlib
fig, ax = plt.subplots()
ax.scatter(range(10000), range(10000), rasterized=[1])
plt.savefig('plot.pdf')
Drag options to blanks, or click blank then click option'
A1
BFalse
C'True'
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'True' as a string instead of boolean True.
Passing 1 instead of True.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that rasterizes scatter plots only for points where y is greater than 50.

Matplotlib
plots = {x: ax.scatter(x, y, [1]=True) for x, y in data.items() if y [2] 50}
Drag options to blanks, or click blank then click option'
Arasterized
B>
C<
Drasterize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'rasterize' instead of 'rasterized'.
Using '<' instead of '>' in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that rasterizes bar plots for labels where the length of the label is less than 5.

Matplotlib
plots = {label[1]: ax.bar(label, value, [2]=True) for label, value in data.items() if len(label) [3] 5}
Drag options to blanks, or click blank then click option'
A.upper()
Brasterized
C<
D.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using .lower() instead of .upper() for keys.
Using '>' instead of '<' in the length condition.
Forgetting to set rasterized=True.