0
0
Matplotlibdata~10 mins

Saving to PNG, SVG, PDF in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to save the plot as a PNG file named 'plot.png'.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.[1]('plot.png')
Drag options to blanks, or click blank then click option'
Ashow
Bsavefig
Cplot
Dfigure
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.show() instead of plt.savefig()
Trying to save without calling savefig
Using plt.plot() to save the file
2fill in blank
medium

Complete the code to save the plot as an SVG file named 'image.svg'.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([10, 20, 30], [1, 4, 9])
plt.[1]('image.svg')
Drag options to blanks, or click blank then click option'
Asavefig
Bdraw
Cshow
Dfigure
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.show() instead of plt.savefig()
Not specifying the file extension
Using plt.draw() which only updates the plot
3fill in blank
hard

Fix the error in the code to save the plot as a PDF file named 'chart.pdf'.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([5, 6, 7], [8, 9, 10])
plt.[1]('chart.pdf')
Drag options to blanks, or click blank then click option'
Asave
Bsave_plot
Csavefig
Dsave_image
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.save() which does not exist
Using plt.savefig() but misspelling it
Trying to save without calling any function
4fill in blank
hard

Fill both blanks to save the plot as a PNG file with 300 dpi resolution.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 3, 5], [2, 4, 6])
plt.[1]('high_res.png', dpi=[2])
Drag options to blanks, or click blank then click option'
Asavefig
Bshow
C300
D100
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.show() instead of plt.savefig()
Setting dpi to a low value like 100
Not specifying dpi at all
5fill in blank
hard

Fill all three blanks to save the plot as a PDF file with transparent background and tight bounding box.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([2, 4, 6], [1, 3, 5])
plt.[1]('output.pdf', transparent=[2], bbox_inches=[3])
Drag options to blanks, or click blank then click option'
Asavefig
BTrue
C'tight'
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.show() instead of plt.savefig()
Setting transparent to False
Not using bbox_inches='tight'