2017-11-17 13 views
0

私のジュピターノートに出力2のmatplotlibプロットがあるとしましょう(例えば、これらのチャートはまったく同じです)。私が代わりにそれらを1つのページ上に組み合わせた独自のページを持つ各チャートで単一のPDF、にこれらの2つのチャートを保存することができますどのように2ページの1つのPDFに2つのプロットを保存しますか?

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

Image1

Image2

?あなたはこのようなものを使用でき

+0

は[multipage_pdf.pyサンプルコードをpylab_examples] ... ;-)理由もなく、数年後にテ・コードの例を破るように見えます.html) – sascha

+0

私の答えは、あなたがコメントしたhakvin、saschaと同じものに基づいています。彼らには本当に素晴らしい例があります。 – klaas

答えて

1

を私はあなたのサンプルコードを取り、少しこれは私のmac unsingパイソン3上の素敵な2ページのPDFファイルになり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() 

にそれを適応しました。 これは最終的な解決策ではありませんが、あなたには良い出発点になるはずです。 また、pdfメタデータを追加したり、テキストを挿入したりできます。

multipage pdf sample

OSXユーザー向けの発言:私が原因の行にmatplotlibのからの例の問題を持っていた:

plt.rc('text', usetex=True) 

これは、ラテックスエラーが発生しました。

RuntimeError: LaTeX was not able to process the following string: 
b'lp' 
Here is the full report generated by LaTeX: 

ちょうど軌道に乗るための最初の回避策として、

usetex=False 

を設定します。

0

@サシャ。あなたが参照したコード。 (https://matplotlib.org/examples/pylab_examples/multipage_pdf:ページ紹介は時々

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

""" 
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() 
+0

素晴らしい!本当にありがとう。 PDFに動的な名前を付ける機能があるかどうか知っていますか?ファイルの名前を今日の日付にしたいのと同様に、どうすればできますか? –

+0

コード行 'pdfPages( 'multipage_pdf.pdf')をpdf:'その名前が入っていることを確認してください。 'multipage_pdf.pdf'を' myfile_2017_11_17.pdf'に変更すると、それはうまくいくはずです。 – ZF007

+0

私はそれを動的にしたい場合、どのように私はそれをフォーマットするのだろうか?文字列への単純なdatetime関数ですか? –

関連する問題