diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/numerics/bar.py	Sun Mar 15 08:28:42 2015 -0700
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+bar charts using bokeh
+
+See:
+- http://bokeh.pydata.org/tutorial/solutions/gallery/olympics.html
+"""
+
+# imports
+import argparse
+import csv
+import os
+import pandas as pd
+import subprocess
+import sys
+from bokeh.charts import Bar
+from bokeh.plotting import *
+from collections import OrderedDict
+
+__all__ = ['bar_chart', 'main']
+
+def bar_chart(data, output, title=None):
+    """create a bar chart"""
+
+    bar = Bar(data, tools="pan,wheel_zoom,box_zoom,reset,resize")
+    bar.filename(output)
+    bar.width(len(data)*50)
+    bar.show()
+
+class BarChartParser(CSVParser):
+    # TODO: upstream to PlotParser
+
+    def __init__(self, **kwargs):
+        kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
+        kwargs.setdefault('description', __doc__)
+        CSVParser.__init__(self, **kwargs)
+        parser.add_argument('-t', '--title', dest='title',
+                            help="plot title")
+
+
+def main(args=sys.argv[1:]):
+    """CLI"""
+
+    # parse command line
+    parser = BarChartParser()
+    options = parser.parse_args(args)
+
+    # parse file
+    data = pd.read_csv(options.input, header=None, names=options.columns, index_col=0)
+
+    # generate bar chart
+    bar_chart(data, options.output, title=options.title or options.input.name)
+
+if __name__ == '__main__':
+    main()