Mercurial > hg > numerics
annotate numerics/bar.py @ 115:7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 15 Mar 2015 17:03:38 -0700 |
parents | f9900883db2e |
children | fe820a3afa48 |
rev | line source |
---|---|
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
3 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
4 """ |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
5 bar charts using bokeh |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
6 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
7 See: |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
8 - http://bokeh.pydata.org/tutorial/solutions/gallery/olympics.html |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
9 """ |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
10 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
11 # imports |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
12 import argparse |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
13 import sys |
115
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
14 from .data import transpose |
112 | 15 from .manipulate import ManipulationParser |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
16 from bokeh.charts import Bar |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
17 from bokeh.plotting import * |
115
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
18 from collections import OrderedDict |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
19 |
107
19a5c2fb52bb
add transpose functionality
Jeff Hammel <k0scist@gmail.com>
parents:
104
diff
changeset
|
20 __all__ = ['bar_chart', 'BarChartParser', 'main'] |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 |
115
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
22 |
113
f9900883db2e
hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents:
112
diff
changeset
|
23 def bar_chart(data, output): |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
24 """create a bar chart""" |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
25 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
26 bar = Bar(data, tools="pan,wheel_zoom,box_zoom,reset,resize") |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
27 bar.filename(output) |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
28 bar.width(len(data)*50) |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
29 bar.show() |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
30 |
107
19a5c2fb52bb
add transpose functionality
Jeff Hammel <k0scist@gmail.com>
parents:
104
diff
changeset
|
31 |
112 | 32 class BarChartParser(ManipulationParser): |
104 | 33 """command line options parser for bar charts""" |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
34 # TODO: upstream to PlotParser |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
35 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
36 def __init__(self, **kwargs): |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
37 kwargs.setdefault('description', __doc__) |
112 | 38 ManipulationParser.__init__(self, **kwargs) |
104 | 39 self.add_argument('-t', '--title', dest='title', |
40 help="plot title") | |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
41 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
42 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
43 def main(args=sys.argv[1:]): |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
44 """CLI""" |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
45 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
46 # parse command line |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
47 parser = BarChartParser() |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
48 options = parser.parse_args(args) |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
49 |
112 | 50 # process data |
51 data = parser.typed_data() | |
52 | |
115
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
53 # ensure a mapping is given |
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
54 if len(data) == 1: |
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
55 data.insert(0, range(len(data[-1]))) |
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
56 if len(data) != 2: |
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
57 parser.error("A mapping is required") |
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
58 mapping = OrderedDict(transpose(data)) |
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
59 |
113
f9900883db2e
hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents:
112
diff
changeset
|
60 # generate bar chart |
115
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
61 bar_chart(mapping, options.output) |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
62 |
113
f9900883db2e
hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents:
112
diff
changeset
|
63 # BBB keeping this around for reference; |
f9900883db2e
hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents:
112
diff
changeset
|
64 # we should probably move to a better parsing system at some point |
f9900883db2e
hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents:
112
diff
changeset
|
65 # parse file |
f9900883db2e
hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents:
112
diff
changeset
|
66 # data = pd.read_csv(options.input, header=None, names=options.columns, index_col=0) |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
67 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
68 if __name__ == '__main__': |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
69 main() |