Python 在带有PdfPages的matplotlib中,如何将打印区域设置为仅使用整页的上半部分?
有了下面的代码,我想创建一个两页的pdf,两页都是标准的肖像(8.5英寸宽,11英寸高) 如何将第二页的打印区域设置为仅使用页面的上半部分?我尝试使用注释掉的代码行,但这只是将页面大小减半,而不是将页面大小保留为tact,并将绘图区域减半 谢谢Python 在带有PdfPages的matplotlib中,如何将打印区域设置为仅使用整页的上半部分?,python,matplotlib,pdfpages,Python,Matplotlib,Pdfpages,有了下面的代码,我想创建一个两页的pdf,两页都是标准的肖像(8.5英寸宽,11英寸高) 如何将第二页的打印区域设置为仅使用页面的上半部分?我尝试使用注释掉的代码行,但这只是将页面大小减半,而不是将页面大小保留为tact,并将绘图区域减半 谢谢 import numpy as np import matplotlib matplotlib.use("PDF") import matplotlib.pyplot as plt from matplotlib.backends.
import numpy as np
import matplotlib
matplotlib.use("PDF")
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import seaborn as sns
sns.set()
xs = np.linspace(-np.pi, np.pi, 40)
ys = np.sin(xs)
with PdfPages('multipage_pdf.pdf') as pdf:
plt.figure(figsize=(8.5, 11))
plt.plot(xs, ys, '-')
plt.title('Page One')
pdf.attach_note('Full page')
pdf.savefig()
plt.close()
plt.figure(figsize=(8.5, 11))
# plt.figure(figsize=(8.5, 5.5))
plt.plot(xs, ys, '-')
plt.title('Page Two')
pdf.attach_note('Want top half of page')
pdf.savefig()
plt.close()
经过大量研究,我没有找到最好的解决办法。因此,我绘制了多个图形,并编写了代码,其中第二个图形为空白图形。如果我的回答会减少从别人那里得到答案的机会,我很抱歉
import numpy as np
import matplotlib
matplotlib.use("PDF")
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import seaborn as sns
sns.set()
xs = np.linspace(-np.pi, np.pi, 40)
ys = np.sin(xs)
pp = PdfPages('SaveMultiPDF.pdf')
fig = plt.figure(figsize=(8.5, 11))
ax = fig.add_subplot(111)
ax.plot(xs, ys, '-')
ax.set_title('Page One')
pp.attach_note('Full page')
plt.savefig(pp, format='pdf')
fig.clf()
fig1 = plt.figure(figsize=(8.5, 11))
ax1 = fig1.add_subplot(211)
ax1.plot(xs, ys, '-')
ax1.set_title('Page Two')
pp.attach_note('Want top half of page')
# blank graph
sns.set_style('white')
ax2 = fig1.add_subplot(212)
ax2.plot([], [])
ax2.axis('off')
plt.savefig(pp, format='pdf')
fig1.clf()
pp.close()
在其他人的帮助下,以下是解决方案(非常简单)
所以有这样一种方法。这很有帮助。
import numpy as np
import matplotlib
matplotlib.use("PDF")
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import seaborn as sns
sns.set()
xs = np.linspace(-np.pi, np.pi, 40)
ys = np.sin(xs)
with PdfPages('multipage_pdf.pdf') as pdf:
plt.figure(figsize=(8.5, 11))
plt.plot(xs, ys, '-')
plt.title('Page One')
pdf.attach_note('Full page')
pdf.savefig()
plt.close()
fig = plt.figure(figsize=(8.5, 11))
ax = fig.add_subplot(211)
ax.plot(xs, ys, '-')
ax.set_title('Page Two')
pdf.attach_note('Want top half of page')
pdf.savefig()
plt.close()