diff options
Diffstat (limited to 'plot/style.py')
-rw-r--r-- | plot/style.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/plot/style.py b/plot/style.py new file mode 100644 index 0000000..7e964b0 --- /dev/null +++ b/plot/style.py | |||
@@ -0,0 +1,62 @@ | |||
1 | from collections import namedtuple | ||
2 | import matplotlib.pyplot as plot | ||
3 | |||
4 | class Style(namedtuple('SS', ['marker', 'line', 'color'])): | ||
5 | def fmt(self): | ||
6 | return self.marker + self.line + self.color | ||
7 | |||
8 | class StyleMap(object): | ||
9 | '''Maps configs (dicts) to specific line styles.''' | ||
10 | DEFAULT = Style('.', '-', 'k') | ||
11 | |||
12 | def __init__(self, col_list, col_values): | ||
13 | '''Assign (some) columns in @col_list to fields in @Style to vary, and | ||
14 | assign values for these columns to specific field values.''' | ||
15 | self.value_map = {} | ||
16 | self.field_map = {} | ||
17 | |||
18 | for field, values in self.__get_all()._asdict().iteritems(): | ||
19 | next_column = col_list.pop(0) | ||
20 | value_dict = {} | ||
21 | |||
22 | for value in sorted(col_values[next_column]): | ||
23 | value_dict[value] = values.pop(0) | ||
24 | |||
25 | self.value_map[next_column] = value_dict | ||
26 | self.field_map[next_column] = field | ||
27 | |||
28 | def __get_all(self): | ||
29 | '''A Style holding all possible values for each property.''' | ||
30 | return Style(list('.,ov^<>1234sp*hH+xDd|_'), # markers | ||
31 | ['-', ':', '--'], # lines | ||
32 | list('bgrcmyk')) # colors | ||
33 | |||
34 | def get_style(self, kv): | ||
35 | '''Translate column values to unique line style.''' | ||
36 | style_fields = {} | ||
37 | |||
38 | for column, values in self.value_map.iteritems(): | ||
39 | if column not in kv: | ||
40 | continue | ||
41 | field = self.field_map[column] | ||
42 | style_fields[field] = values[kv[column]] | ||
43 | |||
44 | return StyleMap.DEFAULT._replace(**style_fields) | ||
45 | |||
46 | def get_key(self): | ||
47 | '''A visual description of this StyleMap.''' | ||
48 | key = [] | ||
49 | |||
50 | for column, values in self.value_map.iteritems(): | ||
51 | # print("***%s, %s" % column, values) | ||
52 | for v in values.keys(): | ||
53 | sdict = dict([(column, v)]) | ||
54 | style = self.get_style(sdict) | ||
55 | |||
56 | styled_line = plot.plot([], [], style.fmt())[0] | ||
57 | description = "%s:%s" % (column, v) | ||
58 | |||
59 | key += [(styled_line, description)] | ||
60 | |||
61 | return sorted(key, key=lambda x:x[1]) | ||
62 | |||