简体   繁体   English

使用Matplotlib创建多个图

[英]Creating Multiple Plots Using Matplotlib

I'm trying to create multiple separate plots using Matplotib, then save these to a single PDF document. 我正在尝试使用Matplotib创建多个单独的图,然后将这些图保存到单个PDF文档中。 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 (ie 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. 我遇到的问题是这个代码正在绘制一个单独的数字(而不是两个单独的数字。也就是说, product 1product 2绘制在一个数字上(即彼此重叠)。我试图很好使用Figure()分开绘图(如StackOverflow上的几篇文章所示),但这似乎不起作用。

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%). 这可能是因为我有fig1ax1fig2ax2错误定义(即新的Python的,我还是不明白,他们使用100%)。

Does anyone see why this code is producing a single plot instead of the intended two separate plots? 有谁知道为什么这个代码生成一个单独的图而不是预期的两个单独的图?

Thanks in advance! 提前致谢!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM