Bird
0
0

Why does this code not produce a transparent background in the saved image?

medium📝 Debug Q7 of 15
Matplotlib - Export and Publication Quality
Why does this code not produce a transparent background in the saved image?
import matplotlib.pyplot as plt
fig, ax = plt.subplots(facecolor='white')
ax.plot([1, 2, 3])
fig.savefig('output.png', transparent=True)
AThe axes background color overrides the transparent parameter.
BThe figure facecolor parameter is ignored when transparent=True is set.
CThe transparent parameter only works with plt.plot(), not fig.savefig().
DThe transparent parameter must be set to False to work.
Step-by-Step Solution
Solution:
  1. Step 1: Understand figure and axes backgrounds

    The default axes background color is white, which remains opaque even with transparent=True. Setting facecolor='white' on plt.subplots() sets the figure background color, which is made transparent by transparent=True.
  2. Step 2: Effect of axes background on transparency

    Even if transparent=True is set, the axes background remains white, so the saved image is not fully transparent.
  3. Final Answer:

    The axes background color overrides the transparent parameter. -> Option A
  4. Quick Check:

    Axes background color can block transparency [OK]
Quick Trick: Set axes facecolor to 'none' for full transparency [OK]
Common Mistakes:
  • Assuming transparent=True affects axes background
  • Thinking transparent only works with plt.plot()
  • Setting transparent=False to fix transparency

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes