ToC
In [4]:
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
iris= sns.load_dataset('iris')
iris.head()
Out[4]:
Pairgrid is similar to pairplot
, except, it returns back an empty grid that you can fill up with desired plots later. Refresher on pariplot
In [6]:
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)
Out[6]:
lmplot()
for scatter and regression per categorySometimes, 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.
In [330]:
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
)
In [7]:
#load tips data
tips = sns.load_dataset('tips')
tips.head()
Out[7]:
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 FacetGrid
s.
In [12]:
#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')
Out[12]:
In [16]:
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')
Out[16]:
In [24]:
plt.figure(figsize=(5,5)) #generate a fig, sns will piggyback this with the plot
sns.distplot(tips['total_bill'])
Out[24]:
In [31]:
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')
Out[31]:
Another way to set the size is to access the fig
handle direclty
In [35]:
#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')
Out[35]: