0
0
Matplotlibdata~20 mins

Saving to PNG, SVG, PDF in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Matplotlib Saving Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What file is created by this code?
Consider this Python code using matplotlib:
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')
AAn SVG vector graphic file named 'chart.svg'
BA PNG image file named 'chart.svg'
CA PDF document named 'chart.svg'
DA text file named 'chart.svg'
Attempts:
2 left
💡 Hint
The file extension determines the format matplotlib saves.
Predict Output
intermediate
1:30remaining
What happens if you save a plot with dpi=300 to PNG?
Look at this code:
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)
AThe saved PNG image will be smaller in file size
BThe saved PNG image will have higher resolution and be sharper
CThe saved PNG image will be in vector format
DThe dpi parameter is ignored for PNG files
Attempts:
2 left
💡 Hint
DPI means dots per inch, affecting image clarity.
🔧 Debug
advanced
2:00remaining
Why does this PDF save fail?
This code tries to save a plot as PDF but fails:
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')
AThe format argument conflicts with the file extension, causing a mismatch error
BMatplotlib cannot save PDF files
CThe file name must be 'plot.png' to save as PNG
DThe plot must be shown before saving
Attempts:
2 left
💡 Hint
Check if the format matches the file extension.
data_output
advanced
1:30remaining
How many files are created by this code?
Given 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()
A1 file
B2 files
C3 files
D0 files
Attempts:
2 left
💡 Hint
Each loop iteration saves one file with a different extension.
🚀 Application
expert
2: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?
Aplt.savefig('output.pdf', transparent=True, dpi=72)
Bplt.savefig('output.pdf', transparent=False, dpi=72)
Cplt.savefig('output.pdf', transparent=False, dpi=300)
Dplt.savefig('output.pdf', transparent=True, dpi=300)
Attempts:
2 left
💡 Hint
Transparent background requires transparent=True. Higher dpi means better quality.