<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Graphics using Seaborn
We previously have covered how to do some basic graphics using matplotlib. In this notebook we introduce a package called seaborn. seaborn builds on top of matplotlib by doing 2 things
Step1:  As per usual, we begin by listing the versions of each package that is used in this notebook.
Step2:  Datasets
There are some classical datasets that get used to demonstrate different types of plots. We will use several of them here.
tips 
Step3:  Better Defaults
Recall that in our previous notebook that we used plt.style.use to set styles. We will begin by setting the style to "classic"; this sets all of our default settings back to matplotlib's default values.
Below we plot open and closing prices on the top axis and the implied returns on the bottom axis.
Step4:  What did you notice about the differences in the settings of the plot?
Which do you like better? We like the second better.
Investigate other styles and create the same plot as above using a style you like. You can choose from the list in the code below.
If you have additional time, visit the seaborn docs and try changing other default settings.
Step5:  We could do the same for a different style (like ggplot)
Step6:  Exercise
Step7:  kdeplot
What does kde stand for?
kde stands for "kernel density estimation." This is (far far far) beyond the scope of this class, but the basic idea is that this is a smoothed histogram. When we are trying to get information about distributions it sometimes looks nicer than a histogram does.
Step8:  Exercise
Step9:  Jointplot
We now show what jointplot does. It draws a scatter plot of two variables and puts their histogram just outside of the scatter plot. This tells you information about not only the joint distribution, but also the marginals.
Step10:  We can also plot everything as a kernel density estimate -- Notice the main plot is now a contour map.
Step11:  Like an contour map
Exercise
Step12:  violinplot
Some of the story of this notebook is that distributions matter and how we can show them. Violin plots are similar to a sideways kernel density and it allows us to look at how distributions matter over some aspect of the data.
Step13:  Exercise
Step14:  Pairplot
Pair plots show us two things. They show us the histograms of the variables along the diagonal and then the scatter plot of each pair of variables on the off diagonal pictures.
Why might this be useful? It allows us to look get an idea of the correlations across each pair of variables and gives us an idea of their relationships across the variables.
Step15:  Below is the same plot, but slightly different. What is different?
Step16:  What's different about this plot?
Different colors for each company.
Step17:  lmplot
We often want to think about running regressions of variables. A statistician named Francis Anscombe came up with four datasets that
Step18:  lmplot plots the data with the regression coefficient through it.
Step19:  regplot
regplot also shows the regression line through data points
<ASSISTANT_TASK:>
Python Code:
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import sys
%matplotlib inline
Explanation: Graphics using Seaborn
We previously have covered how to do some basic graphics using matplotlib. In this notebook we introduce a package called seaborn. seaborn builds on top of matplotlib by doing 2 things:
Gives us access to more types of plots (Note: Every plot created in seaborn could be made by matplotlib, but you shouldn't have to worry about doing this)
Sets better defaults for how the plot looks right away
Before we start, make sure that you have seaborn installed. If not, then you can install it by
conda install seaborn
This notebook was created by Dave Backus, Chase Coleman, and Spencer Lyon for the NYU Stern course Data Bootcamp.
End of explanation
# check versions (overkill, but why not?)
print('Python version:', sys.version)
print('Pandas version: ', pd.__version__)
print('Matplotlib version: ', mpl.__version__)
print('Seaborn version: ', sns.__version__)
Explanation: As per usual, we begin by listing the versions of each package that is used in this notebook.
End of explanation
tips = sns.load_dataset("tips")
ansc = sns.load_dataset("anscombe")
tita = sns.load_dataset("titanic")
Explanation: Datasets
There are some classical datasets that get used to demonstrate different types of plots. We will use several of them here.
tips : This dataset has informaiton on waiter tips. Includes information such as total amount of the bill, tip amount, sex of waiter, what day of the week, which meal, and party size.
anscombe: This dataset is a contrived example. It has 4 examples which differ drastically when you look at them, but they have the same correlation, regression coefficient, and $R^2$.
titanic : This dataset has information on each of the passengers who were on the titanic. Includes information such as: sex, age, ticket class, fare paid, whether they were alone, and more.
End of explanation
plt.style.use("classic")
def plot_tips():
    fig, ax = plt.subplots(2, figsize=(8, 6))
    tips[tips["sex"] == "Male"].plot(x="total_bill", y="tip", ax=ax[0], kind="scatter",
                                     color="blue")
    tips[tips["sex"] == "Female"].plot(x="total_bill", y="tip", ax=ax[1], kind="scatter",
                                       color="#F52887")
    ax[0].set_xlim(0, 60)
    ax[1].set_xlim(0, 60)
    ax[0].set_ylim(0, 15)
    ax[1].set_ylim(0, 15)
    ax[0].set_title("Male Tips")
    ax[1].set_title("Female Tips")
    fig.tight_layout()
plot_tips()
# fig.savefig("/home/chase/Desktop/foo.png")
# sns.set() resets default seaborn settings
sns.set()
plot_tips()
Explanation: Better Defaults
Recall that in our previous notebook that we used plt.style.use to set styles. We will begin by setting the style to "classic"; this sets all of our default settings back to matplotlib's default values.
Below we plot open and closing prices on the top axis and the implied returns on the bottom axis.
End of explanation
plt.style.available
Explanation: What did you notice about the differences in the settings of the plot?
Which do you like better? We like the second better.
Investigate other styles and create the same plot as above using a style you like. You can choose from the list in the code below.
If you have additional time, visit the seaborn docs and try changing other default settings.
End of explanation
sns.axes_style?
sns.set()
plot_tips()
Explanation: We could do the same for a different style (like ggplot)
End of explanation
# Move back to seaborn defaults
sns.set()
Explanation: Exercise: Find a style you like and recreate the plot above using that style.
The Juicy Stuff
While having seaborn set sensible defaults is convenient, it isn't a particularly large innovation. We could choose sensible defaults and set them to be our default. The main benefit of seaborn is the types of graphs that it gives you access to -- All of which could be done in matplotlib, but, instead of 5 lines of code, it would require possibly hundreds of lines of code. Trust us... This is a good thing.
We don't have time to cover everything that can be done in seaborn, but we suggest having a look at the gallery of examples.
We will cover:
kdeplot
jointplot
violinplot
pairplot
...
End of explanation
fig, ax = plt.subplots()
ax.hist(tips["tip"], bins=25)
ax.set_title("Histogram of tips")
plt.show()
fig, ax = plt.subplots()
sns.kdeplot(tips["tip"], ax=ax)
ax.hist(tips["tip"], bins=25, alpha=0.25, normed=True, label="tip")
ax.legend()
fig.suptitle("Kernel Density with Histogram");
Explanation: kdeplot
What does kde stand for?
kde stands for "kernel density estimation." This is (far far far) beyond the scope of this class, but the basic idea is that this is a smoothed histogram. When we are trying to get information about distributions it sometimes looks nicer than a histogram does.
End of explanation
fig, ax = plt.subplots()
sns.kdeplot(tips.total_bill, ax=ax)
sns.kdeplot(tips.tip, ax=ax)
# ax.hist(tips.total_bill, alpha=0.3, normed=True)
Explanation: Exercise: Create your own kernel density plot using sns.kdeplot of "total_bill" from the tips dataframe
End of explanation
sns.jointplot(x="total_bill", y="tip", data=tips)
Explanation: Jointplot
We now show what jointplot does. It draws a scatter plot of two variables and puts their histogram just outside of the scatter plot. This tells you information about not only the joint distribution, but also the marginals.
End of explanation
sns.jointplot(x="total_bill", y="tip", data=tips, kind="kde")
Explanation: We can also plot everything as a kernel density estimate -- Notice the main plot is now a contour map.
End of explanation
sns.jointplot(x="size", y="tip", data=tips, 
              kind="kde")
sns.jointplot(x="size", y="tip", data=tips)
Explanation: Like an contour map
Exercise: Create your own jointplot. Feel free to choose your own x and y data (if you can't decide then use x=size and y=tip). Interpret the output of the plot.
End of explanation
tita.head()
tips.head()
sns.violinplot?
sns.violinplot(x="fare", y="deck", hue="survived", split=True, data=tita)
# sns.swarmplot(x="class", y="age", hue="sex", data=tita)
sns.boxplot(x="class", y="age", hue="sex", data=tita)
Explanation: violinplot
Some of the story of this notebook is that distributions matter and how we can show them. Violin plots are similar to a sideways kernel density and it allows us to look at how distributions matter over some aspect of the data.
End of explanation
sns.swarmplot(x="sex", y="age", hue="survived", data=tita)
Explanation: Exercise: We might also want to look at the distribution of prices across ticket classes. Make a violin plot of the prices over the different ticket classes.
End of explanation
sns.pairplot(tips[["tip", "total_bill", "size"]], size=2.5)
Explanation: Pairplot
Pair plots show us two things. They show us the histograms of the variables along the diagonal and then the scatter plot of each pair of variables on the off diagonal pictures.
Why might this be useful? It allows us to look get an idea of the correlations across each pair of variables and gives us an idea of their relationships across the variables.
End of explanation
sns.pairplot(tips[["tip", "total_bill", "size"]], size=2.5, diag_kind="kde")
Explanation: Below is the same plot, but slightly different. What is different?
End of explanation
tips.head()
sns.pairplot(tips[["tip", "total_bill", "size", "time"]], size=3.5,
             hue="time", diag_kind="kde")
Explanation: What's different about this plot?
Different colors for each company.
End of explanation
fig, ax = plt.subplots(2, 2, figsize=(8, 6))
ansc[ansc["dataset"] == "I"].plot.scatter(x="x", y="y", ax=ax[0, 0])
ansc[ansc["dataset"] == "II"].plot.scatter(x="x", y="y", ax=ax[0, 1])
ansc[ansc["dataset"] == "III"].plot.scatter(x="x", y="y", ax=ax[1, 0])
ansc[ansc["dataset"] == "IV"].plot.scatter(x="x", y="y", ax=ax[1, 1])
ax[0, 0].set_title("Dataset I")
ax[0, 1].set_title("Dataset II")
ax[1, 0].set_title("Dataset III")
ax[1, 1].set_title("Dataset IV")
fig.suptitle("Anscombe's Quartet")
Explanation: lmplot
We often want to think about running regressions of variables. A statistician named Francis Anscombe came up with four datasets that:
Same mean for $x$ and $y$
Same variance for $x$ and $y$
Same correlation between $x$ and $y$
Same regression coefficient of $x$ on $y$
Below we show the scatter plot of the datasets to give you an idea of how different they are.
End of explanation
sns.lmplot(x="x", y="y", data=ansc, col="dataset", hue="dataset",
           col_wrap=2, ci=None)
sns.lmplot(x="total_bill", y="tip", data=tips, hue="sex")
Explanation: lmplot plots the data with the regression coefficient through it.
End of explanation
sns.regplot(x="total_bill", y="tip", data=tips)
Explanation: regplot
regplot also shows the regression line through data points
End of explanation
<END_TASK>