Saving multiple plots on a single pdf page using matplotlib

Viewed 10390

I'm trying to save the graphs of all 11 sectors from sectorlist to 1 pdf sheet. So far the code below gives me a graph on a separate sheet (11 pdf pages).

The daily return functions is the data I'm plotting. There are 2 lines on each graph.

with PdfPages('test.pdf') as pdf:
n=0
for i in sectorlist:
    fig = plt.figure(figsize=(12,12))
    n+=1
    fig.add_subplot(4,3,n)
    (daily_return[i]*100).plot(linewidth=3)
    (daily_return['^OEX']*100).plot()
    ax = plt.gca()
    ax.set_ylim(0, 100)
    plt.legend()
    plt.ylabel('Excess movement (%)')
    plt.xticks(rotation='45')
    pdf.savefig(fig)
plt.show()
1 Answers
Related