1

从以下数据帧和系列生成 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()
4

1 回答 1

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()
于 2014-09-10T16:02:56.543 回答