In [5]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits import mplot3d
%matplotlib inline

Matplotlib 3D Example


In [9]:
fig = plt.figure()
ax = plt.axes(projection='3d')
data_z = 15 * np.random.random(200)
data_x = np.sin(data_z) + 0.1 * np.random.randn(200)
data_y = np.cos(data_z) + 0.1 * np.random.randn(200)
ax.scatter3D(data_x, data_y, data_z, c=data_z, cmap="Reds")


Out[9]:
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x118fae4e0>

Seaborn Examples


In [10]:
rng = np.random.RandomState(0)
x = np.linspace(0, 10, 500)
y = np.cumsum(rng.randn(500, 6), 0)
plt.plot(x, y)
plt.legend('123456', ncol=2, loc='upper left')


Out[10]:
<matplotlib.legend.Legend at 0x1195ba390>

In [13]:
data = np.random.multivariate_normal([0, 0], [[5, 2], [2, 2]], size=2000)
df = pd.DataFrame(data, columns=['x', 'y'])
sns.distplot(df['x'])


Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0x119727208>

In [14]:
sns.jointplot('x', 'y', df)


Out[14]:
<seaborn.axisgrid.JointGrid at 0x118fbc8d0>

In [15]:
iris = sns.load_dataset('iris')
sns.pairplot(iris, hue='species', size=2.5)


Out[15]:
<seaborn.axisgrid.PairGrid at 0x119d3afd0>

In [22]:
tips = sns.load_dataset('tips')
print(tips.head())
tips['tip_pct'] = 100 * tips['tip'] / tips['total_bill']
grid = sns.FacetGrid(tips, row='sex', col='time', margin_titles=True)
grid.map(plt.hist, 'tip_pct', bins=np.linspace(0, 50, 20));


   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1       10.34  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4       24.59  3.61  Female     No  Sun  Dinner     4

In [23]:
sns.jointplot('total_bill', 'tip', data=tips, kind='reg')


Out[23]:
<seaborn.axisgrid.JointGrid at 0x11bc983c8>

In [45]:
df = pd.read_csv('marathon-data.csv')
df['split_secs'] = df['split'].apply(pd.to_timedelta).astype(int) / 1E9
df['final_secs'] = df['final'].apply(pd.to_timedelta).astype(int) / 1E9
df.head()


Out[45]:
age gender split final split_secs final_secs
0 33 M 01:05:38 02:08:51 3938.0 7731.0
1 32 M 01:06:26 02:09:28 3986.0 7768.0
2 31 M 01:06:49 02:10:42 4009.0 7842.0
3 38 M 01:06:16 02:13:45 3976.0 8025.0
4 31 M 01:06:32 02:13:59 3992.0 8039.0

In [47]:
g = sns.jointplot('split_secs', 'final_secs', df)
g.ax_joint.plot(np.linspace(4000, 16000), np.linspace(8000, 32000)) # reference if the speed is steady


Out[47]:
[<matplotlib.lines.Line2D at 0x11bcccd30>]

In [50]:
df['split_frac'] = 1 - 2 * df['split_secs'] / df['final_secs'] # split_frac indicates if a person reduces the speed (>0) or increases the speed (<0)
df.head()


Out[50]:
age gender split final split_secs final_secs split_frac
0 33 M 01:05:38 02:08:51 3938.0 7731.0 -0.018756
1 32 M 01:06:26 02:09:28 3986.0 7768.0 -0.026262
2 31 M 01:06:49 02:10:42 4009.0 7842.0 -0.022443
3 38 M 01:06:16 02:13:45 3976.0 8025.0 0.009097
4 31 M 01:06:32 02:13:59 3992.0 8039.0 0.006842

In [51]:
sns.kdeplot(df.split_frac[df.gender=='M'], label='male', shade=True)
sns.kdeplot(df.split_frac[df.gender=='W'], label='female', shade=True)
plt.xlabel('split_frac')


Out[51]:
<matplotlib.text.Text at 0x11d8fd390>

In [53]:
sns.violinplot('gender', 'split_frac', data=df, palette=['lightblue', 'pink'])


Out[53]:
<matplotlib.axes._subplots.AxesSubplot at 0x11f7c31d0>

Seaborn Regression Examples


In [3]:
anscombe = sns.load_dataset('anscombe')

In [8]:
sns.lmplot(x='x', y='y', data=anscombe.query("dataset=='I'"), ci=None, scatter_kws={'s': 50})


Out[8]:
<seaborn.axisgrid.FacetGrid at 0x116435b38>

In [11]:
sns.lmplot(x='x', y='y', data=anscombe.query("dataset=='II'"), ci=None, scatter_kws={'s': 50}, order=2)


Out[11]:
<seaborn.axisgrid.FacetGrid at 0x116e11080>

In [13]:
tips = sns.load_dataset('tips')
tips.head()


Out[13]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

In [14]:
tips['tip_pct'] = tips['tip'] / tips['total_bill'] * 100
tips.head()


Out[14]:
total_bill tip sex smoker day time size tip_pct
0 16.99 1.01 Female No Sun Dinner 2 5.944673
1 10.34 1.66 Male No Sun Dinner 3 16.054159
2 21.01 3.50 Male No Sun Dinner 3 16.658734
3 23.68 3.31 Male No Sun Dinner 2 13.978041
4 24.59 3.61 Female No Sun Dinner 4 14.680765

In [16]:
sns.lmplot(x='total_bill', y='tip_pct', data=tips)


Out[16]:
<seaborn.axisgrid.FacetGrid at 0x1170164a8>

In [17]:
sns.lmplot(x='total_bill', y='tip_pct', hue='sex', data=tips)


Out[17]:
<seaborn.axisgrid.FacetGrid at 0x116dfb6d8>

In [18]:
sns.lmplot(x='total_bill', y='tip_pct', hue='sex', col='time', data=tips)


Out[18]:
<seaborn.axisgrid.FacetGrid at 0x1172f9a58>

In [21]:
sns.stripplot(x='day', y='tip_pct', data=tips, jitter=True)


Out[21]:
<matplotlib.axes._subplots.AxesSubplot at 0x118bdf3c8>

In [23]:
sns.swarmplot(x='day', y='tip_pct', data=tips)


Out[23]:
<matplotlib.axes._subplots.AxesSubplot at 0x118f2d550>

In [ ]: