File:Running times 41Berlin marathon.svg

Uploaded by Geek3
Upload date 2014-09-28T21:11:32Z
MIME type image/svg+xml
Dimensions 400 × 280 px
File size 42.1 KB

Summary

Description
English: Histogram of running times of all successful participants of the 41. Berlin Marathon from 2h to 7h. Male and female participants are drawn as two separate curves.
Date
Source Own work
Author Geek3
SVG development
InfoField
 The SVG code is valid.
 This plot was created with Matplotlib.
Source code
InfoField

Python code

Python source code
#!/usr/bin/python
# -*- coding: utf8 -*-

import numpy as np
import pylab as pl
import urllib
import StringIO
import os
import re
from lxml import etree


website = 'http://results.scc-events.com/2014/?event=MAL&pid=list&num_results=100'


def fetch_times_from_page(pagename):
    page = urllib.urlopen(pagename)
    page_text = page.read()
    page_xml = etree.parse(StringIO.StringIO(page_text), etree.HTMLParser())
    
    times_txt = []
    for element in page_xml.iter('tr'):
        if len(element) == 10:
            t_text = element[8].text
            match = re.match('\A(\d{2}):(\d{2}):(\d{2})\Z', t_text)
            if match == None:
                continue
            hms = match.groups()
            if len(hms) == 3:
                times_txt.append(t_text)
    return times_txt


def fetch_list(pagename):
    print 'fetching data from website'
    times_txt = []
    i = 0
    while True:
        print 'fetching set', i
        new_times = fetch_times_from_page(pagename + '&page=' + str(i))
        if len(new_times) == 0:
            print 'no more new entries on page', i
            break
        else:
            times_txt = times_txt + new_times
        i += 1
    return times_txt


times = {}
for gender in ['M', 'W']:

    fname = 'marathon_' + gender + '.txt'
    if not os.path.isfile(fname):
        # have no local file -> need to download data
        pagename = website + '&search[se' + 'x]=' + gender
        times_list = fetch_list(pagename)
        f = open(fname, 'w')
        f.write('\n'.join(times_list))
        f.close()
    
    f = open(fname, 'r')
    times[gender] = []
    for line in f.readlines():
        match = re.match('\A(\d{2}):(\d{2}):(\d{2})\Z', line.strip())
        if match == None:
            continue
        hms = match.groups()
        if len(hms) == 3:
            t = float(hms[0]) + float(hms[1]) / 60. + float(hms[2]) / 3600.
            times[gender].append(t)
    f.close()
    
w = 0.2
histM, edg = np.histogram(times['M'], np.arange(2, 7+w, w), density=True)
histW, edg = np.histogram(times['W'], np.arange(2, 7+w, w), density=True)
t_arr = w/2 + edg[:-1]

pl.figure(figsize=(50/9., 35/9.))
pl.plot(t_arr, histM, 'bo-', label='m')
pl.plot(t_arr, histW, 'ro-', label='w')
pl.xlim(2, 7)
pl.xlabel('t [h]')
pl.ylabel('P')
pl.title('41. Berlin Marathon running times')
pl.grid(True)
pl.legend(loc='upper right')
pl.tight_layout()
pl.savefig('running_times_41Berlin_marathon.svg')

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution
This file is licensed under the Creative Commons Attribution 3.0 Unported license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

Captions

Add a one-line explanation of what this file represents

Items portrayed in this file

depicts

28 September 2014

43,072 byte

image/svg+xml

5bc0d6984a095367ed06e17f3f98ddf289261088