# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Plotting with catagorical data

# [Tutorial](http://seaborn.pydata.org/tutorial.html#tutorial)

# %matplotlib inline

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns

from matplotlib import rcParams

rcParams["figure.figsize"]=[8,6]

sns.set(style="whitegrid", color_codes=True)

np.random.seed(sum(map(ord, "categorical")))

titanic = sns.load_dataset("titanic")
tips = sns.load_dataset("tips")
iris = sns.load_dataset("iris")

titanic[:2]

tips[:2]

iris[:2]

# ## 1 - Categorical scatterplots

# ### [`sns.stripplot()`](http://seaborn.pydata.org/generated/seaborn.stripplot.html#seaborn.stripplot)
# ```python
# sns.stripplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, jitter=False, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs)
# ```

sns.stripplot(x="day", y="total_bill", data=tips, jitter=True, hue="sex", dodge=True, size=8);

# ### [`sns.swarmplot()`](http://seaborn.pydata.org/generated/seaborn.swarmplot.html#seaborn.swarmplot)
# ```python
# sns.swarmplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs)
# ```
#

sns.swarmplot(x="day", y="total_bill", hue="sex", data=tips);

# Categories that look numerical will be sorted:

sns.swarmplot(x="size", y="total_bill", data=tips, hue="day");

sns.swarmplot(x="total_bill", y="day", hue="time", data=tips);

# ## 2 - Distributions of categories

# ### [`sns.boxplot()`](http://seaborn.pydata.org/generated/seaborn.boxplot.html#seaborn.boxplot)
# ```python
#  sns.boxplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None,
# saturation=0.75, width=0.8, dodge=True, fliersize=5, linewidth=None, whis=1.5, notch=False, ax=None, **kwargs)
# ```

sns.boxplot(x="day", y="total_bill", hue="time", data=tips);

tips["weekend"] = tips["day"].isin(["Sat", "Sun"])

sns.boxplot(x="day", y="total_bill", hue="weekend", data=tips);

# ### [`sns.violinplot()`](http://seaborn.pydata.org/generated/seaborn.violinplot.html#seaborn.violinplot)
# [violin plot - wikipedia](https://en.wikipedia.org/wiki/Violin_plot)
# ```python
# sns.violinplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, bw='scott', cut=2, scale='area', scale_hue=True, gridsize=100, width=0.8, inner='box', split=False, dodge=True, orient=None, linewidth=None, color=None, palette=None, saturation=0.75, ax=None, **kwargs)
# ```
# - `scale_hue` : `scale=False`整体计算 `scale` , `True` 分类计算

plt.subplot(121)
sns.violinplot(x="day", y="total_bill", hue="time", data=tips);
plt.subplot(122)
sns.swarmplot(x="day", y="total_bill", hue="time", data=tips);

sns.violinplot(x="total_bill", y="day", hue="time", data=tips,
              bw=.1, scale="count", scale_hue=False);

# split the violin when the hue has only two levels
# - `split = True`

sns.violinplot(x="total_bill", y="day", hue="sex", data=tips,
              bw=.2, scale="area", scale_hue=True, split=True);

# `inner = "box" "quartile" "point" "stick" None ` 

sns.violinplot(x="day", y="total_bill", hue="sex", data=tips,
              split=True, inner="quartile" , palette="Set3");

# Combine `sns.swarmplot()` with `sns.violinplot()` or `sns.boxplot()`
# - `alpha` : 透明度 0~1

sns.violinplot(x="day", y="total_bill", data=tips, inner=None, hue="sex", split=True)
sns.swarmplot(x="day", y="total_bill", data=tips,  alpha=0.6, hue="sex", palette="Set2");

# ## 3 - Statistical with categories

# ### [`sns.barplot()`](http://seaborn.pydata.org/generated/seaborn.barplot.html#seaborn.barplot)
# ```python
# sns.barplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean>, ci=95, n_boot=1000, units=None, orient=None, color=None, palette=None, saturation=0.75, errcolor='.26', errwidth=None, capsize=None, dodge=True, ax=None, **kwargs)
# ```

