1

I'm trying to create multiple separate plots using Matplotib, then save these to a single PDF document. Here's my 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()

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

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

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

Thanks in advance!

3
  • could it be that you only call pdf.savefig() once? Commented Sep 14, 2016 at 16:27
  • you need to save the figures specifically to the PDF, e.g., pdf.savefig(fig1) and pdf.savefig(fig2) Commented Sep 14, 2016 at 16:28
  • Thank you both (@benten and @Paul H) for correcting my mistake. The code works as intended now! Commented Sep 14, 2016 at 17:09

1 Answer 1

3

Your code is a bit over-complicated. These two lines are redundant:

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

I would just do:

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.