Created
March 11, 2014 16:18
-
-
Save mattjj/9489192 to your computer and use it in GitHub Desktop.
milk study plots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
from matplotlib import rc, rcParams | |
np.random.seed(0) | |
rc('font',**{'family':'sans-serif','sans-serif':['Arial'],'size':16}) | |
def myboxplot(boxdata,pointdata): | |
def getboxbounds(df): | |
df = pd.DataFrame(df) | |
return [(np.min(d), np.min(d), np.median(d), np.max(d), np.max(d)) | |
for _, d in df.iteritems()] | |
### plot boxes | |
bp = plt.boxplot(getboxbounds(boxdata),sym='') # sym='' means no outliers | |
plt.setp(bp['boxes'], color='black') | |
plt.setp(bp['medians'], color='black') | |
plt.setp(bp['whiskers'], visible=False) | |
### plot points | |
for idx, (name, col) in enumerate(pointdata.iteritems()): | |
N = len(col) | |
xvals = idx + 1 + np.linspace(-0.1,0.1,N,endpoint=True)[np.random.permutation(N)] | |
plt.plot(xvals,col,'ko') | |
def makefig(df,names): | |
plt.figure(figsize=(8,6)) # figure size in inches | |
milk = df[df['Type'] == 1] # "milk" rows are where Type is 1 | |
replacer = df[df['Type'] == 2] # "replacer" rows are where Type is 2 | |
myboxplot(milk[names],replacer[names]) | |
plt.xticks(np.arange(1,len(names)+1),names) | |
plt.ylabel('Concentration (g/1,000 kcal)',labelpad=15) | |
plt.grid('off') | |
plt.grid(axis='y') | |
ymin, ymax = plt.ylim() | |
plt.ylim(ymin-0.05*(ymax-ymin),ymax) | |
ax = plt.gca() | |
ax.set_axisbelow(True) | |
ax.yaxis.tick_left() | |
ax.xaxis.tick_bottom() | |
if __name__ == '__main__': | |
# load the data into a Pandas dataframe named df | |
df = pd.read_excel('milk study data for matt.xlsx','Sheet1') | |
# rename the columns to match the figures | |
df.columns = [ | |
'Sample ID','Type','Protein','Carbohydrate','Fat','Calcium', | |
'Phosphorus','Ca:P ratio','ALA','ARA','EPA','DHA', | |
] | |
makefig(df,['Protein','Fat','Carbohydrate']) | |
plt.yticks(10*np.arange(2,12)) | |
plt.savefig('figure1.pdf') | |
makefig(df,['Calcium','Phosphorus','Ca:P ratio']) | |
plt.savefig('figure2.pdf') | |
makefig(df,['ALA','ARA','EPA','DHA']) | |
plt.savefig('figure3.pdf') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment