|
|
|||||||||||||||||||||||||||||
|
pylab_examples_Examples 37_multipage_pdf. |
H.Kamifuji . |
これは、いくつかのページを含むPDFファイルを作成し、pdfファイルにメタデータと注釈を追加するデモです。
"""
This is a demo of creating a pdf file with several pages,
as well as adding metadata and annotations to pdf files.
"""
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.attach_note("plot of sin(x)") # you can add a pdf note to
# attach metadata to a page
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()
|
|
multipage_pdf.pdf Python 3.11.2 見直しました。上記のコードでは、下記のエラーが発生します。 multipage_pdf.txt matplotlib 内部のエラーのようです。matplotlib の改修(先祖帰りバグの改修)を待つしかない。 Python 3.11.6 (matplotlib 3.7.1) では、下記のようなエラーがあり、1 ページのみの multipage_pdf.pdf ファイルが作成される。 multipage_pdf_3116_371.txt Python 3.12.0 (matplotlib 3.8.1) では、下記のようなエラーがあり、1 ページのみの multipage_pdf.pdf ファイルが作成される。 multipage_pdf_3120_381.txt Python 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) で、見直し中、新しいサンプル(misc-multipage-pdf-py) を見つけ、下記のコードで、実行できたが、結果は、変化なしです。
"""
=============
Multipage PDF
=============
This is a demo of creating a pdf file with several pages,
as well as adding metadata and annotations to pdf files.
If you want to use a multipage pdf file using LaTeX, you need
to use ``from matplotlib.backends.backend_pgf import PdfPages``.
This version however does not support `.attach_note`.
"""
import datetime
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
# 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()
# if LaTeX is not installed or error caught, change to `False`
plt.rcParams['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.attach_note("plot of sin(x)") # attach metadata (as pdf note) to page
pdf.savefig()
plt.close()
plt.rcParams['text.usetex'] = False
fig = plt.figure(figsize=(4, 5))
plt.plot(x, x ** 2, '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'] = '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()
Python 3.11.6 (matplotlib 3.7.1) では、下記のようなエラーがあり、1 ページのみの multipage_pdf.pdf ファイルが作成される。multipage_pdf_2_3116_371.txt Python 3.12.0 (matplotlib 3.8.1) では、下記のようなエラーがあり、1 ページのみの multipage_pdf.pdf ファイルが作成される。 multipage_pdf_2_3120_381.txt 当方の環境には、LaTeX がインストールされていないので、32 行目の plt.rcParams['text.usetex'] = True を plt.rcParams['text.usetex'] = Falsea に書き換えるべきでした。
"""
=============
Multipage PDF
=============
This is a demo of creating a pdf file with several pages,
as well as adding metadata and annotations to pdf files.
If you want to use a multipage pdf file using LaTeX, you need
to use ``from matplotlib.backends.backend_pgf import PdfPages``.
This version however does not support `.attach_note`.
"""
import datetime
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
# 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_3.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()
# if LaTeX is not installed or error caught, change to `False`
plt.rcParams['text.usetex'] = False
plt.figure(figsize=(8, 6))
x = np.arange(0, 5, 0.1)
plt.plot(x, np.sin(x), 'b-')
plt.title('Page Two')
pdf.attach_note("plot of sin(x)") # attach metadata (as pdf note) to page
pdf.savefig()
plt.close()
plt.rcParams['text.usetex'] = False
fig = plt.figure(figsize=(4, 5))
plt.plot(x, x ** 2, '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'] = '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()
Python 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) 共に、正常実行です。multipage_pdf_3.pdf |
|
pylab_examples_Examples code: multipage_pdf.py misc-multipage-pdf-py |
|