forked from arraystream/weplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubplot.py
More file actions
65 lines (53 loc) · 1.66 KB
/
subplot.py
File metadata and controls
65 lines (53 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import itertools
from collections import namedtuple
class SubPlotSpec(namedtuple('SubPlotSpec', ['r', 'c', 'rs', 'cs'])):
@property
def row_end(self):
return self.r + self.rs
@property
def col_end(self):
return self.c + self.cs
@property
def location(self):
return self.r, self.c
@property
def r_max(self):
return self.row_end - 1
@property
def c_max(self):
return self.col_end - 1
def area_spec(self):
area = {}
for location in itertools.product(range(self.r, self.row_end), range(self.c, self.col_end)):
area[location] = None
anchor_spec = {}
if self.rs > 1:
anchor_spec['rowspan'] = self.rs
if self.cs > 1:
anchor_spec['colspan'] = self.cs
area[self.location] = anchor_spec
return area
class PlotCanvas(object):
def __init__(self):
self.canvas = {}
self.specs = []
self.max_rows = 1
self.max_cols = 1
def _expand(self, r_max, c_max):
if r_max > self.max_rows:
self.max_rows = r_max
if c_max > self.max_cols:
self.max_cols = c_max
def occupy_area(self, spec):
if spec is not None:
self.canvas.update(spec.area_spec())
self._expand(spec.r_max, spec.c_max)
self.specs.append(spec)
def make_plotly_spec(self):
plotly_specs = []
for r in range(1, self.max_rows + 1):
row_spec = []
for c in range(1, self.max_cols + 1):
row_spec.append(self.canvas.get((r, c), {}))
plotly_specs.append(row_spec)
return plotly_specs