In [28]:
# Imports from __future__ in case we're running Python 2
from __future__ import division, print_function
from __future__ import absolute_import, unicode_literals

#pandas
import pandas as pd

# Our numerical workhorses
import numpy as np
import scipy.integrate

# Import pyplot for plotting
import matplotlib.pyplot as plt

# Seaborn, useful for graphics
import seaborn as sns

#pretty tables with plotly
import plotly
plotly.offline.init_notebook_mode() # run at the start of every notebook


# Import Bokeh modules for interactive plotting
import bokeh.io
import bokeh.mpl
import bokeh.plotting

# Magic function to make matplotlib inline; other style specs must come AFTER
%matplotlib inline

# This enables SVG graphics inline.  There is a bug, so uncomment if it works.
# %config InlineBackend.figure_formats = {'svg',}

# This enables high resolution PNGs. SVG is preferred, but has problems
# rendering vertical and horizontal lines
%config InlineBackend.figure_formats = {'png', 'retina'}

# JB's favorite Seaborn settings for notebooks
rc = {'lines.linewidth': 2, 
      'axes.labelsize': 18, 
      'axes.titlesize': 18, 
      'axes.facecolor': 'DFDFE5'}
sns.set_context('notebook', rc=rc)
sns.set_style('darkgrid', rc=rc)

# Set up Bokeh for inline viewing
bokeh.io.output_notebook()


BokehJS successfully loaded.

In [9]:
import beeswarm as bs

In [11]:
fname= '../input/frog_tongue_adhesion.csv'
df= pd.read_csv(fname, comment= '#')

In [13]:
df['impact force (mN)']= df['impact force (mN)'].astype(np.float64)

In [15]:
inds= df['impact force (mN)'] > 1000
df_big_force= df[inds]
df_big_force


Out[15]:
date ID trial number impact force (mN) impact time (ms) impact force / body weight adhesive force (mN) time frog pulls on target (ms) adhesive force / body weight adhesive impulse (N-s) total contact area (mm2) contact area without mucus (mm2) contact area with mucus / contact area without mucus contact pressure (Pa) adhesive strength (Pa)
0 2013_02_26 I 3 1205 46 1.95 -785 884 1.27 -0.290 387 70 0.82 3117 -2030
1 2013_02_26 I 4 2527 44 4.08 -983 248 1.59 -0.181 101 94 0.07 24923 -9695
2 2013_03_01 I 1 1745 34 2.82 -850 211 1.37 -0.157 83 79 0.05 21020 -10239
3 2013_03_01 I 2 1556 41 2.51 -455 1025 0.74 -0.170 330 158 0.52 4718 -1381
5 2013_03_01 I 4 2276 31 3.68 -592 969 0.96 -0.176 341 106 0.69 6676 -1737
7 2013_03_05 I 2 1928 46 3.11 -804 508 1.30 -0.285 246 178 0.28 7832 -3266
8 2013_03_05 I 3 2641 50 4.27 -690 491 1.12 -0.239 269 224 0.17 9824 -2568
9 2013_03_05 I 4 1897 41 3.06 -462 839 0.75 -0.328 266 176 0.34 7122 -1733
10 2013_03_12 I 1 1891 40 3.06 -766 1069 1.24 -0.380 408 33 0.92 4638 -1879
11 2013_03_12 I 2 1545 48 2.50 -715 649 1.15 -0.298 141 112 0.21 10947 -5064
12 2013_03_12 I 3 1307 29 2.11 -613 1845 0.99 -0.768 455 92 0.80 2874 -1348
13 2013_03_12 I 4 1692 31 2.73 -677 917 1.09 -0.457 186 129 0.31 9089 -3636
14 2013_03_12 I 5 1543 38 2.49 -528 750 0.85 -0.353 153 148 0.03 10095 -3453
15 2013_03_15 I 1 1282 31 2.07 -452 785 0.73 -0.253 290 105 0.64 4419 -1557
17 2013_03_15 I 3 2032 60 3.28 -652 486 1.05 -0.257 147 134 0.09 13784 -4425
18 2013_03_15 I 4 1240 34 2.00 -692 906 1.12 -0.317 364 260 0.28 3406 -1901
20 2013_03_19 II 1 1612 18 3.79 -655 3087 1.54 -0.385 348 15 0.96 4633 -1881
25 2013_03_21 II 2 1539 43 3.62 -664 741 1.56 -0.046 85 24 0.72 18073 -7802
28 2013_03_25 II 1 1453 72 3.42 -92 1652 0.22 -0.008 136 0 1.00 10645 -678
34 2013_04_03 II 1 1182 28 2.78 -522 1197 1.23 -0.118 281 0 1.00 4213 -1860

In [17]:
# Make a dictionary to rename columns
rename_dict = {'trial number' : 'trial',
        'contact area with mucus / contact area without mucus' : 'ca_ratio'}

# Rename the columns
df = df.rename(columns=rename_dict)
df = df.rename(columns={'impact force (mN)': 'impf'})

