mpl_grid.py 2.66 KB
Newer Older
1
2
3
4
_D_MARGIN = 0.05
_D_PADDING = 0.05
_D_POS = [0.0, 0.0, 1.0, 1.0]

5

6
7
class Grid(object):

8
    def __init__(self, pos=_D_POS, margin=_D_MARGIN, padding=_D_PADDING):
9
10
11
12
13
14
15
        self.margin = margin
        self.padding = padding
        left, bottom, width, height = pos
        self.left = left
        self.bottom = bottom
        self.width = width
        self.height = height
16
17
        self.items = []
        self.fig = None
18
        self.grids = []
19
20

    def layout(self):
21
22

        if self.contains_a_grid():
23
            for grid in self.get_grids():
24
                grid.layout()
25

26
    def append_axes(self, ax, ax_label, rawdata):
27
        self.items.append(ax)
28
        self.fig.add_axes(ax, ax_label)
29
        self.fig.set_rawdata(self.fig.rawdata + (rawdata,))
30

31
    def append_grid(self, grid):
32
        self.items.append(grid)
33
        self.grids.append(grid)
34
        self.items[-1].fig = self.fig
35

36
    def get_item(self, index):
37
38
        return(self.items[index])

39
    def get_items(self):
40
41
        return(self.items)

42
    def get_grids(self):
43
        return(self.grids)
44

45
46
    def contains_a_grid(self):
        return(len(self.grids) > 0)
47

48
49
50
51
52
53
    def set_position(self, pos):
        left, bottom, width, height = pos
        self.left = left
        self.bottom = bottom
        self.width = width
        self.height = height
54

55

56
57
58
class VerticalGrid(Grid):

    def layout(self):
59

60
        nbr_axes = len(self.items)
61
        if nbr_axes > 0:
62
63
            axes_height = (self.height - (nbr_axes-1)*self.padding -
                           2.*self.margin)/nbr_axes
64
65
            for index in range(0, nbr_axes):
                new_left = self.margin + self.left
66
67
68
                new_bottom = self.margin + self.bottom + \
                    index*axes_height + index*self.padding
                new_width = self.width - 2.*self.margin
69
70
71
                new_height = axes_height
                new_frame = [new_left, new_bottom, new_width, new_height]
                self.items[index].set_position(new_frame)
72

73
74
        Grid.layout(self)

75

76
77
78
class HorizontalGrid(Grid):

    def layout(self):
79

80
        nbr_axes = len(self.items)
81
        if nbr_axes > 0:
82
83
            axes_width = (self.width - (nbr_axes-1)*self.padding -
                          2.*self.margin)/nbr_axes
84
            for index in range(0, nbr_axes):
85
86
87
                new_left = self.margin + self.left + \
                    index*axes_width + index*self.padding
                new_bottom = self.margin + self.bottom
88
89
90
91
92
                new_width = axes_width
                new_height = self.height - 2.*self.margin
                new_frame = [new_left, new_bottom, new_width, new_height]
                self.items[index].set_position(new_frame)

93
        Grid.layout(self)