[英]Saving multiple figures to one pdf file in matplotlib
我有一個代碼,基於groupby
創建大約50個圖表。 代碼如下所示:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
with PdfPages('foo.pdf') as pdf:
for i, group in df.groupby('station_id'):
plt.figure()
fig=group.plot(x='year', y='Value',title=str(i)).get_figure()
pdf.savefig(fig)
當我希望將所有數據存儲到一個pdf中時,這只保存了一個數字(我系列中的最后一個)。 任何幫助,將不勝感激。
您的代碼中存在縮進錯誤。 由於您的繪圖命令不在循環中,因此它只會創建最后一個繪圖。
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
with PdfPages('foo.pdf') as pdf:
for i, group in df.groupby('station_id'):
plt.figure()
fig=group.plot(x='year', y='Value',title=str(i)).get_figure()
pdf.savefig(fig)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.