Online Learning Platform

Data Analysis Using Python > Data Visualization > How to save a chart into my Working directory?

How to save a chart into my Working directory?

Let my target folder is "/Users/mdfazlulkarimpatwary/Documents/Lectures/Data Analysis Python”

To store my folder path in a variable:

new_directory = "/Users/mdfazlulkarimpatwary/Documents/Lectures/Data Analysis Python"

To change current folder location to new location we need to use “os” module.

import os

print(os.getcwd())

Now changing Python’s default location to my new location:

try:

  os.chdir(new_directory)

  print(f"Working directory changed to: {os.getcwd()}")

except FileNotFoundError:

  print(f"Directory not found: {new_directory}")

The following commands is to save ouput as png or jpg or any other format as your wish.

filename = "cumulative_runs_chart.png"

plt.savefig(filename, transparent=True)

print(f"Chart saved to: {os.getcwd()}/{filename}")

 

Prev
Line Chart

No More

Feedback
ABOUT

Statlearner


Statlearner STUDY

Statlearner