[英]How to stop extra page being created in PDF output from matplotlib PdfPages function
I can't figure out why I'm getting an extra page in my PDF file. 我无法弄清楚为什么我的PDF文件中有额外的页面。 I only have 7 files that are loaded but 8 are being created in the PDF output file.
我只加载了7个文件,但是在PDF输出文件中创建了8个文件。 Below is the code I'm working with.
以下是我正在使用的代码。 The files being loaded are actually copies of one file each renamed to a different name to avoid problems I don't think the fact that the actual content is the same should matter right?
正在加载的文件实际上是一个文件的副本,每个文件都重命名为一个不同的名称以避免问题我不认为实际内容是否相同这一事实是否正确?
Edit: I have been able to verify that it's the last file that's loaded and added to the PDF file that gets created twice but I still don't see why.. 编辑:我已经能够验证它是最后一个加载并添加到PDF文件的文件,它被创建了两次,但我仍然不明白为什么..
def processFiles():
##Set some vars
global kdeData
counter = 0
sColumn = selectCol()
sSamples = setSamples()
rfName = raw_input("Name of file to save results to: ")+".pdf"
createPDF = PdfPages(rfName)
##Iterate for each file
for file in fileList:
valid = [sColumn]
matrix = np.loadtxt(file, skiprows=1, usecols=valid)
colCount = np.loadtxt(file, dtype=object)
totalCols = colCount.shape[1]
ldlValid = [i for i in range(totalCols)]
lDL = np.loadtxt(file, usecols=ldlValid, dtype=object)
kdeData = np.array(matrix)
gkde = stats.gaussian_kde(kdeData)
ind = np.linspace(-int(getRange()), int(getRange()), len(kdeData) * sSamples)
kdepdf = gkde.evaluate(ind)
plt.figure()
##plot histogram of sample
plt.hist(kdeData, len(kdeData), normed=1, alpha=0.20)
##plot data generating density
plt.plot(ind, stats.norm.pdf(ind), 'r', linewidth=0.8, label='DGP normal')
##plot estimated density
plt.plot(ind, kdepdf, 'g', linewidth=0.8, label='kde')
plt.title('KDE for '+str(nameList[counter]))
plt.legend()
plt.savefig(createPDF, format='pdf')
counter += 1
##Save PDF and open it
createPDF.savefig()
createPDF.close()
os.startfile(rfName)
这是额外的createPDF.savefig()
从底部向上两行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.