In [1]:
! pip install Seaborn


Collecting Seaborn
  Downloading seaborn-0.8.tar.gz (178kB)
    100% |████████████████████████████████| 184kB 2.6MB/s ta 0:00:01
Collecting scipy (from Seaborn)
  Downloading scipy-0.19.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (16.2MB)
    100% |████████████████████████████████| 16.2MB 48kB/s  eta 0:00:01
Requirement already satisfied: numpy>=1.8.2 in /Users/bergeric/miniconda3/lib/python3.6/site-packages (from scipy->Seaborn)
Building wheels for collected packages: Seaborn
  Running setup.py bdist_wheel for Seaborn ... done
  Stored in directory: /Users/bergeric/Library/Caches/pip/wheels/59/10/63/af632c81af65a5acca29220158270dc767927a81650acffcb5
Successfully built Seaborn
Installing collected packages: scipy, Seaborn
Successfully installed Seaborn-0.8 scipy-0.19.1

In [2]:
import numpy as np
from numpy.random import randn
import pandas as pd
from pandas import Series, DataFrame

import matplotlib.pyplot as plt
from matplotlib import rcParams
import seaborn as sb

In [3]:
%matplotlib inline 
rcParams['figure.figsize'] = 5,4
sb.set_style('whitegrid')

Plotting a line chart in matplotlib


In [5]:
x=range(1,10)
y=[1,2,3,4,0,4,3,2,1]
plt.plot(x,y)


Out[5]:
[<matplotlib.lines.Line2D at 0x10fcff0b8>]

Plotting a line chart from a Pandas object


In [6]:
# address = some data set 
# cars = pd.read_csv(address)
# cars.columns = ['car_names','mpg','cyl','disp','hp','drat','wt','qsec','vs','am',gear',carb']
#mpg = cars['mpg']
#mpg.plot()

Creating bar charts:


In [7]:
plt.bar(x,y)


Out[7]:
<Container object of 9 artists>

In [8]:
#Creating bar chart from pandas object
#mpg.plot(kind='bar')

Creating a pie chart


In [9]:
x=[1,2,3,4,0.5]
plt.pie(x)
plt.show()



In [10]:
#plt.savefig('pie_chart.jpeg') Saves plot as jpeg in wd
#plt.show()

Defining elements of a plot

Defining axes, ticks, and grids


In [11]:
x=range(1,10)
y=[1,2,3,4,0,4,3,2,1]

fig=plt.figure()
ax = fig.add_axes([.1, .1,1,1])
ax.plot(x,y)


Out[11]:
[<matplotlib.lines.Line2D at 0x11000da90>]

In [12]:
fig = plt.figure()
ax = fig.add_axes([.1, .1,1,1])

ax.set_xlim([1,9])
ax.set_ylim([0,5])

ax.set_xticks([0,1,2,4,5,6,8,9,10])
ax.set_yticks([0,1,2,3,4,5])

ax.plot(x,y)


Out[12]:
[<matplotlib.lines.Line2D at 0x1102d1e10>]

In [14]:
fig = plt.figure()
ax = fig.add_axes([.1, .1,1,1])

ax.set_xlim([1,9])
ax.set_ylim([0,5])

ax.grid() #seems like this takes away grid? different than tutorial
ax.plot(x,y)


Out[14]:
[<matplotlib.lines.Line2D at 0x110433e80>]

In [15]:
fig = plt.figure()
fig, (ax1,ax2)=plt.subplots(1,2)

ax1.plot(x)
ax2.plot(x,y)


Out[15]:
[<matplotlib.lines.Line2D at 0x10fd53c88>]
<matplotlib.figure.Figure at 0x110433e48>

Plot Formatting

Defining plot color, customizing line styles, setting marker styles


In [16]:
x = range(1,10)
y= [1,2,3,4,0.5,4,3,2,1]

plt.bar(x,y)


Out[16]:
<Container object of 9 artists>

In [17]:
wide=[0.5,0.5,0.5,0.9,0.9,0.5,0.5,0.9,0.9]
color = ['salmon']
plt.bar(x, y, width=wide, color=color, align='center')


Out[17]:
<Container object of 9 artists>

Note: with pandas objects

color_theme = ['darkgray','lightsalmon','powderblue'] df.plot(color=color_theme)


In [21]:
z = [1,2,3,4,0.5]

color_theme = ['#A9A9A9', '#FFA07A', '#B0E0E6','#FFE4CA','#BDB76B'] #hex codes
plt.pie(z, colors = color_theme)
plt.show()



In [22]:
#line styles
x1= range(0,10)
y1=[10,9,8,7,6,5,4,3,2,1]

plt.plot(x,y,ls = 'steps', lw = 5)
plt.plot(x1,y1, ls = '--', lw = 10)


Out[22]:
[<matplotlib.lines.Line2D at 0x110ad3470>]

In [23]:
#plot markers
plt.plot(x,y,marker = '1', mew=20)
plt.plot(x1,y1, marker = '+', mew=15)


Out[23]:
[<matplotlib.lines.Line2D at 0x110adad30>]

Create labels and annotations

labeling plot features, adding a legend, annotating plot


In [24]:
#functional method
x= range(1,10)
y=[1,2,3,4,0.5,4,3,2,1]
plt.bar(x,y)

plt.xlabel('your x-axis label')
plt.ylabel('your y-axis label')


Out[24]:
<matplotlib.text.Text at 0x110890390>

In [25]:
z= [1,2,3,4,0.5]
veh_type = ['bicycle', 'motorbike','car','van','stroller']
plt.pie(z, labels= veh_type)
plt.show()



In [26]:
#object oriented has ways of doing this too
#uses cars dataset
#didn't copy down everything

#fig = plt.figure()
#ax = fig.add_axes([])
#mpg.plot()

#ax.set_xticks(range(32))

#ax.set_xticklabels()
#ax.set_title('Title goes here')
#ax.set_xlabel('car names')
#ax.set_ylabel('miles/gal')

#ax.legend(loc='best')

In [27]:
#add legend
plt.pie(z)
plt.legend(veh_type, loc='best')
plt.show()



In [28]:
#annotate
#object oriented: ax.annotate('Toyota Corolla', xy=(19,33,9), xytext=(21,35), 
    #arrowprops = dict(facecolor='black',shrink=0.05))

Time series visualizations


In [29]:
#address = 'address'
#df = pd.read_csv(address, index_col='Order Date', parse_dates=True)
#df.head()
#df2 = df.sample(n=100, random_state=25, axis=0)
#plt.xlabel('Order Date')
#plt.ylabel('Order Quantity')
#plt.title('Superstore Sales')

#df2['Order Quantity'].plot()

Histograms, box plots, and scatter plots


In [30]:
from pandas.tools.plotting import scatter_matrix

In [31]:
#pandas dataset import
#mpg.plot(kind='hist')
# or plt.hist(mpg) with plt.show()

#with seaborn
#sb.distplot(mpg)

#scatterplots
#cars.plot(kind='scatter',x='hp',y='mpg',c=['darkgray'], s=150)
#sb.regplot(x='hp', y='mpg', data=cars, scatter=True)

#seaborn automatically creates trend line

#sb.pairplot(cars)

#get subset of data using dataframes:
#cars_df = pd.DataFrame((cars.ix[:,(1,3,4,6)].values), columns = ['mpg', 'disp','hp','wt'])

In [32]:
#boxplots

#cars.boxplot(column='mpg', by='am')

#in seaborn
#sb.boxplot(x='am', y='mpg', data=cars, palette='hls')

In [ ]: