Bird
Raised Fist0
Matplotlibdata~20 mins

Transparent backgrounds in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does setting transparent=True in plt.savefig() do in matplotlib?
easy
A. Saves the figure without any axes
B. Saves the figure with a white background
C. Saves the figure with a transparent background
D. Saves the figure with a black background

Solution

  1. Step 1: Understand the transparent parameter in plt.savefig()

    The transparent parameter controls whether the saved figure's background is transparent or not.
  2. Step 2: Effect of setting transparent=True

    When set to True, the figure background becomes transparent instead of the default color.
  3. Final Answer:

    Saves the figure with a transparent background -> Option C
  4. Quick Check:

    transparent=True means transparent background [OK]
Hint: Remember: transparent=True makes background clear [OK]
Common Mistakes:
  • Thinking it changes axes visibility
  • Assuming it sets background to white
  • Confusing it with figure size
2. Which of the following is the correct syntax to save a matplotlib figure with a transparent background?
easy
A. plt.savefig('plot.png', transparent=True)
B. plt.save('plot.png', transparent=True)
C. plt.savefig('plot.png', background='transparent')
D. plt.savefig('plot.png', transparent=False)

Solution

  1. Step 1: Identify the correct function to save figures

    The correct function is plt.savefig(), not plt.save().
  2. Step 2: Check the correct parameter for transparency

    The parameter is transparent=True, not background='transparent' or transparent=False.
  3. Final Answer:

    plt.savefig('plot.png', transparent=True) -> Option A
  4. Quick Check:

    Correct function and parameter = plt.savefig('plot.png', transparent=True) [OK]
Hint: Use plt.savefig with transparent=True to save transparent images [OK]
Common Mistakes:
  • Using plt.save instead of plt.savefig
  • Wrong parameter name like background='transparent'
  • Setting transparent=False by mistake
3. What will be the background color of the saved image after running this code?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('myplot.png', transparent=True)
medium
A. White background
B. Transparent background
C. Black background
D. Red background

Solution

  1. Step 1: Analyze the plt.savefig() call

    The code uses transparent=True in plt.savefig(), which sets the saved image background to transparent.
  2. Step 2: Understand default background behavior

    Without transparent=True, the background would be white, but here it is explicitly transparent.
  3. Final Answer:

    Transparent background -> Option B
  4. Quick Check:

    transparent=True means transparent background [OK]
Hint: transparent=True means no background color [OK]
Common Mistakes:
  • Assuming default white background
  • Confusing plot color with background
  • Thinking transparent=True changes plot line color
4. Identify the error in this code that tries to save a transparent background image:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 1])
plt.savefig('image.png', transparent='yes')
medium
A. The transparent parameter should be a boolean, not a string
B. The filename must be .jpg for transparency
C. plt.plot() must be called after plt.savefig()
D. transparent parameter is not supported in plt.savefig()

Solution

  1. Step 1: Check the type of transparent parameter

    The transparent parameter expects a boolean value True or False, not a string like 'yes'.
  2. Step 2: Understand correct usage of transparent

    Passing a string will cause the parameter to be ignored or cause an error; it must be transparent=True for transparency.
  3. Final Answer:

    The transparent parameter should be a boolean, not a string -> Option A
  4. Quick Check:

    transparent=True is boolean, not string [OK]
Hint: Use True/False for transparent, not strings like 'yes' [OK]
Common Mistakes:
  • Passing 'yes' or 'no' as string instead of boolean
  • Using unsupported file formats for transparency
  • Calling plot after savefig
5. You want to save a matplotlib figure with a transparent background and ensure it blends well on any website background. Which file format and save command should you use?
hard
A. plt.savefig('figure.jpg', transparent=True) - JPEG supports transparency
B. plt.savefig('figure.bmp', transparent=True) - BMP supports transparency
C. plt.savefig('figure.svg', transparent=False) - SVG does not support transparency
D. plt.savefig('figure.png', transparent=True) - PNG supports transparency

Solution

  1. Step 1: Identify file formats that support transparency

    PNG and SVG support transparency; JPEG and BMP do not support transparent backgrounds properly.
  2. Step 2: Choose correct save command for transparent background

    Use transparent=True with a PNG file to save a transparent image that blends well on websites.
  3. Final Answer:

    plt.savefig('figure.png', transparent=True) - PNG supports transparency -> Option D
  4. Quick Check:

    PNG + transparent=True = best for transparent images [OK]
Hint: Use PNG format with transparent=True for best transparency [OK]
Common Mistakes:
  • Using JPEG or BMP which don't support transparency
  • Setting transparent=False by mistake
  • Assuming SVG does not support transparency