sns.barplot(x="sex", y="survived", hue="class", data=titanic, palette="hls");

# ### [`sns.countplot()`](http://seaborn.pydata.org/generated/seaborn.countplot.html#seaborn.countplot)
# ```python
# sns.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, 
#               color=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)
# ```
# - `saturation` : 颜色的饱和度

# +
plt.subplot(121)
plt.title("saturation=1.1")
sns.countplot(x="deck", data=titanic, palette="hls", saturation=1.1);

plt.subplot(122)
plt.title("saturation=0.5")
sns.countplot(x="deck", data=titanic, palette="hls", saturation=0.5);
# -

sns.countplot(x="deck", data=titanic, palette="hls", saturation=0.5);

sns.countplot(y="deck", data=titanic, hue="sex", palette="hls");

survive = titanic[titanic["survived"]>0]

# ### [`sns.pointplot()`](http://seaborn.pydata.org/generated/seaborn.pointplot.html#seaborn.pointplot)
# ```python
# sns.pointplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean>, ci=95, n_boot=1000, units=None, markers='o', linestyles='-', dodge=False, join=True, scale=1, orient=None, color=None, palette=None, errwidth=None, capsize=None, ax=None, **kwargs)
# ```
# - point estimation : 点估计
# - confidence interval : 置信区间

# +
ci=95
plt.subplot(121)
plt.title("ci= %s"  %ci)  
sns.pointplot(x="class", y="age", data=survive, ci=ci);

ci=65
plt.subplot(122)
plt.title("ci= %s"  %ci)
sns.pointplot(x="class", y="age", data=survive, ci=ci);
# -

sns.pointplot(x="class", y="age", hue="sex", data=survive, 
              pallete={"male":"g", "female":"m"},
             markers=["^", "o"], linestyles=["-", "--"]);

sns.boxplot(data=iris, orient="v");

sns.violinplot(x=iris.species, y=iris.sepal_length);

f, axes = plt.subplots(nrows=1, ncols=2, figsize=(16,5))
sns.countplot(y="deck", data=titanic, ax=axes[0]);
sns.pointplot(x="deck", y="survived", data=titanic, ax=axes[1]);

# ### [`sns.factorplot()`](http://seaborn.pydata.org/generated/seaborn.factorplot.html#seaborn.factorplot)
# ```python
# sns.factorplot(x=None, y=None, hue=None, data=None, row=None, col=None, col_wrap=None, estimator=<function mean>, ci=95, n_boot=1000, units=None, order=None, hue_order=None, row_order=None, col_order=None, kind='point', size=4, aspect=1, orient=None, color=None, palette=None, legend=True, legend_out=True, sharex=True, sharey=True, margin_titles=False, facet_kws=None, **kwargs)
# ```
# - `size` : height(in inches) of each facet  
# - `aspect`: aspect ratio 长宽比,  $aspect * size = width $

sns.factorplot(x="day", y="total_bill", hue="smoker", data=tips, kind="swarm");

sns.factorplot(x="survived", hue="class", col="sex", data=titanic, kind="count");

sns.factorplot(x="time", y="total_bill", hue="smoker",
              col="day", data=tips, kind="box", size=6, aspect=0.5);

g = sns.PairGrid(tips,
            x_vars=["smoker", "time", "sex"],
            y_vars=["total_bill", "tip"],
            aspect=0.75, size=6);
g.map(sns.violinplot, palette="hls");

# ### [`sns.FacetGrid()`](http://seaborn.pydata.org/generated/seaborn.FacetGrid.html#seaborn.FacetGrid)
# ```python
# sns.FacetGrid(data, row=None, col=None, hue=None, col_wrap=None, sharex=True, sharey=True, size=3, aspect=1, palette=None, row_order=None, col_order=None, hue_order=None, hue_kws=None, dropna=True, legend_out=True, despine=True, margin_titles=False, xlim=None, ylim=None, subplot_kws=None, gridspec_kws=None)
# ```



StarcoderdataJupyterScriptsDedupFiltered