1

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

6
  • Please create a Minimal, Complete, and Verifiable Example. Commented Oct 24, 2017 at 14:28
  • I have edited my question with my script, please check Commented Oct 24, 2017 at 14:30
  • What is candidates? Commented Oct 24, 2017 at 14:31
  • Its a list and stored some data Commented Oct 24, 2017 at 14:34
  • @DavidG, I have again edited full script. please check Commented Oct 24, 2017 at 14:47

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Dear Can you please explain with code? I want to save all graph in separate file with unique name in a directory
@user8825922: I have added 2-lines code sample. It should be enough to understand what to change in your code.
Thanks but that is also tried. I want to save all graphs in a output directory with unique file name. please suggest
You can use protein_name argument as a file name, so it would be: path = "C:/Users/Vishnu/Desktop/Hiral_project_analysis/Output/"+protein_name+".png"
I am not going to write a code for you or find syntax errors in code snippets. I showed you everything you need to solve this issue and I believe you are smart enough to understand what you are doing in your code and why. If it is so you should also understand my suggestions.
|
2

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)

2 Comments

Dear I used this ` path = 'C:/Users/Vishnu/Desktop/Hiral_project_analysis/Output/' plt.savefig(path + s_no + '_' + pdb_id + ".png") ` and getting blank graph (white screen)
@user8825922 What are you plotting? What is s_no and pdb_id? As i've said before, create a Minimal, Complete, and Verifiable Example, otherwise i can't help you...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.