134 lines
2.7 KiB
Python
134 lines
2.7 KiB
Python
from matplotlib.ticker import MultipleLocator,FuncFormatter,NullFormatter
|
|
import matplotlib.pyplot as plt
|
|
|
|
import numpy as np
|
|
|
|
def format_pace_tick(x,pos=None):
|
|
min=int(x/60)
|
|
sec=int(x-min*60.)
|
|
sec_str=str(sec).zfill(2)
|
|
template='%d:%s'
|
|
return template % (min,sec_str)
|
|
|
|
def format_pace(x,pos=None):
|
|
if isinf(x) or isnan(x):
|
|
x=0
|
|
|
|
min=int(x/60)
|
|
sec=(x-min*60.)
|
|
|
|
str1 = "{min:0>2}:{sec:0>4.1f}".format(
|
|
min = min,
|
|
sec = sec
|
|
)
|
|
|
|
return str1
|
|
|
|
def format_time(x,pos=None):
|
|
|
|
|
|
min = int(x/60.)
|
|
sec = int(x-min*60)
|
|
|
|
str1 = "{min:0>2}:{sec:0>4.1f}".format(
|
|
min=min,
|
|
sec=sec,
|
|
)
|
|
|
|
return str1
|
|
|
|
def format_dist_tick(x,pos=None):
|
|
km = x/1000.
|
|
template='%6.3f'
|
|
return template % (km)
|
|
|
|
def format_time_tick(x,pos=None):
|
|
hour=int(x/3600)
|
|
min=int((x-hour*3600.)/60)
|
|
min_str=str(min).zfill(2)
|
|
template='%d:%s'
|
|
return template % (hour,min_str)
|
|
|
|
def y_axis_range(ydata,miny=0,padding=.1,ultimate=[-1e9,1e9]):
|
|
|
|
# ydata must by a numpy array
|
|
|
|
ymin = np.ma.masked_invalid(ydata).min()
|
|
ymax = np.ma.masked_invalid(ydata).max()
|
|
|
|
|
|
yrange = ymax-ymin
|
|
yrangemin = ymin
|
|
yrangemax = ymax
|
|
|
|
|
|
|
|
if (yrange == 0):
|
|
if ymin == 0:
|
|
yrangemin = -padding
|
|
else:
|
|
yrangemin = ymin-ymin*padding
|
|
if ymax == 0:
|
|
yrangemax = padding
|
|
else:
|
|
yrangemax = ymax+ymax*padding
|
|
else:
|
|
yrangemin = ymin-padding*yrange
|
|
yrangemax = ymax+padding*yrange
|
|
|
|
if (yrangemin < ultimate[0]):
|
|
yrangemin = ultimate[0]
|
|
|
|
if (yrangemax > ultimate[1]):
|
|
yrangemax = ultimate[1]
|
|
|
|
|
|
|
|
return [yrangemin,yrangemax]
|
|
|
|
def mkplot(row,title):
|
|
df = row.df
|
|
|
|
t = df.ix[:,' ElapsedTime (sec)']
|
|
p = df.ix[:,' Stroke500mPace (sec/500m)']
|
|
hr = df.ix[:,' HRCur (bpm)']
|
|
end_time = int(df.ix[df.shape[0]-1,'TimeStamp (sec)'])
|
|
|
|
fig, ax1 = plt.subplots(figsize=(5,4))
|
|
|
|
ax1.plot(t,p,'b-')
|
|
ax1.set_xlabel('Time (h:m)')
|
|
ax1.set_ylabel('(sec/500)')
|
|
|
|
yrange = y_axis_range(df.ix[:,' Stroke500mPace (sec/500m)'],
|
|
ultimate = [85,190])
|
|
plt.axis([0,end_time,yrange[1],yrange[0]])
|
|
|
|
ax1.set_xticks(range(1000,end_time,1000))
|
|
ax1.set_yticks(range(185,90,-10))
|
|
ax1.set_title(title)
|
|
plt.grid(True)
|
|
majorFormatter = FuncFormatter(format_pace_tick)
|
|
majorLocator = (5)
|
|
timeTickFormatter = NullFormatter()
|
|
|
|
ax1.yaxis.set_major_formatter(majorFormatter)
|
|
|
|
for tl in ax1.get_yticklabels():
|
|
tl.set_color('b')
|
|
|
|
ax2 = ax1.twinx()
|
|
ax2.plot(t,hr,'r-')
|
|
ax2.set_ylabel('Heart Rate',color='r')
|
|
majorTimeFormatter = FuncFormatter(format_time_tick)
|
|
majorLocator = (15*60)
|
|
ax2.xaxis.set_major_formatter(majorTimeFormatter)
|
|
ax2.patch.set_alpha(0.0)
|
|
for tl in ax2.get_yticklabels():
|
|
tl.set_color('r')
|
|
|
|
plt.subplots_adjust(hspace=0)
|
|
|
|
return fig
|
|
|