我试图将所有11个扇区的图表从部门列表保存到1pdf表格。到目前为止,下面的代码给了我一个单独的表格上的图表(11页pdf页)。
每日返回函数是我正在绘制的数据。每张图上有两行。
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()
发布于 2017-07-18 06:30:16
不确定你的缩进是否在你的问题上是错误的,但关键是你需要在保存你的无花果为pdf之前完成绘制所有的子图。具体来说,您需要将fig = plt.figure(figsize=(12,12))
和pdf.savefig(fig)
移到for
循环之外,并将它们保留在with
语句中。下面是您修改的一个示例,它给您提供了一个pdf页面,包含11个子图:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import numpy as np
with PdfPages('test.pdf') as pdf:
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
s = s * 50
fig = plt.figure(figsize=(12,12))
n=0
for i in range(11):
n += 1
ax = fig.add_subplot(4,3,n)
ax.plot(t, s, linewidth=3, label='a')
ax.plot(t, s / 2, linewidth=3, label='b')
ax.set_ylim(0, 100)
ax.legend()
ax.yaxis.set_label_text('Excess movement (%)')
plt.setp(ax.xaxis.get_ticklabels(), rotation='45')
pdf.savefig(fig)
https://stackoverflow.com/questions/45157243
复制相似问题