0
0
Matplotlibdata~10 mins

Transparent backgrounds 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 save the plot with a transparent background.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('plot.png', transparent=[1])
Drag options to blanks, or click blank then click option'
AFalse
BTrue
C'yes'
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a boolean for the transparent parameter.
Forgetting to set transparent=True and getting a white background.
2fill in blank
medium

Complete the code to create a figure with a transparent background.

Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(facecolor=[1])
plt.plot([1, 2, 3], [3, 2, 1])
plt.show()
Drag options to blanks, or click blank then click option'
A'none'
B'white'
C'black'
D'transparent'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'transparent' which is not a valid color name.
Using 'white' which makes the background white, not transparent.
3fill in blank
hard

Fix the error in the code to save the plot with a transparent background.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 1])
plt.savefig('plot.png', transparent=[1])
Drag options to blanks, or click blank then click option'
ATrue
B'True'
C1
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'True' as a string instead of True as a boolean.
Using 1 which may work but is not the recommended boolean.
4fill in blank
hard

Fill both blanks to create and save a plot with a transparent figure and save background.

Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(facecolor=[1])
plt.plot([1, 2, 3], [1, 4, 9])
plt.savefig('plot.png', transparent=[2])
Drag options to blanks, or click blank then click option'
A'none'
BTrue
C'white'
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'white' for facecolor which is not transparent.
Setting transparent=False which disables transparency on save.
5fill in blank
hard

Fill all three blanks to create a transparent figure, plot data, and save with transparent background.

Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(facecolor=[1])
plt.plot([[2]], [[3]])
plt.savefig('plot.png', transparent=True)
Drag options to blanks, or click blank then click option'
A'none'
B1, 2, 3
C3, 2, 1
D'white'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'white' for facecolor which is not transparent.
Swapping x and y data lists causing confusing plots.