Home > Software design >  How to save plot to new Folder
How to save plot to new Folder

Time:01-17

I would like to save my matplotlib plot to the new generated Folder. I just dont get it to save my plot to this path...

This is my Code so far:

now = datetime.now()
current_time = now.strftime("%m_%d_%Y, %H_%M_%S")
dir_name = "Messdaten "   current_time

os.mkdir(dir_name)

my_path = os.path.abspath(os.getcwd())
    
save_path = my_path   "\\"   dir_name

path = Path(save_path)

plt.savefig(path)

I would be grateful for any help!

CodePudding user response:

try use os module:

import os
my_path = os.path.abspath(__file__) # Figures out the absolute path 
fig.savefig(my_path   '/Sub Directory/graph.png')

or:

script_dir = os.path.dirname(__file__)
results_dir = os.path.join(script_dir, 'Results/')
sample_file_name = "sample"

os.makedirs(results_dir, exist_ok=True)
plt.savefig(os.path.join(results_dir, sample_file_name))
  •  Tags:  
  • Related