简体   繁体   English

在带有 PdfPages 的 matplotlib 中,如何将绘图区域设置为仅使用整页的上半部分?

[英]In matplotlib with PdfPages, how do I set the plotting area to only use the top half of a full page?

With the code below, I'd like to create a two page pdf, with both pages in standard portrait (8.5 inches wide and 11 inches tall).使用下面的代码,我想创建一个两页 pdf,两页都是标准纵向(8.5 英寸宽和 11 英寸高)。

How can I set the plot area of the second page to only use the top half of the page?如何将第二页的 plot 区域设置为仅使用页面的上半部分? I tried using the commented out line of code but that just cut the page size in half rather than leaving the page size in tact and halving the plot area.我尝试使用注释掉的代码行,但这只是将页面大小减半,而不是将页面大小保持不变并将 plot 区域减半。

Thanks!谢谢!

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()

With the help from others, here's the solution (pretty straightforward).在其他人的帮助下,这是解决方案(非常简单)。

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()

After much research, I did not find the best solution.经过大量研究,我没有找到最佳解决方案。 So I drew multiple graphs and wrote the code with the second one as a blank graph.所以我画了多张图,把第二张的代码写成空白图。 I'm sorry if my answer will reduce the chances of getting answers from others.如果我的回答会减少从其他人那里得到答案的机会,我很抱歉。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM