[英]Save multiple figures in one pdf page, matplotlib
我正在嘗試僅在一個pdf頁面中獲取數據,但是我不知道該怎么做。 我發現可以使用“ matplotlib.backends.backend_pdf”將多個圖形保存在pdf文件中,但是它僅對一頁無效。 有任何想法嗎? 將數字轉換成一個數字?
您可以使用matplotlib gridspec在1個窗口中包含多個圖
http://matplotlib.org/users/gridspec.html
from matplotlib.gridspec import GridSpec
import random
import numpy
from matplotlib import pyplot as pl
fig = pl.figure(figsize=(12, 16))
G = GridSpec(2,2)
axes_1 = pl.subplot(G[0, :])
x = [random.gauss(3,1) for _ in range(400)]
bins = numpy.linspace(-10, 10, 100)
axes_1.hist(x, bins, alpha=0.5, label='x')
axes_2 = pl.subplot(G[1, :])
axes_2.plot(x)
pl.tight_layout()
pl.show()
您可以更改行和列的值,並可以細分各個部分。
這是matplotlib提供的解決方案:
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
with PdfPages('foo.pdf') as pdf:
#As many times as you like, create a figure fig and save it:
fig = plt.figure()
pdf.savefig(fig)
....
fig = plt.figure()
pdf.savefig(fig)
瞧
在此處找到完整的示例: 多頁pdf matplotlib
順便說一句,對於一個數字,您不需要matplotlib.backends.backend_pdf
只需添加pdf擴展名,如下所示:
plt.savefig("foo.pdf")
PDF后端每個圖一頁。 使用子圖將多個圖繪制成一個圖形,它們都將一起顯示在PDF的一頁上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.