I have a dataframe that looks like this:
import pandas as pd
foo = pd.DataFrame({'occasions': ['a', 'a', 'b', 'b'], 'val':[1,1,1,2]})
I am trying to create a histogram with a facet using seaborn like this and I want to save that image to a pdf, so i am doing this:
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
pdf = PdfPages('exploration.pdf')
g = sns.FacetGrid(foo, col="occasions", margin_titles=True)
g.map(plt.hist,'val', color="steelblue")
g.set_titles(col_template = '{col_name}')
g.fig.suptitle('title')
plot_fig(g, pdf)
where
def plot_fig(plot, pdf):
try:
fig = plot.draw()
except:
fig = plot
pdf.savefig(fig, height=10, width=18, dpi=500, bbox_inches='tight', pad_inches=0.5)
plt.close()
But I get an error: ValueError: No figure <seaborn.axisgrid.FacetGrid object at 0x7f1ca2300780>, any ideas why and how I could fix that ?