user8825922
user8825922

Reputation: 21

How to save multiple graph in directory with unique file name in python?

I am trying to save multiple graph in a directory with unique file name. I have used plt.savefig() but its work only for one graph and also used below

Edited

path = 'C:/Users/Vishnu/Desktop/Hiral_project_analysis/Output/'
plt.savefig(path + s_no + '_' + pdb_id  + ".png")

I have used above code to save the output file in directory with unique name... Now I am able to save the graphs but all graphs are blank (White Screen)...

Please help me

Upvotes: 1

Views: 2755

Answers (2)

DavidG
DavidG

Reputation: 25380

The basic idea of savefig has been described in the other answer. You need to provide the path and the filename to savefig. A simple example:

import numpy as np
import matplotlib.pyplot as plt

def create_and_savefig(fname):
    # create some random data
    x = np.random.randn(10)
    y = np.random.randn(10)

    plt.clf()  # clear the current figure
    plt.plot(x,y)

    path = "C:\Python34\\"
    plt.savefig(path + fname + ".png")

filenames = ["Test1", "Test2", "Test3"]

for fname in filenames:
    create_and_savefig(fname)

Upvotes: 2

running.t
running.t

Reputation: 5709

Accordign to documentation of pyplot.savefig first argument fname is not a path to a directory in which you want to save file, but

A string containing a path to a filename, or a Python file-like object, or possibly some backend-dependent object such as PdfPages.

So you simply can use absolute path to a file as a first argument, eg.

path =  "C:/Users/Vishnu/Desktop/Hiral_project_analysis/Outpit/" + protein_name + ".png"
plt.savefig(path)

Also I could not find in documentation other arguments you passed to savefig method i.e. ext=".png", close=False, verbose=True.

Upvotes: 3

Related Questions