VoodooChild92 VoodooChild92 - 3 months ago 14
Python Question

Matplotlib.pyplot : Save the plots into a pdf

------ plotting module ------

def plotGraph(X,Y):
fignum = random.randint(0,sys.maxint)
plt.figure(fignum)
### Plotting arrangements ###
return fignum


------ plotting module ------

----- mainModule ----

import matplotlib.pyplot as plt
### tempDLStats, tempDLlabels are the argument
plot1 = plotGraph(tempDLstats, tempDLlabels)
plot2 = plotGraph(tempDLstats_1, tempDLlabels_1)
plot3 = plotGraph(tempDLstats_2, tempDLlabels_2)
plt.show()


The problem is I want to save all the graphs plot1, plot2, plot3 to a single pdf. So, is there any way to achieve it and no, I can't include the plotGraph function in the mainModule.

There's a function named "pylab.savefig" but that seems to work only if its placed along with plotting module. Is there any other way to accomplish it?
Suggest me any changes to my function codes so that I can save the plots into a single pdf file.

Answer

Never mind got the way to do it.

def plotGraph(X,Y):
     fignum = random.randint(0,sys.maxint)
     fig = plt.figure(fignum)
     ### Plotting arrangements ###
     return fig

------ plotting module ------

----- mainModule ----

 import matplotlib.pyplot as plt
 ### tempDLStats, tempDLlabels are the argument
 plot1 = plotGraph(tempDLstats, tempDLlabels)
 plot2 = plotGraph(tempDLstats_1, tempDLlabels_1)
 plot3 = plotGraph(tempDLstats_2, tempDLlabels_2)
 plt.show()
 plot1.savefig('plot1.png')
 plot2.savefig('plot2.png')
 plot3.savefig('plot3.png')

----- mainModule -----