Skip to content
Exercises · Q3

Q.Name the function which is used to save the plot.

Rajasthan RbseTextbookSubjective· 2mImportance★★★★★
55% · 24/44 Questions
✓ Free question

The savefig() function (from matplotlib.pyplot) is used to save a plot to a file.

The Idea: Saving a Plot

When you create a plot using Matplotlib, it exists in memory as a figure object. To preserve it for later use — in a report, presentation, or assignment — you need to write it to a file on disk. That's exactly what savefig() does.

The function is a method of the pyplot module (commonly imported as plt), so you call it as plt.savefig(filename). It takes the plot that is currently active in Matplotlib's state machine and exports it to the specified file format.

Why savefig() and not something else?

You might wonder: why not just take a screenshot? Because savefig() gives you:

  • Vector quality — formats like PDF and SVG remain crisp at any zoom level
  • Exact dimensions — you control the figure size, DPI, and aspect ratio
  • No pixelation — raster formats (PNG, JPG) are rendered at the resolution you specify
  • Automation — you can save hundreds of plots in a loop without manual intervention

The Syntax

import matplotlib.pyplot as plt

# Create a simple plot
plt.plot([1, 2, 3], [4, 5, 6])
plt.title("My Plot")

# Save it
plt.savefig('my_plot.png')   # Saves as PNG
plt.savefig('my_plot.pdf')   # Saves as PDF (vector)
plt.savefig('my_plot.svg')   # Saves as SVG (vector)
Tip

Always call savefig() before plt.show(). If you call show() first, the figure is cleared and savefig() will save a blank image. This is a very common mistake in exams and practicals.

Common Parameters

ParameterPurposeExample
dpiResolution in dots per inchdpi=300 for print quality
bbox_inchesCrop whitespace around the plotbbox_inches='tight'
facecolorBackground colour of the saved imagefacecolor='white'
transparentMake background transparenttransparent=True

The Classic Pitfall

Watch out

If you run plt.show() and then plt.savefig(), you'll get an empty file. The show() function clears the current figure. Always save before showing, or use plt.gcf().savefig() to explicitly grab the current figure after showing.

Answer

✓Final answer

The function used to save a plot is savefig() from the matplotlib.pyplot module.

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.