Creating Multiple Plots Using Matplotlib
<p>I'm trying to create multiple separate plots using Matplotib, then save these to a single PDF document.  Here's my code:</p>

<pre><code>pdf = matplotlib.backends.backend_pdf.PdfPages('Activity_Report.pdf')

fig1 = plt.figure(1)
fig1.figure(figsize=(11.69, 8.27))
ax1 = fig1.add_subplot(111)


# ******** product 1 ********
ax1.plot(Prod_01['Date'], Prod_01['Orders'], marker='o', label='Orders', color='navy', linewidth='2')
ax1.plot(Prod_01['Date'], Prod_01['Orders_MA'], linestyle='--', label='Orders (10-d)', color='darkblue', linewidth='2')

ax1.plot(Prod_01['Date'], Prod_01['Volume'], marker='o', label='Volume', color='firebrick', linewidth='2')
ax1.plot(Prod_01['Date'], Prod_01['Volume_MA'], linestyle='--', label='Volume (10-d)', color='firebrick', linewidth='2')

ax1.plot(Prod_01['Date'], Prod_01['Pass'], marker='o', label='Pass', color='darkgreen', linewidth='2')
ax1.plot(Prod_01['Date'], Prod_01['Pass_MA'], linestyle='--', label='Pass (10-d)', color='darkgreen', linewidth='2')

ax1.plot(Prod_01['Date'], Prod_01['Request'], marker='o', label='Request', color='cyan', linewidth='2')
ax1.plot(Prod_01['Date'], Prod_01['Request_MA'], linestyle='--', label='Request (10-d)', color='cyan', linewidth='2')

ax1.set_title('Prod_01', fontsize='20', rasterized=True)
ax1.tick_params(axis='both', which='major', labelsize='10')
ax1.legend(loc='upper left', fontsize='10')

ax1.get_yaxis().set_major_formatter(tkr.FuncFormatter(lambda x, p: format(int(x), ',')))
ax1.xaxis.set_major_formatter(custom_x_axis_format)

# ******** product 2 ********

fig2 = plt.figure(2)
fig2.figure(figsize=(11.69, 8.27))
ax2 = fig2.add_subplot(111)

ax2.plot(Prod_02['Date'], Prod_02['Order'], marker='o', label='Order', color='navy', linewidth='2')
ax2.plot(Prod_02['Date'], Prod_02['Order_MA'], linestyle='--', label='Order (10-d)', color='darkblue', linewidth='2')

ax2.plot(Prod_02['Date'], Prod_02['Volume'], marker='o', label='Volume', color='firebrick', linewidth='2')
ax2.plot(Prod_02['Date'], Prod_02['Volume_MA'], linestyle='--', label='Volume (10-d)', color='firebrick', linewidth='2')

ax2.plot(Prod_02['Date'], Prod_02['Pass'], marker='o', label='Pass', color='darkgreen', linewidth='2')
ax2.plot(Prod_02['Date'], Prod_02['Pass_MA'], linestyle='--', label='Pass (10-d)', color='darkgreen', linewidth='2')

ax2.plot(Prod_02['Date'], Prod_02['Request'], marker='o', label='Request', color='cyan', linewidth='2')
ax2.plot(Prod_02['Date'], Prod_02['Request_MA'], linestyle='--', label='Request (10-d)', color='cyan', linewidth='2')

ax2.set_title('Prod_02', fontsize='20', rasterized=True)
ax2.tick_params(axis='both', which='major', labelsize='10')
ax2.legend(loc='upper left', fontsize='10')

ax2.get_yaxis().set_major_formatter(tkr.FuncFormatter(lambda x, p: format(int(x), ',')))
ax2.xaxis.set_major_formatter(custom_x_axis_format)


pdf.savefig()
pdf.close()
</code></pre>

<p>The problem I'm having is that this code is plotting to a <strong>single</strong> figure (rather than <strong>two separate</strong> figures.  That is, <code>product 1</code> and <code>product 2</code> are plotted on a single figure (i.e. on top of each other).  I tried to great separate plots using <code>Figure()</code> (as seen on a few posts on StackOverflow), but that doesn't seem to work.</p>

<p>It's likely that I have <code>fig1</code>, <code>ax1</code>, <code>fig2</code> and <code>ax2</code> defined incorrectly (being new to Python, I still don't understand their use 100%).</p>

<p>Does anyone see why this code is producing a single plot instead of the intended two separate plots?</p>

<p>Thanks in advance!</p>

<p>Your code is a bit over-complicated. These two lines are redundant:</p>

<pre><code>fig1 = plt.figure(1)
fig1.figure(figsize=(11.69, 8.27))
</code></pre>

<p>I would just do:</p>

<pre><code>FIGSIZE = (11.69, 8.27) 
fig1, ax1 = plt.subplots(figsize=FIGSIZE)
# plot things 

fig2, ax2 = plt.subplots(figsize=FIGSIZE)
# plot more things

# etc etc

pdf = matplotlib.backends.backend_pdf.PdfPages('Activity_Report.pdf')
for fig in [fig1, fig2, ...]:
    pdf.savefig(fig)

plt.close('all')
</code></pre>

python matplotlib