0
0
Matplotlibdata~20 mins

Saving figures to files in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Saving Figures
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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')
AA file named 'myplot.png' is created in the current directory.
BA file named 'myplot.pdf' is created in the current directory.
CA file named 'myplot.jpg' is created in the current directory.
DNo file is created because plt.show() is missing.
Attempts:
2 left
💡 Hint
The filename extension in savefig determines the file format.
Predict Output
intermediate
2: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')
AAn error occurs because savefig is called twice.
BOnly 'second.png' is created, overwriting 'first.png'.
CNo files are created because plt.close() is missing.
DBoth 'first.png' and 'second.png' files are created separately.
Attempts:
2 left
💡 Hint
Each savefig call writes the current figure to the given filename.
🔧 Debug
advanced
3: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()
AThe plot is created after savefig, so the saved image is blank.
Bplt.show() clears the figure before saving.
CThe filename extension 'png' is unsupported.
DThe plot command is missing plt.draw() before savefig.
Attempts:
2 left
💡 Hint
Think about the order of commands and when the figure is saved.
data_output
advanced
2: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')
A100 pixels wide by 100 pixels tall
B4 pixels wide by 3 pixels tall
C400 pixels wide by 300 pixels tall
DCannot determine without plt.show()
Attempts:
2 left
💡 Hint
Image size in pixels = figsize (in inches) × dpi.
🚀 Application
expert
3: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])
Aplt.savefig('transparent.png', transparent=False)
Bplt.savefig('transparent.png', transparent=True)
Cplt.savefig('transparent.png', alpha=0.5)
Dplt.savefig('transparent.png', bg='transparent')
Attempts:
2 left
💡 Hint
Check the savefig parameter that controls background transparency.