Using plt.savefig over a for loop of iterated plots returns blank image

Viewed 240

Ok, so I apologize if this has been asked before, but I am running into some issues with trying to execute plt.savefig on some plots I want to save to a certain directory on my computer.

I currently have 481 plots that I generated through the following code:

ID=np.array(table['ID'])

My ID array comes from an isolated column from an astropy table which I made into an array to match the format of my other variables.

path=str('/Volumes/Desktop/Folder')
filename=str('/Filename_ID_')
outfilename=path+filename
outfilename
yra = [0, 3]


def PLOTS(M,P,Match,Mean,P25,P50,P75):
    ax=plt.subplot();
    return (plt.figure(),
            ax.plot(M,P,'b',lw=2),
            ax.plot((Match*np.ones(2)), yra, 'red',lw=3, label='match'),
            ax.plot((P25*np.ones(2)), yra, 'b--', label='P25'),
            ax.plot(P50*np.ones(2), yra, 'c--', label='P50',linewidth=4),
            ax.plot(P75*np.ones(2), yra, 'r--', label='P75'),
            ax.plot(Mean*np.ones(2), yra, 'darkgoldenrod',label='mean'),
            ax.set_xlabel('M',fontsize=14),
            ax.set_ylabel('P',fontsize=14),
            ax.legend(fontsize=10,loc='upper right'),
            plt.savefig(outfilename+str(ID)+'.pdf')));

for i in range(len(M)):
    print(PLOTS(M, P[i],Matches[i],Mean[i],P25[i],P50[i],P75[i]))
    plt.savefig(outfilename+str(ID[i])+'.pdf')

This outputs my plots but when I go check my Folder in my desktop the image is blank. I know that if you execute plt.show() before your plt.savefig() then that is the reason why you get a blank image, but I am not executing plt.show() at all. I have tried to add the plt.show() command after the plt.savefig commands on both my function and my for loop but I still come up with a blank image.

I have also checked out other posts similar to mine such as How do I save a new graph as png with every iteration of a loop but this still did not help with my issue.

I really appreciate any help you guys may provide and again apologize if this question has been asked.

0 Answers
Related