我正在尝试将图形保存到pdf
使用matplot,Python 2.7和Windows 7的系统中。我可以将图形另存为a .png
,但是当我尝试将其另存为.pd
f时,文件为空白。这是一些示例代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
x = [1, 2, 3, 4, 5]
y = [6, 7, 8, 9, 10]
linewidthPlot = '2'
linestylePlot = '-'
markerPlot = 'o'
colorPlot = 'b'
xlabel = 'x'
ylabel = 'y'
title = 'x v y'
# Plot the data using matplot.
fig1 = plt.figure()
ax1 = fig1.gca()
line1, = plt.plot(x, y, linewidth=linewidthPlot,
linestyle=linestylePlot, marker=markerPlot, color=colorPlot)
axisScale = 0.05
xmin = min(x) - (axisScale * (max(x) - min(x)))
xmax = max(x) + (axisScale * (max(x) - min(x)))
ymin = min(y) - (axisScale * (max(y) - min(y)))
ymax = max(y) + (axisScale * (max(y) - min(y)))
plt.axis([xmin, xmax, ymin, ymax])
ax1.ticklabel_format(useOffset=False)
plt.grid()
# Add a legend.
first_legend = plt.legend([line1], ["data"], loc=1, numpoints=1)
# Label the axes.
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
#pp = PdfPages("discard1.pdf")
#plt.savefig(pp, format='pdf')
#pp.close()
plt.savefig('discard1.png')
plt.show()
这段代码有效,但是当我将文件名更改为discard1.pdf
或使用该PdfPages
函数生成multipage
PDF时,最终得到一个空白文件。有人知道如何解决此问题吗?
对于他人的教育:
正如Martin Evans指出的那样,这是Adobe Reader和其他一些pdf阅读器无法查看由matplotlib创建的pdf数字的问题。
解决方案是在以下位置将linewidthPlot ='2'更改为linewidthPlot = 2:
line1, = plt.plot(x, y, linewidth=linewidthPlot,
linestyle=linestylePlot, marker=markerPlot, color=colorPlot)
也许应该将其报告为错误?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句