Challenge - 5 Problems
Master of Saving Figures
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What file is created by this code?
Consider this Python code using matplotlib to save a plot. What is the name and format of the saved file?
Matplotlib
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.savefig('myplot.png')
Attempts:
2 left
💡 Hint
The filename extension in savefig determines the file format.
✗ Incorrect
The plt.savefig() function saves the current figure to the filename given. Here, 'myplot.png' means a PNG image file is created.
❓ Predict Output
intermediate2:00remaining
What happens if you save a figure twice with different names?
What files are created after running this code?
Matplotlib
import matplotlib.pyplot as plt plt.plot([1, 2], [3, 4]) plt.savefig('first.png') plt.savefig('second.png')
Attempts:
2 left
💡 Hint
Each savefig call writes the current figure to the given filename.
✗ Incorrect
Calling savefig twice with different filenames creates two separate files with the same figure content.
🔧 Debug
advanced3:00remaining
Why does this code save a blank image?
This code saves a blank image file. What is the reason?
Matplotlib
import matplotlib.pyplot as plt plt.savefig('blank.png') plt.plot([1, 2, 3], [4, 5, 6]) plt.show()
Attempts:
2 left
💡 Hint
Think about the order of commands and when the figure is saved.
✗ Incorrect
savefig saves the current figure state. Since no plot was drawn before savefig, the image is blank.
❓ data_output
advanced2:00remaining
What is the size of the saved figure in pixels?
Given this code, what is the pixel size of the saved image 'plot.png'?
Matplotlib
import matplotlib.pyplot as plt fig = plt.figure(figsize=(4, 3), dpi=100) plt.plot([0, 1], [0, 1]) fig.savefig('plot.png')
Attempts:
2 left
💡 Hint
Image size in pixels = figsize (in inches) × dpi.
✗ Incorrect
The figure size is 4×3 inches and dpi is 100, so pixels = 4*100 by 3*100 = 400×300.
🚀 Application
expert3:00remaining
How to save a figure with transparent background?
Which code snippet correctly saves a matplotlib figure with a transparent background?
Matplotlib
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6])
Attempts:
2 left
💡 Hint
Check the savefig parameter that controls background transparency.
✗ Incorrect
The 'transparent=True' argument saves the figure with a transparent background.