matplotlibの出力画像をPDFで保存する

作成日

2020年5月21日

グラフなどを保存するときに PNG 等より PDF で保存した方が拡大時にも鮮明に表示できるので,matplotlib の機能で PDF をエクスポートしてみます.

sample.py
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

# sample data
x = np.arange(120) * np.pi / 30
y = np.sin(x)

fig = plt.figure()
ax = fig.add_subplot()
ax.plot(y)

with PdfPages('sample.pdf') as pdf:
    pdf.savefig(fig)

mktia's note

Research & Engineering / Blockchain / Web Dev

© 2017-2025 mktia. All rights reserved.