In [1]:
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

%load_ext version_information
%load_ext rpy2.ipython

Types of Plots

In [2]:
import warnings
warnings.filterwarnings("ignore")
In [3]:
url = 'http://bit.ly/2b72LNj'
df = pd.read_csv(url)
In [4]:
df.head()
Out[4]:
model mpg cyl disp hp drat wt qsec vs am gear carb
0 Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
1 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
2 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
3 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
4 Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2

Scatter plot

In [5]:
plt.scatter(x = 'wt', y = 'mpg', data = df)
pass
_images/Basic_Plots_6_0.png

Line plot

In [6]:
plt.plot('wt', 'mpg', '-o', data = df.sort_values('wt'))
pass
_images/Basic_Plots_8_0.png

Fitting a regression line

Linear regression

In [7]:
sns.regplot('wt', 'mpg', data = df)
pass
_images/Basic_Plots_11_0.png

Non-parametric regression

In [8]:
sns.regplot('wt', 'mpg', data = df, lowess = True,)
pass
_images/Basic_Plots_13_0.png

Swarm plot

In [9]:
sns.swarmplot('gear', 'mpg', data = df)
pass
_images/Basic_Plots_15_0.png

Bar plot

In [10]:
sns.barplot('gear', 'mpg', data = df)
pass
_images/Basic_Plots_17_0.png

Box plot

In [11]:
sns.boxplot('gear', 'mpg', data = df)
pass
_images/Basic_Plots_19_0.png

Violin plot

In [12]:
sns.violinplot('gear', 'mpg', data = df)
pass
_images/Basic_Plots_21_0.png

Histogram

In [13]:
sns.distplot(df.mpg, kde=False, rug=True)
Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0x1203a35f8>
_images/Basic_Plots_23_1.png

Density plot

In [14]:
sns.distplot(df.mpg)
Out[14]:
<matplotlib.axes._subplots.AxesSubplot at 0x12068a8d0>
_images/Basic_Plots_25_1.png

Heatmap

In [15]:
import string
In [16]:
nrows = 20
ncols = 20
row_names = range(1970, 1970 + nrows)
col_names = [string.ascii_lowercase[i:i+5] for i in range(ncols)]
values = np.random.random((nrows, ncols))
In [17]:
df = pd.DataFrame(values, index = row_names, columns = col_names)
In [18]:
sns.heatmap(df)
pass
_images/Basic_Plots_30_0.png
In [19]:
cmap = sns.light_palette('red', as_cmap=True)
g = sns.clustermap(df, cmap = cmap)
plt.setp(g.ax_heatmap.yaxis.get_majorticklabels(), rotation=0)
pass
_images/Basic_Plots_31_0.png

Exercises

In [ ]:

Version information

In [20]:
%load_ext version_information
%version_information
The version_information extension is already loaded. To reload it, use:
  %reload_ext version_information
Out[20]:
SoftwareVersion
Python3.5.2 64bit [GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)]
IPython5.0.0
OSDarwin 15.6.0 x86_64 i386 64bit
Tue Aug 16 09:05:23 2016 EDT
In [ ]: