[英]Saving multiple plots to one pdf file
I am trying to save multiple plots in one pdf file.我正在尝试将多个图保存在一个 pdf 文件中。
can anyone help with the following code?任何人都可以帮助以下代码吗?
what is wrong with this code:这段代码有什么问题:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import random as rand
pdfFile = PdfPages("output.pdf")
for i in range(10):
xVals = [x for x in range(20)]
yVals = [rand.randint(50,100) for x in xVals]
plt.figure(figsize=(20,10))
fig = plt.plot(xVals , yVals)
plt.xlabel('Data point')
plt.ylabel('Strain [us]')
plt.title(i)
#plt.show()
pdfFile.savefig(fig)
print(i)
pdfFile.close()
plot
returns as list of Line2D
s (in your case just one). plot
作为Line2D
的列表返回(在您的情况下只有一个)。 From this line you need to get the figure using get_figure
:从这一行中,您需要使用
get_figure
获取图形:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import random as rand
pdfFile = PdfPages("output.pdf")
for i in range(10):
xVals = [x for x in range(20)]
yVals = [rand.randint(50,100) for x in xVals]
plt.figure(figsize=(20,10))
line, = plt.plot(xVals , yVals)
plt.xlabel('Data point')
plt.ylabel('Strain [us]')
plt.title(i)
#plt.show()
pdfFile.savefig(line.get_figure())
print(i)
pdfFile.close()
When no figure is specified in savefig
the current figure is saved, so you could also simply write:如果
savefig
中没有指定图形,则保存当前图形,因此您也可以简单地编写:
plt.plot(xVals , yVals)
pdfFile.savefig()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.