Challenge - 5 Problems
Matplotlib Saving Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
What file is created by this code?
Consider this Python code using matplotlib:
What type of file will be saved?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('chart.svg')What type of file will be saved?
Matplotlib
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.savefig('chart.svg')
Attempts:
2 left
💡 Hint
The file extension determines the format matplotlib saves.
✗ Incorrect
The 'savefig' function saves the plot in the format matching the file extension. Here, '.svg' means a vector graphic SVG file.
❓ Predict Output
intermediate1:30remaining
What happens if you save a plot with dpi=300 to PNG?
Look at this code:
What is the effect of dpi=300?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('high_res.png', dpi=300)What is the effect of dpi=300?
Matplotlib
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.savefig('high_res.png', dpi=300)
Attempts:
2 left
💡 Hint
DPI means dots per inch, affecting image clarity.
✗ Incorrect
Setting dpi=300 increases the pixel density, making the PNG image sharper and suitable for printing.
🔧 Debug
advanced2:00remaining
Why does this PDF save fail?
This code tries to save a plot as PDF but fails:
Why does it fail to save a PDF file?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('plot.pdf', format='png')Why does it fail to save a PDF file?
Matplotlib
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.savefig('plot.pdf', format='png')
Attempts:
2 left
💡 Hint
Check if the format matches the file extension.
✗ Incorrect
The 'format' parameter says save as PNG but the file extension is '.pdf'. This mismatch causes an error or unexpected behavior.
❓ data_output
advanced1:30remaining
How many files are created by this code?
Given this code:
How many files are created after running this code?
import matplotlib.pyplot as plt
for ext in ['png', 'svg', 'pdf']:
plt.plot([1, 2], [3, 4])
plt.savefig(f'file.{ext}')
plt.clf()How many files are created after running this code?
Matplotlib
import matplotlib.pyplot as plt for ext in ['png', 'svg', 'pdf']: plt.plot([1, 2], [3, 4]) plt.savefig(f'file.{ext}') plt.clf()
Attempts:
2 left
💡 Hint
Each loop iteration saves one file with a different extension.
✗ Incorrect
The loop runs 3 times, saving 'file.png', 'file.svg', and 'file.pdf'. So 3 files are created.
🚀 Application
expert2:30remaining
Choose the best code to save a plot as a high-quality PDF with transparent background
You want to save a matplotlib plot as a PDF file named 'output.pdf'. The PDF should have a transparent background and high quality. Which code snippet achieves this?
Attempts:
2 left
💡 Hint
Transparent background requires transparent=True. Higher dpi means better quality.
✗ Incorrect
Setting transparent=True makes the background clear. dpi=300 increases quality. This combination saves a high-quality transparent PDF.