将 2 个图保存为一个包含 2 页的 PDF? [英] Saving 2 plots into one PDF with 2 pages?
问题描述
假设我从jupyter笔记本中输出了2个matplotlib图(例如,这些图完全相同).
将matplotlib.pyplot导入为plt将numpy导入为np#绘图数据t = np.arange(0.0,2.0,0.01)s = 1 + np.sin(2 * np.pi * t)# 注意下面使用plt.subplots等价于使用# fig = plt.figure 然后 ax = fig.add_subplot(111)无花果,ax = plt.subplots()ax.plot(t,s)ax.set(xlabel='time (s)', ylabel='电压 (mV)',title='关于尽可能简单,伙计们')ax.grid()fig.savefig("test.png")plt.show()
如何将这两个图表保存到一个 PDF 中,每个图表都有自己的页面,而不是将它们合并到一个页面上?
您可以使用以下方式:
我获取了您的示例代码,并将其略微调整为适用于matplotlib中的
针对osx用户的注释:由于出现以下行,我对matplotlib中的示例有疑问:
plt.rc('text',usetex = True)
这导致了 LaTex 错误.
RuntimeError:LaTeX无法处理以下字符串:b'lp'这是LaTeX生成的完整报告:
只需设置
usetex=False
作为第一个解决方法.
Let's say I have from output 2 matplotlib plots in my jupyter notebook (for example purposes these charts are the exact same).
import matplotlib.pyplot as plt
import numpy as np
# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
# Note that using plt.subplots below is equivalent to using
# fig = plt.figure and then ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
ax.grid()
fig.savefig("test.png")
plt.show()
How can I save these 2 charts into a single PDF with each chart having its own page, instead of having them combined onto a single page?
You could use something like this:
I took your sample code and slightly adapted it to the multipage example from matplotlib.
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import numpy as np
with PdfPages('multipage_pdf.pdf') as pdf:
# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
# Note that using plt.subplots below is equivalent to using
# fig = plt.figure and then ax = fig.add_subplot(111)
#fig, ax = plt.subplots()
plt.plot(t, s)
#ax.set(xlabel='time (s)', ylabel='voltage (mV)', title='About as simple as it gets, folks')
#ax.grid()
plt.title("Page one")
pdf.savefig()
plt.close()
plt.rc('text', usetex=False)
plt.title("PAGE TWO")
# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
# Note that using plt.subplots below is equivalent to using
# fig = plt.figure and then ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
ax.grid()
pdf.savefig()
plt.close()
This results in a nice two page pdf on my mac unsing python 3. This is not intended to be the final solution but should be a good starting point for you oto go on. You can also add pdf metadata, insert text and much more.
remark for osx users: I had problems with the example from matplotlib due to the line:
plt.rc('text', usetex=True)
This resulted in a LaTex error.
RuntimeError: LaTeX was not able to process the following string:
b'lp'
Here is the full report generated by LaTeX:
Just set
usetex=False
as a first workaround to get going.
这篇关于将 2 个图保存为一个包含 2 页的 PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!