In [1]:
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnnotationBbox, OffsetImage
from matplotlib._png import read_png
import pandas as pd
import numpy as np
import datetime
name = '2017-05-05'
df = pd.read_excel('/Users/mkudija/Desktop/Kudija_Chart/Data/'+name+'.xlsx')
df = df.fillna('')
print('Cycle Length: '+str(df.shape[0])+' days')
#df.tail()
In [2]:
def plot_chart(df, name):
# function to plot sticker images
def plot_image(x, y, path, zoom):
arr_hand = read_png(path)
imagebox = OffsetImage(arr_hand, zoom=zoom)
xy = [x, y]
ab = AnnotationBbox(imagebox, xy, xybox=(0, 0), xycoords='data', boxcoords="offset points", frameon=False)
ax.add_artist(ab)
# configure axes
fig = plt.gcf()
fig.clf()
plt.figure(figsize=(7*5/2,1.85), dpi=150)
ax = plt.subplot(111)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.axis('off')
# plot grid
plt.text(6.5,.15,'BSE',ha='center',size=7,weight='bold')
linewidth1=.5
linewidth2=2.5
yrange = [0,2,3.75,4.7]
for x in range(0,7*6+1):
for y in yrange:
if x % 7 == 0:
plt.plot([x,x],[0,y],'-k',linewidth=linewidth2)
else:
plt.plot([x,x],[0,y],'-k',linewidth=linewidth1)
if y==yrange[1]:
plt.plot([0,x],[y,y],'-k',linewidth=linewidth1)
else:
plt.plot([0,x],[y,y],'-k',linewidth=linewidth2)
if x<42: plt.text(x+.5, 4.1, x+1,ha='center',size=9) # plot day number
# plot day number, date, and observation text
for row in range(0,df.shape[0]):
date = df.ix[row,'Date']
intercourse = df.ix[row,'Intercourse']
discharge = df.ix[row,'Discharge'].rstrip()
plt.text(row+.5,1.5,str(date.month)+'/'+str(date.day),ha='center',name='Verdana',size=6)
if 'I' in intercourse:
if row==6: plt.text(row+.5,.5,intercourse,ha='center',name='Verdana',size=7)
else: plt.text(row+.5,.1,intercourse,ha='center',name='Verdana',size=7)
if len(discharge)<5:
plt.text(row+.5,1,discharge,ha='center',name='Verdana',size=7)
else:
space = discharge.find(' ')
plt.text(row+.5,1,discharge[0:space],ha='center',name='Verdana',size=7)
row2 = discharge[space+1:]
if len(row2)<6:
plt.text(row+.5,.55,row2,ha='center',name='Verdana',size=7)
else:
space = row2.find(' ')
plt.text(row+.5,.55,row2[0:space],ha='center',name='Verdana',size=7)
plt.text(row+.5,.35,row2[space:],ha='center',name='Verdana',size=7)
#call plot_image function
sticker = str(df.ix[row,'Sticker']).lower()
if len(sticker)>0: path = 'Stickers/'+sticker+'.png'
else: path = 'Stickers/blank.png'
plot_image(row+.5, 2.85, path, .125)
# save & show plot
#plt.draw()
plt.savefig('/Users/mkudija/Desktop/Kudija_Chart/Charts/'+name+'.png', dpi=150, bbox_inches='tight')
#plt.show()
# -------------------------------------------------------
plot_chart(df, name)
print('Done.')
In [ ]: