使用 matplotlib 将绘图保存为 pdf 文件 [英] Saving plots to pdf files using matplotlib
本文介绍了使用 matplotlib 将绘图保存为 pdf 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将 1 个以上的绘图保存到 pdf 文件中.这是我的代码:
I want to save more than 1 plot to a pdf file. Here is my code:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
def function_plot(X,Y):
plt.figure()
plt.clf()
pp = PdfPages('test.pdf')
graph = plt.title('y vs x')
plt.xlabel('x axis', fontsize = 13)
plt.ylabel('y axis', fontsize = 13)
pp.savefig(graph)
function_plot(x1,y1)
function_plot(x2,y2)
我知道我的想法很混乱,但我找不到编写代码的方法.问题是我需要在图形上标注x和y轴.
I know that my ideas are scrambled but I can't find the way to write my code. The thing is that I need my graphs to have labeled x and y axis.
推荐答案
我能够解决它.我的错误是 pp.savefig()
不应带有参数.
I was able to solve it. My mistake was that pp.savefig()
should not take arguments.
这是我的最终代码:
from matplotlib.backends.backend_pdf import PdfPages
import numpy as np
import matplotlib.pyplot as plt
x1 = np.arange(10)
y1 = x1**2
x2 = np.arange(20)
y2 = x2**2
pp = PdfPages('test.pdf')
def function_plot(X,Y):
plt.figure()
plt.clf()
plt.plot(X,Y)
plt.title('y vs x')
plt.xlabel('x axis', fontsize = 13)
plt.ylabel('y axis', fontsize = 13)
pp.savefig()
function_plot(x1,y1)
function_plot(x2,y2)
pp.close()
这篇关于使用 matplotlib 将绘图保存为 pdf 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文