使用Matplotlib创建多个图

时间:2016-09-14 16:19:36

标签: python matplotlib

我正在尝试使用Matplotib创建多个单独的图,然后将这些图保存到单个PDF文档中。这是我的代码:

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()

我遇到的问题是此代码正在绘制单个数字(而不是两个单独的数字。即product 1和{ {1}}被绘制在一个图形上(即彼此重叠)。我尝试使用product 2创建了很好的单独图(如在StackOverflow上的一些帖子中所见),但这似乎不起作用

我可能错误地定义了Figure()fig1ax1fig2(对Python来说是新手,我仍然不明白他们100%使用它们)

有没有人知道为什么这段代码会生成一个单独的图而不是预期的两个单独的图?

提前致谢!

1 个答案:

答案 0 :(得分:3)

您的代码有点过于复杂。这两行是多余的:

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

我会这样做:

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')