将多个数字写入.pdf时出错

时间:2014-09-09 20:55:12

标签: python pandas pdfpages

当从以下数据帧和系列生成pdf时,pandas会生成两个空白数字并将它们插入到我的.pdf文件中。图1和图3都是空白,而图2和图4是打印到.pdf文件。我曾希望将下面的所有三个数字(图1,图2和图3)写入.pdf文件。

import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from pandas.tools.plotting import bootstrap_plot
from pandas.tools.plotting import scatter_matrix
from pandas import *

range_days = 10
ydate = "2014-09-09"
wdate = "2014-08-31"

c_data = [1,2,3,4,3,2,1,3,4,5]
p_data = [1,2,3,4,3,2,1,3,4,5]
d_data = [1,2,3,4,3,2,1,3,4,5]

d = {"C":Series(c_data, index=date_range(wdate, periods=range_days)),"P": Series(p_data, index=date_range(wdate, periods=range_days)),"D":Series(d_data, index=date_range(wdate, periods=range_days))}
d2 = {"C":d["C"], "P":d["P"]}

ts2 = DataFrame(d2, index=date_range(wdate, periods=range_days))
ts = DataFrame(d, index=date_range(wdate, periods=range_days))

pdf_name = "Metrics_" + str(wdate) + "_to_" + str(ydate)+ ".pdf"
pdf = PdfPages(pdf_name)

#P & C over time
fig1 = plt.figure()
ts2.plot(secondary_y=["P"])
pdf.savefig(fig1)

#Density Plot of C
fig2 = plt.figure()
d["C"].plot(kind="kde")
pdf.savefig(fig2)

#scatter matrix on the data_frame
fig3 = plt.figure()
scatter_matrix(ts, alpha=0.2, figsize=(6, 6), diagonal='kde')
pdf.savefig(fig3)

pdf.close()

1 个答案:

答案 0 :(得分:1)

删除一些plt.figure()变量会删除正在创建的空白数字并将其插入到pdf文件中。

import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from pandas.tools.plotting import bootstrap_plot
from pandas.tools.plotting import scatter_matrix
from pandas import *

range_days = 10
ydate = "2014-09-09"
wdate = "2014-08-31"

c_data = [1,3,3,2,3,2,1,6,7,9]
p_data = [0,5,6,11,3,2,1,1,3,4]
d_data = [1,7,3,8,3,2,1,3,2,3]

d = {"C":Series(c_data, index=date_range(wdate, periods=range_days)),"P": Series(p_data, index=date_range(wdate, periods=range_days)),"D":Series(d_data, index=date_range(wdate, periods=range_days))}
d2 = {"C":d["C"], "P":d["P"]}

ts2 = DataFrame(d2, index=date_range(wdate, periods=range_days))
ts = DataFrame(d, index=date_range(wdate, periods=range_days))

pdf_name = "Metrics_" + str(wdate) + "_to_" + str(ydate)+ ".pdf"
pdf = PdfPages(pdf_name)

#P & C over time
ts2.plot(secondary_y=["P"])
pdf.savefig()

#Density Plot of Conversion
plt.figure()
d["C"].plot(kind="kde")
pdf.savefig()

#scatter matrix on the data_frame
scatter_matrix(ts, alpha=0.2, figsize=(6, 6), diagonal='kde')
pdf.savefig()

pdf.close()