0
0
Matplotlibdata~20 mins

Transparent backgrounds in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Transparent Background Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of saving a plot with transparent background
What will be the background color of the saved image when running this code?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('plot.png', transparent=True)
plt.close()
AThe saved image will have a gray background.
BThe saved image will have a white background.
CThe saved image will have a black background.
DThe saved image will have a transparent background.
Attempts:
2 left
💡 Hint
Check the meaning of the 'transparent' parameter in plt.savefig.
data_output
intermediate
2:00remaining
Background color of figure and axes with transparency
Given this code, what is the background color of the figure and axes when displayed?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig.patch.set_alpha(0.5)
ax.plot([1, 2], [3, 4])
plt.show()
AFigure background is semi-transparent, axes background is default opaque white.
BBoth figure and axes backgrounds are fully transparent.
CBoth figure and axes backgrounds are semi-transparent gray.
DFigure background is default opaque white, axes background is semi-transparent.
Attempts:
2 left
💡 Hint
Alpha controls transparency but default colors remain unless changed.
🔧 Debug
advanced
2:00remaining
Why does transparent=True not work as expected?
This code saves a plot with transparent=True but the background is still white. What is the likely cause?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [3, 2, 1])
plt.savefig('plot.png', transparent=True)
plt.close()
AThe axes background color must be set to None for transparency.
BThe transparent parameter only works for SVG files, not PNG.
CThe figure background color is explicitly set to white, overriding transparency.
DThe plot command must include alpha=0.5 for transparency to work.
Attempts:
2 left
💡 Hint
Check if figure or axes background colors are set explicitly.
visualization
advanced
2:00remaining
Visual effect of transparent background on saved plot
Which option best describes the visual difference between these two saved images: one saved with transparent=True and one without?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([0, 1], [0, 1])
plt.savefig('opaque.png')
plt.savefig('transparent.png', transparent=True)
plt.close()
AThe transparent.png image has no background color, showing the desktop or webpage behind it.
BBoth images look identical with white backgrounds.
CThe transparent.png image has a black background.
DThe opaque.png image has a transparent background, transparent.png has white.
Attempts:
2 left
💡 Hint
Think about what transparent background means for image files.
🧠 Conceptual
expert
3:00remaining
Effect of transparent backgrounds on plot elements
When saving a matplotlib plot with transparent=True, which elements remain fully opaque by default?
AOnly the figure background becomes transparent; axes background and plot elements remain opaque.
BPlot lines, markers, and text remain fully opaque; only figure and axes backgrounds become transparent.
CAll elements including plot lines and text become semi-transparent automatically.
DAxes background and plot elements become transparent; figure background remains opaque.
Attempts:
2 left
💡 Hint
Consider what transparent=True affects in the saved image.