July 21, 2014

How to Visualise Data

Amongst my friends, one of the most dreaded subject lines in an email from me is, "Thought you might be interested in...". This is the result of a python script that lets me share links with you. Over the weekend, I greatly enhanced it, adding the ability to view, remove and visualise whatever links I've sent in a histogram. It's the latter piece of code that is mirrored here:

    import pandas as pd
    from ggplot import geom_bar, aes, ggplot, ggsave, ggtitle
    from imgur.factory import factory

    today = links[links['Time'] > int(datetime.date.today().strftime('%s')) - 1]
    logging.debug(today.to_string())
    today['Hour'] = [datetime.datetime.fromtimestamp(t).strftime('%H') for t in today['Time']]
    logging.debug(today['Hour'].to_string())
    p = ggplot(today, aes(x='Hour')) + geom_bar() + ggtitle('Links shared by hour of day today')
    with tempfile.NamedTemporaryFile() as fileout:
        ggsave(p, fileout.name)
        imgur_key = u'4feb29d00face5bc1b9dae536e15c373'
        req = factory.build_request_upload_from_path(fileout.name)
        res = imgur.retrieve(req)
        print('Image may be viewed at {}'.format(res['link']))

No comments:

Post a Comment