Python for Data Science: Chapter 6: Data Visualization

Matplotlib: Saving Plots to Files

Python open source drawing library

After creating a graph, we might need to include it in a report or share it with others.

Saving Plots to Files

• After creating a graph, we might need to include it in a report or share it with others. Matplotlib allows us to save any plot like saving an image.

• The function plt.savefig() is used to save the plot.


Syntax

plt.savefig("filename.format")

where

filename: It is the name of which we want to give it to our file.

format: It is the type of image. It can be .png, .jpg, .pdf or .svg file.


Demo example

Step 1:

Create a Python script that uses Matplotlib and draws a simple line plot.

import matplotlib.pyplot as plt

x = [1,2,3,4,5]

y = [10,20,25,30,40]

plt.plot(x,y)

plt.title("Simple Line Plot")

plt.xlabel("X‒axis")

plt.ylabel("Y‒axis")

plt.savefig("my_plot.pdf")

plt.show()

Step 2:

Open my_plot.pdf which gets created at the current working directory. This pdf file stored the plot created by above Python program.


 

Python for Data Science: Chapter 6: Data Visualization : Tag: Computer Programming, Python, Data Science : Python open source drawing library - Matplotlib: Saving Plots to Files


Python for Data Science: Chapter 6: Data Visualization



Under Subject


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science - Laboratory

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation