<jupyter_start><jupyter_text>Data Visualization        Seaborn        <NAME>        www.data4sci.com        @bgoncalves, @data4sci<jupyter_code>import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

import warnings
warnings.filterwarnings("ignore")

import watermark
import seaborn as sns

%load_ext watermark
%matplotlib inline
%watermark -n -v -m -g -iv<jupyter_output>numpy     1.18.1
watermark 2.0.2
pandas    1.0.1
autopep8  1.5
seaborn   0.10.0
json      2.0.9
Mon Jun 01 2020 

CPython 3.7.3
IPython 6.2.1

compiler   : Clang 4.0.1 (tags/RELEASE_401/final)
system     : Darwin
release    : 19.4.0
machine    : x86_64
processor  : i386
CPU cores  : 8
interpreter: 64bit
Git hash   : f388954fe48cfc6df2a30721b0c4d3dfcad2dae5<jupyter_text>Seaborn datasets Seaborn makes a small list of datasets easily available:<jupyter_code>plt.style.use('seaborn')
sns.get_dataset_names()<jupyter_output><empty_output><jupyter_text>This makes it easier to learn how to use the library using standard data<jupyter_code>tips = sns.load_dataset("tips")<jupyter_output><empty_output><jupyter_text>The first time you load a dataset you'll need an internet connection so that the data can be downloaded from github. The **load_dataset** function returns a pandas data frame<jupyter_code>tips.head()<jupyter_output><empty_output><jupyter_text>Scatter plot Seaborn is designed to facilitate the use of data frames. Making an attractive scatter plot is as simple as:<jupyter_code>ax = sns.scatterplot(x="total_bill", y="tip", data=tips)
type(ax)
tips.plot.scatter(x='total_bill', y='tip')<jupyter_output><empty_output><jupyter_text>As you can see the axis level functions return an Axis object<jupyter_code>fg = sns.relplot(x="total_bill", y="tip", data=tips, kind="scatter")
type(fg)<jupyter_output><empty_output><jupyter_text>The generic figure functions return a FacetGrid object. Making more complex figures is also very simple<jupyter_code>sns.relplot(x="total_bill", y="tip", data=tips, kind="scatter",
            hue="sex", size="size",
            )<jupyter_output><empty_output><jupyter_text>And we can specify the order in which the colors are used<jupyter_code>sns.relplot(x="total_bill", y="tip", data=tips, kind="scatter",
            hue="sex", size="size",
            hue_order=["Male", "Female"],  
            )<jupyter_output><empty_output><jupyter_text>And differnet marker types, etc<jupyter_code>sns.relplot(x="total_bill", y="tip", data=tips, kind="scatter",
            hue="sex", size="size", style='smoker',
            hue_order=["Female", "Male"],  
            )<jupyter_output><empty_output><jupyter_text>The main advantage of the figure level functions is that they can easily generate subplots covering other dimensions of the dataset<jupyter_code>sns.relplot(x="total_bill", y="tip", data=tips, kind="scatter",
            hue="sex", size="size", 
            col='smoker', row='day', style='time',
            row_order=["Thur", "Fri", "Sat", 'Sun']
            )<jupyter_output><empty_output><jupyter_text>Categorical functions<jupyter_code>sns.catplot(x="sex", y="total_bill", hue="day", data=tips, kind="strip")
sns.catplot(x="sex", y="total_bill", hue="day", data=tips, kind="strip", 
            jitter=False, dodge=True)
sns.catplot(x="sex", y="total_bill", hue="day", data=tips, kind="box")
sns.catplot(x="sex", y="total_bill", hue="day", data=tips, kind="violin")<jupyter_output><empty_output><jupyter_text>Axis level functions While Figure level plots are extremely convenient for data exploration they have one fundamental limitation. Figure level functions build their own figure from scratch. This means that they are not easily stackable so you can't easily combine multiple plots on top of one another. This is perhaps the main reason you migth find yourself using axis level functions directly. Let's look at a quick example<jupyter_code>sns.boxplot(x="sex", y="total_bill", data=tips, color=".8", showfliers=False)
sns.swarmplot(x="sex", y="total_bill", hue="day", data=tips)
plt.gcf().set_size_inches(11, 8)<jupyter_output><empty_output><jupyter_text>FacetGrid We already saw how we can easily use figure level functions to generate faceted plots<jupyter_code>sns.relplot(x="total_bill", y="tip", data=tips, kind="scatter",
            hue="sex", 
            col='smoker', row='day',
            row_order=["Thur", "Fri", "Sat", 'Sun']
            )<jupyter_output><empty_output><jupyter_text>This can also be done manually using **FacetGrid**. First we instantiate a facet<jupyter_code>fg = sns.FacetGrid(tips,
            col="smoker", row="day", hue="sex", 
            row_order=["Thur", "Fri", "Sat", 'Sun']
            )
fg.map(plt.scatter, "total_bill", "tip")
plt.gcf().set_size_inches(11, 16)<jupyter_output><empty_output><jupyter_text>The **FacetGrid** object contains pointers to all the axes so that we can manipulate them directly, if we wish<jupyter_code>fg.axes<jupyter_output><empty_output><jupyter_text>So if we want to add an extra line to one of the subplots we can easily do:<jupyter_code>fg = sns.FacetGrid(tips,
            col="smoker", row="day", hue="sex", 
            row_order=["Thur", "Fri", "Sat", 'Sun']
            )
fg.map(plt.scatter, "total_bill", "tip")

# call methods directly on the axis object
fg.axes[1][1].plot([10, 50], [2, 10], 'r-') 
fg.axes[1][1].set_title('Friday non-smokers')
fg.axes[1][1].legend()
plt.gcf().set_size_inches(11, 16)<jupyter_output><empty_output><jupyter_text>Advanced plots  Pair plotThis generates a matrix of plots looking at all combinations of numerical columns<jupyter_code>tips.head()
tips.dtypes
pp = sns.pairplot(tips, hue="sex")<jupyter_output><empty_output><jupyter_text>Pairplots return a **PairGrid** object, a specialized version of **FacetGrid**. We can still access the individual axis objects in the same way as before<jupyter_code>pp.axes<jupyter_output><empty_output><jupyter_text>Joint plot**jointplot** is also a useful way of exploring datasets. It makes a scatter plot in the center and histograms on the margins<jupyter_code>jp = sns.jointplot("total_bill", "tip", data=tips)<jupyter_output><empty_output><jupyter_text>**Joint plots** return a **JointGrid** object that is slightly different in structure than a **FacetGrid**. To access the center plot you use **jp.ax_joint** and **jp.ax_marg_x**, **jp.ax_marg_y** for the x and y margin plots<jupyter_code>print(type(jp.ax_joint))
print(type(jp.ax_marg_x))
print(type(jp.ax_marg_y))<jupyter_output><class 'matplotlib.axes._subplots.AxesSubplot'>
<class 'matplotlib.axes._subplots.AxesSubplot'>
<class 'matplotlib.axes._subplots.AxesSubplot'><jupyter_text>You can also use the jointplot to visualize the point density<jupyter_code>jp = sns.jointplot("total_bill", "tip", data=tips, kind="hex")
jp = sns.jointplot("total_bill", "tip", data=tips, kind="kde")<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup