Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在单个PDF文件中保存多个绘图 绘图模块 主模块_Python_Matplotlib - Fatal编程技术网

Python 在单个PDF文件中保存多个绘图 绘图模块 主模块

Python 在单个PDF文件中保存多个绘图 绘图模块 主模块,python,matplotlib,Python,Matplotlib,我想将所有图plot1、plot2、plot3保存到一个PDF文件中。有什么办法可以实现吗?我无法在主模块中包含绘图功能 有一个名为pyplot.savefig的函数,但它似乎只适用于一个图形。还有其他方法可以实现它吗?没关系 def plotGraph(X,Y): fignum = random.randint(0,sys.maxint) fig = plt.figure(fignum) ### Plotting arrangements ### ret

我想将所有图plot1、plot2、plot3保存到一个PDF文件中。有什么办法可以实现吗?我无法在主模块中包含
绘图功能


有一个名为
pyplot.savefig
的函数,但它似乎只适用于一个图形。还有其他方法可以实现它吗?

没关系

def plotGraph(X,Y):
     fignum = random.randint(0,sys.maxint)
     fig = plt.figure(fignum)
     ### Plotting arrangements ###
     return fig
------绘图模块------

-----主模块----


-----main模块------

对于单个pdf文件中的多个绘图,您可以使用

plotGraph
函数中,应返回地物,然后调用地物对象的
savefig

------绘图模块------

------绘图模块------

-----主模块----


如果有人从谷歌来到这里,希望将一个数字转换成.pdf格式(这正是我想要的):


等等,我以为你想把绘图保存到一个PDF文件中。您的解决方案将图像保存到三个单独的PNG文件中,这似乎是另一个问题的答案。非常抱歉。不知怎的,我在保存文件方面做得更多了。我知道后端pdf的事情,但是继续我的工作,忽略了添加它。无论如何,谢谢你指出。看到反对票的数量,你可能会考虑删除这个答案,为其他答案留出“空间”。你如何设置pdf的页面大小?@我不确定你是否可以直接设置pdf的页面大小,但你可以更改数字大小:f=plt.figure(figsize=(5,10)),例如,要更改pdf比率,“绘图排列”值得一个示例来解释如何实际将绘图添加到图形中@user2127595这对我有用:def plot_graph(x,y1,y2):fig=plt.figure()axes1=fig.add_subplot(2,1,1)axes2=fig.add_subplot(2,1,2)axes1.plot(x,y1)axes2.plot(x,y2)return figIf您正在使用的
plt.show()
放在
pdf.savefig()之后。
import matplotlib.pyplot as plt
### tempDLStats, tempDLlabels are the argument
plot1 = plotGraph(tempDLstats, tempDLlabels)
plot2 = plotGraph(tempDLstats_1, tempDLlabels_1)
plot3 = plotGraph(tempDLstats_2, tempDLlabels_2)
plt.show()
def plotGraph(X,Y):
     fignum = random.randint(0,sys.maxint)
     fig = plt.figure(fignum)
     ### Plotting arrangements ###
     return fig
 import matplotlib.pyplot as plt
 ### tempDLStats, tempDLlabels are the argument
 plot1 = plotGraph(tempDLstats, tempDLlabels)
 plot2 = plotGraph(tempDLstats_1, tempDLlabels_1)
 plot3 = plotGraph(tempDLstats_2, tempDLlabels_2)
 plt.show()
 plot1.savefig('plot1.png')
 plot2.savefig('plot2.png')
 plot3.savefig('plot3.png')
def plotGraph(X,Y):
      fig = plt.figure()
      ### Plotting arrangements ###
      return fig
from matplotlib.backends.backend_pdf import PdfPages

plot1 = plotGraph(tempDLstats, tempDLlabels)
plot2 = plotGraph(tempDLstats_1, tempDLlabels_1)
plot3 = plotGraph(tempDLstats_2, tempDLlabels_2)

pp = PdfPages('foo.pdf')
pp.savefig(plot1)
pp.savefig(plot2)
pp.savefig(plot3)
pp.close()
import datetime
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

# Create the PdfPages object to which we will save the pages:
# The with statement makes sure that the PdfPages object is closed properly at
# the end of the block, even if an Exception occurs.
with PdfPages('multipage_pdf.pdf') as pdf:
    plt.figure(figsize=(3, 3))
    plt.plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o')
    plt.title('Page One')
    pdf.savefig()  # saves the current figure into a pdf page
    plt.close()

    plt.rc('text', usetex=True)
    plt.figure(figsize=(8, 6))
    x = np.arange(0, 5, 0.1)
    plt.plot(x, np.sin(x), 'b-')
    plt.title('Page Two')
    pdf.savefig()
    plt.close()

    plt.rc('text', usetex=False)
    fig = plt.figure(figsize=(4, 5))
    plt.plot(x, x*x, 'ko')
    plt.title('Page Three')
    pdf.savefig(fig)  # or you can pass a Figure object to pdf.savefig
    plt.close()

    # We can also set the file's metadata via the PdfPages object:
    d = pdf.infodict()
    d['Title'] = 'Multipage PDF Example'
    d['Author'] = u'Jouni K. Sepp\xe4nen'
    d['Subject'] = 'How to create a multipage pdf file and set its metadata'
    d['Keywords'] = 'PdfPages multipage keywords author title subject'
    d['CreationDate'] = datetime.datetime(2009, 11, 13)
    d['ModDate'] = datetime.datetime.today()
import matplotlib.pyplot as plt

f = plt.figure()
plt.plot(range(10), range(10), "o")
plt.show()

f.savefig("foo.pdf", bbox_inches='tight')