import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt # set up to have plots appear in the ipython notebook: %matplotlib inline sns.set_style("white") # sets the style of seaborn plots # import the dataset csv file into a Pandas dataframe object: sleep = pd.read_csv('../Datasets/sleep.txt', sep='\t') # the "\t" means it's a tab separated file. whos sleep.mean() # look at what the "sleep" dataframe contains: sleep.info() # let's look at the first few lines of the sleep dataset using `head`: sleep.head() # we can also use the "describe" method to summarise some information in our variables: sleep.describe() sleep = sleep.dropna(axis=0) # drop on axis 0 (i.e. rows) sleep.info() sleep.head() heights = pd.DataFrame({'person': ['Jim', 'Sally', 'Meg'], 't1': [120, 123, 118], 't2': [152, 147, 153]}) heights tidy = pd.melt(heights, id_vars='person', var_name='time', value_name='height') tidy # simple histogram of Total sleep: vec = sleep['TotalSleep'] g = sns.distplot(vec) sns.despine(ax=g, offset=10); # the jointplot function can be used to set up a subplot grid: g = sns.jointplot("BodyWt", "TotalSleep", sleep); sleep['Species'][sleep['BodyWt']==sleep['BodyWt'].min()] sleep['Species'][sleep['BodyWt']==sleep['BodyWt'].max()] sleep['BodyWt'].describe() # use the describe method on the variable BodyWt sleep['BodyWt'] # prints the column of "bodyweight" variable. sleep['BodyWt'][0:3] # the first three rows of the bodyweight variable # Boolean indexing. All bodyweights of animals with less than 6 hours of sleep: sleep['BodyWt'][sleep['TotalSleep'] < 6] sleep['log_BodyWt'] = np.log(sleep['BodyWt']) sleep['log_BodyWt'].describe() sleep.info() g = sns.jointplot("log_BodyWt", "TotalSleep", sleep) g = sns.violinplot(sleep['TotalSleep'], groupby=sleep['Danger']) sns.despine(ax=g, offset=10); sleep['Species'][sleep['Danger']==1] sleep['Species'][sleep['Danger']==5] g = sns.FacetGrid(sleep, col="Danger", col_wrap=3) g.map(plt.scatter, 'log_BodyWt', 'TotalSleep'); from IPython.core.display import HTML def css_styling(): styles = open("../custom_style.css", "r").read() return HTML(styles) css_styling()