Bird
Raised Fist0
Matplotlibdata~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the plt.savefig() function do in matplotlib?
easy
A. It displays the plot on the screen.
B. It saves the current plot to a file in a specified format.
C. It clears the current plot.
D. It creates a new figure for plotting.

Solution

  1. Step 1: Understand the purpose of plt.savefig()

    This function is used to save the current figure to a file on your computer.
  2. Step 2: Differentiate from other functions

    Functions like plt.show() display the plot, but do not save it. plt.savefig() specifically saves the plot as an image file.
  3. Final Answer:

    It saves the current plot to a file in a specified format. -> Option B
  4. Quick Check:

    Save plot = plt.savefig() [OK]
Hint: Remember: savefig saves, show displays [OK]
Common Mistakes:
  • Confusing plt.savefig() with plt.show()
  • Thinking savefig displays the plot
  • Using savefig after plt.show() causing empty files
2. Which of the following is the correct syntax to save a plot as a PDF file named 'chart.pdf'?
easy
A. plt.savefig('chart.pdf')
B. plt.save('chart.pdf')
C. plt.savefig('chart.pdf', format='png')
D. plt.savefig(chart.pdf)

Solution

  1. Step 1: Identify the correct function name

    The correct function to save a plot is plt.savefig().
  2. Step 2: Use correct string syntax for filename

    The filename must be a string, so it should be enclosed in quotes: 'chart.pdf'.
  3. Final Answer:

    plt.savefig('chart.pdf') -> Option A
  4. Quick Check:

    Correct function and string filename = plt.savefig('chart.pdf') [OK]
Hint: Use plt.savefig('filename.ext') with quotes [OK]
Common Mistakes:
  • Omitting quotes around filename
  • Using plt.save() instead of plt.savefig()
  • Passing filename without quotes causing syntax error
3. What will be the output file format if you run this code?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('output_image.svg')
plt.close()
medium
A. SVG vector graphic file
B. PNG image file
C. PDF document file
D. JPEG image file

Solution

  1. Step 1: Check the filename extension in savefig

    The filename is 'output_image.svg', which ends with '.svg'.
  2. Step 2: Understand file format selection by extension

    Matplotlib saves the plot in the format matching the file extension. '.svg' means it saves as an SVG vector graphic.
  3. Final Answer:

    SVG vector graphic file -> Option A
  4. Quick Check:

    File extension '.svg' = SVG format [OK]
Hint: File extension decides format: .svg means SVG file [OK]
Common Mistakes:
  • Assuming default PNG without checking extension
  • Confusing SVG with PDF format
  • Not saving before closing causing empty files
4. Identify the error in this code snippet that tries to save a plot as a PNG file:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 1])
plt.show()
plt.savefig('plot.png')
medium
A. plt.savefig() requires an additional argument for format.
B. The filename extension '.png' is incorrect for saving images.
C. plt.plot() is missing required arguments.
D. The plot is saved after plt.show(), which may save a blank image.

Solution

  1. Step 1: Understand the order of plt.show() and plt.savefig()

    Calling plt.show() displays and clears the figure by default.
  2. Step 2: Identify consequence of saving after show()

    Saving after plt.show() often results in an empty or blank image file.
  3. Final Answer:

    The plot is saved after plt.show(), which may save a blank image. -> Option D
  4. Quick Check:

    Save before show to avoid blank files [OK]
Hint: Always savefig before show() to keep the plot [OK]
Common Mistakes:
  • Saving after plt.show() causing empty files
  • Thinking filename extension needs special format argument
  • Assuming plt.plot() needs more arguments
5. You want to save the same plot in three formats: PNG, SVG, and PDF. Which code snippet correctly saves the plot in all three formats?
hard
A. plt.plot([1,2,3],[3,2,1]) plt.savefig('plot.png') plt.savefig('plot.jpg') plt.savefig('plot.pdf')
B. plt.plot([1,2,3],[3,2,1]) plt.savefig('plot') plt.savefig('plot') plt.savefig('plot')
C. plt.plot([1,2,3],[3,2,1]) plt.savefig('plot.png', format='png') plt.savefig('plot.svg', format='svg') plt.savefig('plot.pdf', format='pdf')
D. plt.plot([1,2,3],[3,2,1]) plt.save('plot.png') plt.save('plot.svg') plt.save('plot.pdf')

Solution

  1. Step 1: Check function names and parameters

    The correct function is plt.savefig(). plt.plot([1,2,3],[3,2,1]) plt.save('plot.png') plt.save('plot.svg') plt.save('plot.pdf') uses plt.save(), which is invalid.
  2. Step 2: Confirm saving with explicit format or extension

    plt.plot([1,2,3],[3,2,1]) plt.savefig('plot') plt.savefig('plot') plt.savefig('plot') uses filenames without extensions, so format is unclear. plt.plot([1,2,3],[3,2,1]) plt.savefig('plot.png') plt.savefig('plot.svg') plt.savefig('plot.pdf') relies on extensions only, which works but may be less explicit.
  3. Step 3: Understand explicit format argument

    plt.plot([1,2,3],[3,2,1]) plt.savefig('plot.png', format='png') plt.savefig('plot.svg', format='svg') plt.savefig('plot.pdf', format='pdf') uses both filename and explicit format argument, ensuring correct file type saving.
  4. Final Answer:

    Saves the plot in PNG, SVG, and PDF formats using explicit format arguments. -> Option C
  5. Quick Check:

    Use plt.savefig(filename, format='ext') for clarity [OK]
Hint: Use plt.savefig with filename and format='ext' for multiple saves [OK]
Common Mistakes:
  • Using plt.save() instead of plt.savefig()
  • Saving without file extensions causing format errors
  • Not specifying format when filename lacks extension