Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Saving figures in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Figure Saving Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output file format?

Consider this Python code that saves a plot:

import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('myplot.png')
print('File saved')

What is the format of the saved file?

Data Analysis Python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('myplot.png')
print('File saved')
APNG image file
BJPEG image file
CSVG vector graphic
DPDF document
Attempts:
2 left
💡 Hint

Look at the file extension in the filename.

❓ data_output
intermediate
2:00remaining
How many files are saved?

This code saves multiple figures:

import matplotlib.pyplot as plt
for i in range(3):
    plt.figure()
    plt.plot([1, 2], [i, i+1])
    plt.savefig(f'plot_{i}.png')

How many image files are created after running this code?

Data Analysis Python
import matplotlib.pyplot as plt
for i in range(3):
    plt.figure()
    plt.plot([1, 2], [i, i+1])
    plt.savefig(f'plot_{i}.png')
A0 files
B1 file
C3 files
D2 files
Attempts:
2 left
💡 Hint

Count how many times savefig is called.

🔧 Debug
advanced
2:00remaining
Why is the saved figure blank?

Look at this code:

import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('figure.png')
plt.show()

After running it, the saved file 'figure.png' is blank. Why?

Data Analysis Python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('figure.png')
plt.show()
AThe savefig filename is invalid
BThe figure is saved after showing, so it is blank
CThe plot command is missing
DThe figure is saved before showing, so it is blank
Attempts:
2 left
💡 Hint

Think about when the figure content is finalized.

❓ visualization
advanced
2:00remaining
Which option saves a figure with transparent background?

You want to save a plot with a transparent background. Which code does this?

Data Analysis Python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
Aplt.savefig('plot.png', transparent=True)
Bplt.savefig('plot.png', bg='transparent')
Cplt.savefig('plot.png', alpha=0.5)
Dplt.savefig('plot.png', transparent=False)
Attempts:
2 left
💡 Hint

Check the savefig parameters for transparency.

🚀 Application
expert
3:00remaining
How to save a high-resolution figure?

You want to save a figure with higher quality for printing. Which option correctly sets the resolution to 300 dots per inch (DPI)?

Data Analysis Python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
Aplt.savefig('highres.png', dpi='300dpi')
Bplt.savefig('highres.png', resolution=300)
Cplt.savefig('highres.png', quality=300)
Dplt.savefig('highres.png', dpi=300)
Attempts:
2 left
💡 Hint

Look for the correct parameter name and type for DPI.

Practice

(1/5)
1. What is the main function used in Python's matplotlib to save a plot as an image file?
easy
A. plt.show()
B. plt.savefig()
C. plt.plot()
D. plt.save()

Solution

  1. Step 1: Identify the function for saving plots

    The function plt.savefig() is designed to save the current figure to a file.
  2. Step 2: Differentiate from other functions

    plt.show() displays the plot, plt.plot() creates the plot, and plt.save() does not exist.
  3. Final Answer:

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

    Save plot = plt.savefig() [OK]
Hint: Remember: save figure uses 'savefig' word [OK]
Common Mistakes:
  • Confusing plt.show() with saving
  • Using plt.save() which is not a valid function
  • Thinking plt.plot() saves the figure
2. Which of the following is the correct syntax to save a plot as a PNG file with matplotlib?
easy
A. plt.savefig('plot.png')
B. plt.save('plot.png')
C. plt.savefig(plot.png)
D. plt.savefig('plot')

Solution

  1. Step 1: Check the function name and argument type

    The correct function is plt.savefig() and it requires the filename as a string.
  2. Step 2: Verify the filename format

    The filename must be a string with the extension, e.g., 'plot.png'. plt.savefig('plot.png') matches this exactly.
  3. Final Answer:

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

    Save with filename string = plt.savefig('plot.png') [OK]
Hint: Use quotes around filename with extension [OK]
Common Mistakes:
  • Omitting quotes around filename
  • Using plt.save() instead of plt.savefig()
  • Missing file extension in filename
3. What will be the effect of this code snippet?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('myplot.pdf', dpi=300, bbox_inches='tight')
medium
A. Saves a PNG file named 'myplot.pdf' ignoring dpi and bbox_inches
B. Displays the plot on screen with 300 dpi resolution
C. Raises an error because dpi cannot be set for PDF files
D. Saves a PDF file named 'myplot.pdf' with high resolution and tight layout

Solution

  1. Step 1: Understand plt.savefig parameters

    The code saves the plot as 'myplot.pdf' with dpi=300 for high resolution and bbox_inches='tight' to reduce whitespace.
  2. Step 2: Confirm file format and parameters compatibility

    PDF supports dpi and bbox_inches parameters, so no error occurs and the file is saved correctly.
  3. Final Answer:

    Saves a PDF file named 'myplot.pdf' with high resolution and tight layout -> Option D
  4. Quick Check:

    Save PDF with dpi and tight bbox = correct [OK]
Hint: File extension sets format; dpi and bbox_inches improve quality [OK]
Common Mistakes:
  • Thinking dpi is ignored for PDF
  • Confusing save with display (plt.show())
  • Assuming dpi causes error for PDF
4. Identify the error in this code that tries to save a figure:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig(myplot.png)
medium
A. File extension '.png' is invalid
B. plt.plot() must be called after plt.savefig()
C. Missing quotes around filename 'myplot.png'
D. dpi parameter is required in plt.savefig()

Solution

  1. Step 1: Check the argument to plt.savefig()

    The filename must be a string, so it needs quotes around 'myplot.png'.
  2. Step 2: Verify other parts of the code

    Calling plt.plot() before plt.savefig() is correct, dpi is optional, and '.png' is a valid extension.
  3. Final Answer:

    Missing quotes around filename 'myplot.png' -> Option C
  4. Quick Check:

    Filename must be string with quotes [OK]
Hint: Always put filename in quotes when saving [OK]
Common Mistakes:
  • Forgetting quotes around filename
  • Thinking dpi is mandatory
  • Misordering plot and savefig calls
5. You want to save a plot as a high-quality PNG file with minimal white space around it. Which of the following code snippets achieves this correctly?
hard
A.
plt.savefig('figure.png', dpi=600, bbox_inches='tight')
B.
plt.savefig('figure.png', dpi=72, bbox_inches='standard')
C.
plt.savefig('figure.png', dpi=600, bbox_inches='loose')
D.
plt.savefig('figure.png', dpi='high', bbox_inches='tight')

Solution

  1. Step 1: Understand dpi and bbox_inches parameters

    High dpi (e.g., 600) means better image quality. bbox_inches='tight' reduces extra white space.
  2. Step 2: Evaluate each option

    plt.savefig('figure.png', dpi=600, bbox_inches='tight')
    uses dpi=600 and bbox_inches='tight' correctly.
    plt.savefig('figure.png', dpi=72, bbox_inches='standard')
    uses low dpi and invalid bbox_inches.
    plt.savefig('figure.png', dpi=600, bbox_inches='loose')
    uses bbox_inches='loose' which adds space.
    plt.savefig('figure.png', dpi='high', bbox_inches='tight')
    uses invalid dpi value 'high'.
  3. Final Answer:

    plt.savefig('figure.png', dpi=600, bbox_inches='tight') -> Option A
  4. Quick Check:

    High dpi + tight bbox = best quality [OK]
Hint: Use dpi=600 and bbox_inches='tight' for quality and layout [OK]
Common Mistakes:
  • Using invalid dpi values like strings
  • Choosing bbox_inches='loose' which adds whitespace
  • Ignoring dpi leading to low-quality images
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