comparison numerics/bar.py @ 103:067aa27050a3

limping along towards bar charts
author Jeff Hammel <k0scist@gmail.com>
date Sun, 15 Mar 2015 08:28:42 -0700
parents
children 889728b8d359
comparison
equal deleted inserted replaced
102:1b0854ee78e0 103:067aa27050a3
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 bar charts using bokeh
6
7 See:
8 - http://bokeh.pydata.org/tutorial/solutions/gallery/olympics.html
9 """
10
11 # imports
12 import argparse
13 import csv
14 import os
15 import pandas as pd
16 import subprocess
17 import sys
18 from bokeh.charts import Bar
19 from bokeh.plotting import *
20 from collections import OrderedDict
21
22 __all__ = ['bar_chart', 'main']
23
24 def bar_chart(data, output, title=None):
25 """create a bar chart"""
26
27 bar = Bar(data, tools="pan,wheel_zoom,box_zoom,reset,resize")
28 bar.filename(output)
29 bar.width(len(data)*50)
30 bar.show()
31
32 class BarChartParser(CSVParser):
33 # TODO: upstream to PlotParser
34
35 def __init__(self, **kwargs):
36 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
37 kwargs.setdefault('description', __doc__)
38 CSVParser.__init__(self, **kwargs)
39 parser.add_argument('-t', '--title', dest='title',
40 help="plot title")
41
42
43 def main(args=sys.argv[1:]):
44 """CLI"""
45
46 # parse command line
47 parser = BarChartParser()
48 options = parser.parse_args(args)
49
50 # parse file
51 data = pd.read_csv(options.input, header=None, names=options.columns, index_col=0)
52
53 # generate bar chart
54 bar_chart(data, options.output, title=options.title or options.input.name)
55
56 if __name__ == '__main__':
57 main()