Bird
Raised Fist0
Matplotlibdata~20 mins

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

Choose your learning style10 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.

Practice

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

Solution

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

    This function is used to save the current plot or figure to a file, such as PNG or PDF.
  2. Step 2: Differentiate from display functions

    Functions like plt.show() display the figure, but plt.savefig() writes it to disk.
  3. Final Answer:

    It saves the current figure to a file on your computer. -> Option A
  4. Quick Check:

    plt.savefig() saves figure [OK]
Hint: Remember: savefig saves, show displays [OK]
Common Mistakes:
  • Confusing plt.savefig() with plt.show()
  • Thinking savefig displays the plot
  • Assuming savefig clears the figure
2. Which of the following is the correct syntax to save a figure as a PNG file named 'plot.png'?
easy
A. plt.save('plot.png')
B. plt.savefig('plot.png')
C. plt.savefig(plot.png)
D. plt.save_figure('plot.png')

Solution

  1. Step 1: Recall the correct function name

    The function to save figures is plt.savefig(), not plt.save() or others.
  2. Step 2: Check the argument format

    The filename must be a string, so it should be in quotes: 'plot.png'.
  3. Final Answer:

    plt.savefig('plot.png') -> Option B
  4. Quick Check:

    Correct function and string filename [OK]
Hint: Use plt.savefig('filename.ext') with quotes [OK]
Common Mistakes:
  • Missing quotes around filename
  • Using incorrect function name
  • Passing filename without quotes
3. What will happen if you run this code?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('myplot.pdf', dpi=300)
plt.close()
medium
A. The plot is shown on screen but not saved.
B. An error occurs because dpi is not allowed with PDF files.
C. A PDF file named 'myplot.pdf' is saved with high resolution.
D. The file 'myplot.pdf' is saved but with default dpi 72.

Solution

  1. Step 1: Understand plt.savefig() with dpi parameter

    Setting dpi=300 increases the resolution of the saved image, including PDF files.
  2. Step 2: Confirm file saving and closing

    The code saves the plot as 'myplot.pdf' with dpi 300, then closes the figure to free memory.
  3. Final Answer:

    A PDF file named 'myplot.pdf' is saved with high resolution. -> Option C
  4. Quick Check:

    savefig with dpi=300 saves high-res PDF [OK]
Hint: dpi controls resolution; works with PDF too [OK]
Common Mistakes:
  • Thinking dpi is ignored for PDF
  • Expecting plot to display automatically
  • Confusing plt.close() with plt.show()
4. Identify the error in this code snippet that tries to save a figure:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig(myplot.png)
plt.show()
medium
A. The file extension '.png' is invalid.
B. plt.show() must come before plt.savefig().
C. plt.plot() is missing a figure creation step.
D. The filename is not enclosed in quotes.

Solution

  1. Step 1: Check the filename argument in plt.savefig()

    The filename must be a string, so it needs quotes: 'myplot.png'.
  2. Step 2: Verify order of plt.show() and plt.savefig()

    Calling plt.savefig() before plt.show() is correct and common practice.
  3. Final Answer:

    The filename is not enclosed in quotes. -> Option D
  4. Quick Check:

    Filename must be a string in quotes [OK]
Hint: Always put filename in quotes in savefig() [OK]
Common Mistakes:
  • Omitting quotes around filename
  • Thinking plt.show() must come first
  • Assuming file extension is wrong
5. You want to save a plot tightly cropped around the content to avoid extra white space. Which plt.savefig() parameter helps achieve this?
hard
A. bbox_inches='tight'
B. format='png'
C. transparent=True
D. dpi=300

Solution

  1. Step 1: Understand the role of bbox_inches

    The parameter bbox_inches='tight' trims the saved figure to fit the content closely, removing extra margins.
  2. Step 2: Differentiate from other parameters

    dpi controls resolution, transparent controls background, and format sets file type, none affect cropping.
  3. Final Answer:

    bbox_inches='tight' -> Option A
  4. Quick Check:

    Use bbox_inches='tight' to crop figure [OK]
Hint: Use bbox_inches='tight' to crop saved figure [OK]
Common Mistakes:
  • Confusing dpi with cropping
  • Using transparent instead of bbox_inches
  • Setting format does not crop figure
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard