Mercurial > repos > laurenmarazzi > netisce_test
comparison tools/myTools/bin/sfa/plot/tableaxis.py @ 1:7e5c71b2e71f draft default tip
Uploaded
author | laurenmarazzi |
---|---|
date | Wed, 22 Dec 2021 16:00:34 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:f24d4892aaed | 1:7e5c71b2e71f |
---|---|
1 | |
2 import numpy as np | |
3 import matplotlib.pyplot as plt | |
4 from matplotlib.table import Table | |
5 | |
6 | |
7 class TableAxis(object): | |
8 def __init__(self, ax, df, colors): | |
9 self._ax = ax | |
10 self._df = df | |
11 self._colors = colors | |
12 | |
13 # Create matplotlib's Table object | |
14 self._tb = Table(self._ax, bbox=[0, 0, 1, 1]) | |
15 self._tb.auto_set_font_size(False) | |
16 self._ax.add_table(self._tb) | |
17 | |
18 self._calculate_cell_size() | |
19 self._add_cells() | |
20 self._remove_ticks() | |
21 | |
22 def _calculate_cell_size(self): | |
23 # The number of total rows and columns of the table | |
24 self._nrows = self._df.shape[0] | |
25 self._ncols = self._df.shape[1] | |
26 | |
27 # Calculate the width and height of a single cell in the table | |
28 L = 1.0 | |
29 H = 1.0 | |
30 self._w_cell = L / self._ncols | |
31 self._h_cell = H / self._nrows | |
32 | |
33 def _add_cells(self): | |
34 raise NotImplementedError() | |
35 | |
36 def _remove_ticks(self): | |
37 self._ax.get_xaxis().set_ticks([]) | |
38 self._ax.get_yaxis().set_ticks([]) | |
39 | |
40 def add_row_labels(self): | |
41 """Add row labels using y-axis | |
42 """ | |
43 ylabels = list(self._df.index) | |
44 self._ax.yaxis.tick_left() | |
45 | |
46 yticks = [self._h_cell/2.0] # The position of the first label | |
47 | |
48 # The row labels of condition subtable | |
49 for j in range(1, self._nrows): | |
50 yticks.append(yticks[j-1] + self._h_cell) | |
51 | |
52 self._ax.set_yticks(yticks) | |
53 self._ax.set_yticklabels(ylabels, minor=False) | |
54 self._ax.tick_params(axis='y', which='major', pad=3) | |
55 | |
56 # Hide the small bars of ticks | |
57 for tick in self._ax.yaxis.get_major_ticks(): | |
58 tick.tick1On = False | |
59 tick.tick2On = False | |
60 # end of def | |
61 | |
62 def add_column_labels(self): | |
63 """ | |
64 Add column labels using x-axis | |
65 """ | |
66 xlabels = list(self._df.columns) | |
67 xticks = [self._w_cell/2.0] # The position of the first label | |
68 # The column labels of condition subtable | |
69 for j in range(1, self._ncols): | |
70 xticks.append(xticks[j - 1] + self._w_cell) | |
71 | |
72 self._ax.xaxis.set_ticks_position('none') | |
73 | |
74 # # Hide the small bars of ticks | |
75 # for tick in self._ax.xaxis.get_major_ticks(): | |
76 # tick.tick1On = False | |
77 # tick.tick2On = False | |
78 | |
79 self._ax.set_xticks(xticks) | |
80 self._ax.set_xticklabels(xlabels, rotation=90, minor=False) | |
81 self._ax.tick_params(axis='x', which='major', pad=-2) | |
82 | |
83 # end of def | |
84 | |
85 @property | |
86 def fontsize(self): | |
87 return self._table_fontsize | |
88 | |
89 @fontsize.setter | |
90 def fontsize(self, val): | |
91 """ | |
92 Resize text fonts | |
93 """ | |
94 self._table_fontsize = val | |
95 self._tb.set_fontsize(val) | |
96 | |
97 @property | |
98 def linewidth(self): | |
99 return self._linewidth | |
100 | |
101 @linewidth.setter | |
102 def linewidth(self, val): | |
103 """Adjust the width of table lines | |
104 """ | |
105 self._linewidth = val | |
106 celld = self._tb.get_celld() | |
107 for (i, j), cell in celld.items(): | |
108 cell.set_linewidth(self._linewidth) | |
109 | |
110 | |
111 class ConditionTableAxis(TableAxis): | |
112 | |
113 def _add_cells(self): | |
114 for (i, j), val in np.ndenumerate(self._df): | |
115 if val != 0: | |
116 fcolor = self._colors['cond_up_cell'] | |
117 else: | |
118 fcolor = self._colors['cond_dn_cell'] | |
119 | |
120 self._tb.add_cell(i, j, | |
121 self._w_cell, self._h_cell, | |
122 loc='center', facecolor=fcolor) | |
123 | |
124 | |
125 class ResultTableAxis(TableAxis): | |
126 | |
127 def __init__(self, ax, dfr, dfe, colors): | |
128 self._dfr = dfr | |
129 self._dfe = dfe | |
130 super().__init__(ax, dfr, colors) | |
131 | |
132 def _add_cells(self): | |
133 """Add cells for representing the agreement between | |
134 the results of simulation and experiment. | |
135 """ | |
136 for (i, j), val_cons in np.ndenumerate(self._dfr): | |
137 if val_cons == True: | |
138 fcolor = self._colors['result_up_cell'] | |
139 elif val_cons == False: | |
140 fcolor = self._colors['result_dn_cell'] | |
141 else: | |
142 raise ValueError("The value of result table element " | |
143 "should be bool.") | |
144 | |
145 val_exp = self._dfe.iloc[i, j] | |
146 if val_exp > 0: | |
147 text_arrow = 'UP' | |
148 elif val_exp < 0: | |
149 text_arrow = 'DN' | |
150 else: # val_Exp == 0 | |
151 text_arrow = '─' | |
152 | |
153 self._tb.add_cell(i, j, | |
154 self._w_cell, self._h_cell, | |
155 text=text_arrow, | |
156 loc='center', | |
157 facecolor=fcolor) | |
158 # end of for | |
159 | |
160 # Set colors of the result | |
161 celld = self._tb.get_celld() | |
162 for (i, j), val in np.ndenumerate(self._dfr): | |
163 cell = celld[(i, j)] | |
164 | |
165 # Set colors | |
166 if val > 0: | |
167 tcolor = self._colors['result_up_text'] | |
168 else: | |
169 tcolor = self._colors['result_dn_text'] | |
170 # end of if-else | |
171 | |
172 # Adjust text | |
173 cell.set_text_props(color=tcolor, | |
174 weight='bold') | |
175 # end of for |