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

"""
Explanation: Content:
- 4.2.1 Stripplot
- 4.2.2 Swarmplot
- 4.2.3 Boxplot
- 4.2.4 Violinplot
- 4.2.5 Countplot
- 4.2.6 Barplot
- 4.2.7 Pointplot
Import Libraries
End of explanation
"""


tips = sns.load_dataset('tips')
tips.head()

# Iris data in wide-form
iris = sns.load_dataset('iris')
iris.head()

# Melt iris data to long-form
irislong = pd.melt(iris,id_vars='species', var_name='measurement')
irislong.head()

"""
Explanation: Import data
End of explanation
"""


# Plotting using wide-form iris data, each numeric column will be plotted
sns.stripplot(data=iris);

# Plottin using long-form iris data, indicate x, y values
sns.stripplot(x='measurement',y='value',data=irislong);

# Long-form of data is preferred to take advantage of hue parameter, which allows visualisation of a second categorical variable
sns.stripplot(x='measurement',y='value',data=irislong,hue='species',jitter=0.05);

"""
Explanation: 4.2.1 Stripplot
Stripplot is also known as dotplot in Minitab/Excel. It shows all the data points for each category.
Data can be in long-form or wide-form.
End of explanation
"""


sns.swarmplot(x='measurement',y='value',hue='species',data=irislong);

# Another example using Tips data set
sns.swarmplot(x="day", y="total_bill", data=tips);

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

"""
Explanation: 4.2.2 Swarmplot
Swarmplot is similar to stripplot, but the points are adjusted automatically so that they don't overlap.
End of explanation
"""


sns.boxplot(x='measurement',y='value',data=irislong);

sns.boxplot(x='measurement',y='value',hue='species',data=irislong);

"""
Explanation: 4.2.3 Boxplot
Boxplot can be applied to long-form or wide-form of data. But long-form is preferred to take advantage of hue parameter.
End of explanation
"""


sns.set_style("whitegrid")
sns.boxplot(x='measurement',y='value',data=irislong);
sns.swarmplot(x='measurement',y='value',data=irislong,color='0.4',alpha=0.75);

"""
Explanation: Combining boxplot with swarmplot
End of explanation
"""


sns.violinplot(x='measurement',y='value',data=irislong);

sns.violinplot(x='measurement',y='value',hue='species',data=irislong);

sns.violinplot(x='day',y='tip',data=tips);

# Split the violin into half for a second categorical (binary) variable.
ax = sns.violinplot(x='day',y='tip',data=tips,hue='sex',split=True) # Use dodge instead of split for new version 8.0
ax.legend(bbox_to_anchor=(1,0.5));

# Customising the inner of violinplot
# Default: Box 
# Other choices: point, quartile, stick
sns.violinplot(x='day',y='tip',data=tips,inner='point');

"""
Explanation: 4.2.4 Violinplot
Violinplot draws a combination of boxplot and kde plot.
End of explanation
"""


sns.violinplot(x='day',y='tip',data=tips,inner=None)
sns.swarmplot(x='day',y='tip',data=tips,color='k',alpha=0.5);

"""
Explanation: Combining violinplot with swarmplot.
End of explanation
"""


# Count the frequency of each categorical variable
sns.countplot(x='day',data=tips);

# Display the count for each bar
ax = sns.countplot(x='day',data=tips);
for p in ax.patches:
    h = p.get_height()
    ax.text(p.get_x()+p.get_width()/2, h+2, '{:1.0f}'.format(h), ha="center") 
ax.set_title('Number of customers per day');

"""
Explanation: 4.2.5 Countplot
The traditional bar chart is called Countplot in Seaborn.
End of explanation
"""


# Average of a numerical variable, with CI (default 95%)
ax = sns.barplot(x='day',y='tip',data=tips)
ax.set_title('Average tip per day');

ax = sns.barplot(x='day',y='tip',data=tips,hue='time',capsize=0.1,ci=90)
ax.set_title('Average tip per day')
# Display legend outside the axes
ax.legend(bbox_to_anchor=(1,0.5),title='Time');

"""
Explanation: 4.2.6 Barplot
In Seaborn, the Barplot shows the point estimate and confidence intervals as bars. It will include an error bar by default.
Do not confuse Seaborn Barplot with traditional bar chart in matplotlib or Excel.
End of explanation
"""


sns.pointplot(x='day',y='tip',data=tips);

sns.pointplot(x='day',y='tip',hue='time',data=tips,dodge=True);

"""
Explanation: 4.2.7 Pointplot
Pointplot also shows the point estimate and CI like Barplot, but without the bar.
End of explanation
"""


