Question:

How can you save a matplotlib figure to a file?

Show Hint

The function is savefig, called as plt.savefig("file.png").
Updated On: Oct 1, 2026
  • plt.savefile()
  • plt.savefig(filename)
  • plt.storefig(filename)
  • plt.save(filename)
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Step 1: Understanding the Concept:
Matplotlib has one function to write the current figure to disk. The function is savefig(). For example, plt.savefig("chart.png") saves the figure as a PNG file.

Step 2: Check option 1.
plt.savefile() is not a Matplotlib function. It would raise an AttributeError. So option 1 is wrong.

Step 3: Check option 2.
plt.savefig(filename) is the correct function. It takes the file name, and the extension decides the format. So option 2 is correct.

Step 4: Check option 3.
plt.storefig() does not exist in Matplotlib. So option 3 is wrong.

Step 5: Check option 4.
plt.save() does not exist in Matplotlib. So option 4 is wrong.

Step 6: Final Answer:
The correct way to save a figure is plt.savefig(filename), which is option 2. \[ \boxed{\text{plt.savefig(filename)}} \]
Was this answer helpful?
0
0