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?
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.savefig('myplot.png') print('File saved')
Look at the file extension in the filename.
The file extension '.png' tells matplotlib to save the figure as a PNG image file.
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?
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')
Count how many times savefig is called.
The loop runs 3 times, saving one file each time, so 3 files are created.
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?
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.savefig('figure.png') plt.show()
Think about when the figure content is finalized.
Calling plt.show() clears the figure. Saving before showing saves the correct image. But if the figure is blank, it means the figure was saved after it was cleared by show.
You want to save a plot with a transparent background. Which code does this?
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6])
Check the savefig parameters for transparency.
The transparent=True argument saves the figure background as transparent.
You want to save a figure with higher quality for printing. Which option correctly sets the resolution to 300 dots per inch (DPI)?
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6])
Look for the correct parameter name and type for DPI.
The dpi parameter sets the resolution as an integer. Other parameters like resolution or quality are invalid.