반응형
플롯을 PDF로 저장
플로팅 모듈
def plotGraph(X,Y):
fignum = random.randint(0,sys.maxint)
plt.figure(fignum)
### Plotting arrangements ###
return fignum
메인 모듈
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, plot2, plot3을 하나의 PDF 파일에 저장하고 싶습니다. 그것을 달성하는 방법이 있습니까? plotGraph
메인 모듈에 기능을 포함 할 수 없습니다 .
이름이 지정된 함수가 pylab.savefig
있지만 플로팅 모듈과 함께 배치 된 경우에만 작동하는 것 같습니다. 그것을 달성하는 다른 방법이 있습니까?
누군가가 Google에서 여기에 도착하면 단일 그림을 .pdf로 변환하려고합니다 (제가 찾던 것입니다).
import matplotlib.pyplot as plt
f = plt.figure()
plt.plot(range(10), range(10), "o")
plt.show()
f.savefig("foo.pdf", bbox_inches='tight')
단일 pdf 파일의 여러 플롯의 경우 PdfPages 를 사용할 수 있습니다 .
에서 plotGraph
기능 당신은 그림과 전화보다 반환해야 savefig
그림 개체를.
------ 플로팅 모듈 ------
def plotGraph(X,Y):
fig = plt.figure()
### Plotting arrangements ###
return fig
------ 플로팅 모듈 ------
----- mainModule ----
from matplotlib.backends.backend_pdf import PdfPages
plot1 = plotGraph(tempDLstats, tempDLlabels)
plot2 = plotGraph(tempDLstats_1, tempDLlabels_1)
plot3 = plotGraph(tempDLstats_2, tempDLlabels_2)
pp = PdfPages('foo.pdf')
pp.savefig(plot1)
pp.savefig(plot2)
pp.savefig(plot3)
pp.close()
import datetime
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
# Create the PdfPages object to which we will save the pages:
# The with statement makes sure that the PdfPages object is closed properly at
# the end of the block, even if an Exception occurs.
with PdfPages('multipage_pdf.pdf') as pdf:
plt.figure(figsize=(3, 3))
plt.plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o')
plt.title('Page One')
pdf.savefig() # saves the current figure into a pdf page
plt.close()
plt.rc('text', usetex=True)
plt.figure(figsize=(8, 6))
x = np.arange(0, 5, 0.1)
plt.plot(x, np.sin(x), 'b-')
plt.title('Page Two')
pdf.savefig()
plt.close()
plt.rc('text', usetex=False)
fig = plt.figure(figsize=(4, 5))
plt.plot(x, x*x, 'ko')
plt.title('Page Three')
pdf.savefig(fig) # or you can pass a Figure object to pdf.savefig
plt.close()
# We can also set the file's metadata via the PdfPages object:
d = pdf.infodict()
d['Title'] = 'Multipage PDF Example'
d['Author'] = u'Jouni K. Sepp\xe4nen'
d['Subject'] = 'How to create a multipage pdf file and set its metadata'
d['Keywords'] = 'PdfPages multipage keywords author title subject'
d['CreationDate'] = datetime.datetime(2009, 11, 13)
d['ModDate'] = datetime.datetime.today()
그것을 할 방법이 있어도 상관 없습니다.
def plotGraph(X,Y):
fignum = random.randint(0,sys.maxint)
fig = plt.figure(fignum)
### Plotting arrangements ###
return fig
------ 플로팅 모듈 ------
----- 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 -----
참고URL : https://stackoverflow.com/questions/11328958/save-the-plots-into-a-pdf
반응형
'programing tip' 카테고리의 다른 글
iOS-Xcode 9에서 정의로 이동하기위한 바로 가기? (0) | 2020.11.06 |
---|---|
작업 복사본 잠김 (0) | 2020.11.06 |
Google지도 Places API V3 자동 완성-입력시 첫 번째 옵션 선택 (0) | 2020.11.06 |
System.Security.SecurityException : 원본을 찾을 수 없지만 일부 또는 모든 이벤트 로그를 검색 할 수 없습니다. (0) | 2020.11.06 |
산업 표준에서 #define이 금지되어 있습니까? (0) | 2020.11.06 |