Pivot table for loop multiple plots 导出使用 pdf 页面
Pivot table for loop multiple plots export using pdf pages
使用下面的 pdf 页面会生成没有图表的 pdf。
是否可以从数据透视表中解决此问题?
import seaborn as sns
titanic = sns.load_dataset('titanic')
import matplotlib.backends.backend_pdf
pdf = matplotlib.backends.backend_pdf.PdfPages("seaborn.pdf")
# Loop over list 's' for plots
with PdfPages(r'seaborn.pdf') as export_pdf:
s = ['embark_town', 'class', 'embarked']
for i in s:
fig = plt.figure(figsize = [10, 5]);
ax = titanic.pivot_table(values='fare', columns = i, index='sex', aggfunc='sum').plot()
export_pdf.savefig(fig, bbox_inches='tight')
使用subplots
to get both fig
and ax
then set the ax
argument of DataFrame.plot
:
import seaborn as sns
from matplotlib import pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
titanic = sns.load_dataset('titanic')
pdf = PdfPages("seaborn.pdf")
# Loop over list 's' for plots
with PdfPages(r'seaborn.pdf') as export_pdf:
s = ['embark_town', 'class', 'embarked']
for i in s:
fig, ax = plt.subplots(figsize=[10, 5])
titanic.pivot_table(
'fare', columns=i, index='sex', aggfunc='sum'
).plot(ax=ax)
export_pdf.savefig(fig, bbox_inches='tight')
第 1 页:
第 2 页:
第 3 页:
使用下面的 pdf 页面会生成没有图表的 pdf。
是否可以从数据透视表中解决此问题?
import seaborn as sns
titanic = sns.load_dataset('titanic')
import matplotlib.backends.backend_pdf
pdf = matplotlib.backends.backend_pdf.PdfPages("seaborn.pdf")
# Loop over list 's' for plots
with PdfPages(r'seaborn.pdf') as export_pdf:
s = ['embark_town', 'class', 'embarked']
for i in s:
fig = plt.figure(figsize = [10, 5]);
ax = titanic.pivot_table(values='fare', columns = i, index='sex', aggfunc='sum').plot()
export_pdf.savefig(fig, bbox_inches='tight')
使用subplots
to get both fig
and ax
then set the ax
argument of DataFrame.plot
:
import seaborn as sns
from matplotlib import pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
titanic = sns.load_dataset('titanic')
pdf = PdfPages("seaborn.pdf")
# Loop over list 's' for plots
with PdfPages(r'seaborn.pdf') as export_pdf:
s = ['embark_town', 'class', 'embarked']
for i in s:
fig, ax = plt.subplots(figsize=[10, 5])
titanic.pivot_table(
'fare', columns=i, index='sex', aggfunc='sum'
).plot(ax=ax)
export_pdf.savefig(fig, bbox_inches='tight')
第 1 页:
第 2 页:
第 3 页: