import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
iris= sns.load_dataset('iris')
iris.head()

"""
Explanation: Seaborn - grids and customization
ToC
 - Pairgrids
 - lmplot() for scatter and regression per category
 - FacetGrid
 - Customizing grids
 - Fig and font size
End of explanation
"""


grd = sns.PairGrid(data=iris)

#then you can assign what you want plotted for diagonal, above diagonal, below diagonal.
# when mapping, pass just function pointers, dont call the function itself.
grd.map_diag(sns.distplot)
grd.map_upper(plt.scatter)
grd.map_lower(sns.kdeplot)

"""
Explanation: Pairgrids
Pairgrid is similar to pairplot, except, it returns back an empty grid that you can fill up with desired plots later. Refresher on pariplot
End of explanation
"""


pgrid = sns.lmplot(x='min_season', y='min_pressure_merged', 
                   col='any_basin', # the column by which you need to split - needs to be categorical
                   data=set1, 
                   col_wrap=3, # number of columns per row
                   sharex=False, sharey=False, # will repeat ticks, coords for each plot
                   line_kws={'color':'green'} # symbol for regression line
                  )

"""
Explanation: lmplot() for scatter and regression per category
Sometimes, you need to do a joinplot() but split it by some categorical column. You can custom build it using FacetGrid shown in next section. However, seaborn provides a convenience function called lmplot(). Note: In previous pages, you created lmplots() for just 2 columns without any category.
End of explanation
"""


#load tips data
tips = sns.load_dataset('tips')
tips.head()

"""
Explanation: FacetGrid
During EDA, you want to find the distribution of data by sub-categories, sub-conditions. You can do so by building FacetGrids. As it means, you get a grid for every facet of the data.
End of explanation
"""


#for each unique value in `time` you get a row and 
# each unique value in `smoker` you get a col
fg = sns.FacetGrid(data=tips, row='time', col='smoker')

#now map a plot for each of the grid
fg.map(sns.distplot, 'total_bill')

"""
Explanation: Suppose we want to visualize total_bill by time of day and wheather or not it was a smoker. You need to filter data out then make dist plots. YOu can do all of that in 1 step with FacetGrids.
End of explanation
"""


sns.set_style(style='ticks') #ticks, white, dark, darkgrid, whitegrid
#redraw the facet grid from above
fg = sns.FacetGrid(data=tips, row='time', col='smoker')

#now map a plot for each of the grid
fg.map(sns.distplot, 'total_bill')

"""
Explanation: Customizing grids
If you dont like the pale blue background of seaborn plots, you can modify that with set_style.
<blockquote><b>Note:</b> Using set_style() will control the appearance for the entire notebook and all future plots</blockquote>
End of explanation
"""


plt.figure(figsize=(5,5)) #generate a fig, sns will piggyback this with the plot
sns.distplot(tips['total_bill'])

"""
Explanation: Fig and font size
You can use matplotlib figsize but have to specify that as a context as well.
End of explanation
"""


sns.set_context(context='poster', font_scale=0.8)
# valid contexts = paper, notebook, talk, poster - 
# with notebook being 1:1 and paper being smaller and poster being largest

#draw the facet grid
fg = sns.FacetGrid(data=tips, row='smoker', col='time')
fg.map(sns.distplot, 'total_bill')

"""
Explanation: Using seaborn context
You can use the set_context() to pick sizing templates
End of explanation
"""


#draw the facet grid
fg = sns.FacetGrid(data=tips, row='smoker', col='time')
#set the size
fg.fig.set_size_inches(w=10, h=10)
#plot the fig
fg.map(sns.distplot, 'total_bill')

"""
Explanation: Another way to set the size is to access the fig handle direclty
End of explanation
"""