In [19]:
plt.plot(df.impf, 'o')
plt.margins(0.02)
plt.xlabel('order in DataFrame')
plt.ylabel('impace force (mN)')


Out[19]:
<matplotlib.text.Text at 0x10b880590>

In [20]:
#histogram plot; bins kwarg gives number of bars
_= plt.hist(df.impf, bins=20, normed= False)
plt.xlabel('impace force (mN)')
plt.ylabel('freq')


Out[20]:
<matplotlib.text.Text at 0x104800590>

In [33]:
#See how many data points we have per frog
data_matrix= (['FrogID', 'Sample No.'], 
         ['I', df.ID[df.ID=='I'].count()],
         ['II', df.ID[df.ID=='II'].count()],
         ['III', df.ID[df.ID=='III'].count()],
         ['IV', df.ID[df.ID=='IV'].count()])

table = FF.create_table(data_matrix)
py.iplot(table, filename='my_first_table')


High five! You successfuly sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~dangeles/0 or inside your plot.ly account where it is named 'my_first_table'
Out[33]:

In [36]:
#Compute mean impacts
mean_impact= df.groupby('ID').impf.mean()

#Use SD as error bar
std_impacts= df.groupby('ID').impf.std()

#Bar locs
x= np.arange(4)

#bar width
bar_width= 0.5

#bar labels
bar_labels= df.ID.unique()

#plot
plt.bar(x, mean_impact, yerr= std_impacts, width= bar_width,\
        align= 'center', error_kw= {'ecolor' : 'black'})
plt.grid(axis= 'x')
plt.xticks(x, bar_labels)
plt.ylabel('impact force (mN)')


Out[36]:
<matplotlib.text.Text at 0x10d9bd590>

In [37]:
yerr= df.groupby('ID').impf.std()

#plot
ax= df.groupby('ID').impf.mean().plot(kind= 'bar', yerr= yerr)
ax.grid(axis= 'x')
ax.set_ylabel('Impact Force (mN)')


Out[37]:
<matplotlib.text.Text at 0x10b2bb3d0>

In [39]:
#Now with seaborn
ax= sns.boxplot(x= 'ID', y= 'impf', data= df, width= 0.5)

#relabel axes
ax.set_xlabel('Frog')
ax.set_ylabel('Impact Force (mN)')


Out[39]:
<matplotlib.text.Text at 0x11494f390>

In [41]:
#Make a jitter plot
ax= sns.stripplot(x= 'ID', y= 'impf', data= df, jitter= True, alpha= 0.6)

#Relabel axes
ax.set_xlabel('Frog')
ax.set_ylabel('Impact Force (mN)')


Out[41]:
<matplotlib.text.Text at 0x10c924810>

In [44]:
#Use seaborn to make a box plot
ax= sns.boxplot(x= 'ID', y= 'impf', data= df, width= 0.5)

#make a jitter plot
ax= sns.stripplot(x= 'ID', y= 'impf', data= df, jitter= True, marker= 'o',\
                 alpha= 0.8, edgecolor= 'white')

#relabel axes
ax.set_xlabel('Frog')
ax.set_ylabel('Impact Force (mN)')


Out[44]:
<matplotlib.text.Text at 0x1149293d0>

In [47]:
list_of_impfs= [df.impf[df.ID== 'I'], df.impf[df.ID=='II'], \
               df.impf[df.ID=='III'], df.impf[df.ID=='IV']]

#generate beeswarm
_ = bs.beeswarm(list_of_impfs, labels= ['I', 'II', 'III', 'IV'])
plt.grid(axis= 'x')
plt.ylabel('impact force (mN)')


Out[47]:
<matplotlib.text.Text at 0x1116f2210>

In [52]:
#get list of dates, use unique
dates= df.date.unique()
n_dates= len(dates)

#assign colors to date names with colormap between 0 and 1
colors= []
for i in range(n_dates):
    colors.append(plt.cm.Set1(float(i)/float(n_dates)))

#make a dictionary of dates and colors
color_dict= dict(zip(dates, colors))

#sort by ID to ensure color labels are in correct order in beeswarm plot
df_sorted= df.sort_values('ID')

#color each point by the date it was measured in and make a new colors array
colors= []
#this is why we sorted above^^^
for i in range(len(df)):
    colors.append(color_dict[df_sorted.date.iloc[i]])
    
#Make beeswarm
bs_plot, ax= bs.beeswarm(list_of_impfs, labels= ['I', 'II', 'III','IV'],\
                        col= colors)



In [55]:
#plot adhesive force vs impact force
df.rename(columns= {'adhesive force (mN)' : 'adhf'}, inplace= True)

#Plot adhesive force vs impact force
plt.plot(df.impf, df.adhf, 'o', alpha= 0.6)
plt.xlabel('impact force (mN)')
plt.ylabel('adhesive force (mN)')


Out[55]:
<matplotlib.text.Text at 0x10c178090>