Skip to content

Instantly share code, notes, and snippets.

@sampsyo
Created July 28, 2010 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sampsyo/495211 to your computer and use it in GitHub Desktop.
Save sampsyo/495211 to your computer and use it in GitHub Desktop.
plot time data from runmeter with pychart
"""Plot time data from a Runmeter <http://www.abvio.com/runmeter/>
data export."""
import csv
import sys
from collections import defaultdict
import datetime
def runmeter_read(path):
doc = csv.reader(open(path))
doc.next() # skip header row
routes = defaultdict(list)
for row in doc:
route, date, secs = row[0], row[2], row[4]
if not route: # "totals" row
continue
date = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
secs = int(secs)
routes[route].append((date, secs))
return routes
def plot_for_route(times, route_name):
from pychart import line_plot, category_coord, tick_mark
data = [(date.toordinal(), secs) for (date, secs) in times]
return line_plot.T(
data = data,
tick_mark = tick_mark.Circle(size=2),
label = route_name,
)
def draw_plots(plots, outfile="runmeter.pdf"):
from pychart import canvas, area, axis
# Find universal date extrema.
min_date, max_date = None, None
for plot in plots:
tmin, tmax = plot.get_data_range('X')
if min_date is None or tmin < min_date:
min_date = tmin
if max_date is None or tmax > max_date:
max_date = tmax
date_range = max_date - min_date
def format_date(ordinal):
dt = datetime.date.fromordinal(int(ordinal))
return "/a60{}" + dt.strftime("%m-%d-%y")
def format_time(secs):
secs = int(secs)
mins = secs // 60
secs = secs % 60
if mins:
out = "%i:%02i" % (mins, secs)
else:
out = str(secs) + "s"
return out
can = canvas.init(outfile)
ar = area.T(
x_range = (min_date-1, max_date+1),
x_axis = axis.X(
label = "Date",
format = format_date,
minor_tic_interval = 1,
tic_interval = date_range//7,
),
y_axis = axis.Y(
label = "Time",
format = format_time,
),
size = (250, 110),
)
for plot in plots:
ar.add_plot(plot)
ar.draw()
if __name__ == '__main__':
routes = runmeter_read(sys.argv[1])
plots = []
for name, times in routes.items():
plots.append(plot_for_route(times, name))
draw_plots(plots)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment