0
0
Data Analysis Pythondata~20 mins

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

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