34
34
from matplotlib import docstring
35
35
from .text import Text
36
36
from .transforms import Bbox
37
+ from matplotlib .path import Path
37
38
38
39
39
40
class Cell (Rectangle ):
@@ -146,6 +147,67 @@ def set_text_props(self, **kwargs):
146
147
self ._text .update (kwargs )
147
148
148
149
150
+ class CustomCell (Cell ):
151
+ """
152
+ A subclass of Cell where the sides may be visibly toggled.
153
+
154
+ """
155
+
156
+ _edges = 'BRTL'
157
+ _edge_aliases = {'open' : '' ,
158
+ 'closed' : _edges , # default
159
+ 'horizontal' : 'BT' ,
160
+ 'vertical' : 'RL'
161
+ }
162
+
163
+ def __init__ (self , * args , ** kwargs ):
164
+ visible_edges = kwargs .pop ('visible_edges' )
165
+ Cell .__init__ (self , * args , ** kwargs )
166
+ self .visible_edges = visible_edges
167
+
168
+ @property
169
+ def visible_edges (self ):
170
+ return self ._visible_edges
171
+
172
+ @visible_edges .setter
173
+ def visible_edges (self , value ):
174
+ if value is None :
175
+ self ._visible_edges = self ._edges
176
+ elif value in self ._edge_aliases :
177
+ self ._visible_edges = self ._edge_aliases [value ]
178
+ else :
179
+ for edge in value :
180
+ if edge not in self ._edges :
181
+ msg = ('Invalid edge param {0}, must only be one of'
182
+ ' {1} or string of {2}.' ).format (
183
+ value ,
184
+ ", " .join (self ._edge_aliases .keys ()),
185
+ ", " .join (self ._edges ),
186
+ )
187
+ raise ValueError (msg )
188
+ self ._visible_edges = value
189
+
190
+ def get_path (self ):
191
+ 'Return a path where the edges specificed by _visible_edges are drawn'
192
+
193
+ codes = [Path .MOVETO ]
194
+
195
+ for edge in self ._edges :
196
+ if edge in self ._visible_edges :
197
+ codes .append (Path .LINETO )
198
+ else :
199
+ codes .append (Path .MOVETO )
200
+
201
+ if Path .MOVETO not in codes [1 :]: # All sides are visible
202
+ codes [- 1 ] = Path .CLOSEPOLY
203
+
204
+ return Path (
205
+ [[0.0 , 0.0 ], [1.0 , 0.0 ], [1.0 , 1.0 ], [0.0 , 1.0 ], [0.0 , 0.0 ]],
206
+ codes ,
207
+ readonly = True
208
+ )
209
+
210
+
149
211
class Table (Artist ):
150
212
"""
151
213
Create a table of cells.
@@ -203,6 +265,7 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs):
203
265
204
266
self ._texts = []
205
267
self ._cells = {}
268
+ self ._edges = None
206
269
self ._autoRows = []
207
270
self ._autoColumns = []
208
271
self ._autoFontsize = True
@@ -216,13 +279,21 @@ def add_cell(self, row, col, *args, **kwargs):
216
279
""" Add a cell to the table. """
217
280
xy = (0 , 0 )
218
281
219
- cell = Cell (xy , * args , ** kwargs )
282
+ cell = CustomCell (xy , visible_edges = self . edges , * args , ** kwargs )
220
283
cell .set_figure (self .figure )
221
284
cell .set_transform (self .get_transform ())
222
285
223
286
cell .set_clip_on (False )
224
287
self ._cells [(row , col )] = cell
225
288
289
+ @property
290
+ def edges (self ):
291
+ return self ._edges
292
+
293
+ @edges .setter
294
+ def edges (self , value ):
295
+ self ._edges = value
296
+
226
297
def _approx_text_height (self ):
227
298
return (self .FONTSIZE / 72.0 * self .figure .dpi /
228
299
self ._axes .bbox .height * 1.2 )
@@ -246,8 +317,8 @@ def draw(self, renderer):
246
317
keys .sort ()
247
318
for key in keys :
248
319
self ._cells [key ].draw (renderer )
249
- #for c in self._cells.itervalues():
250
- # c.draw(renderer)
320
+ # for c in self._cells.itervalues():
321
+ # c.draw(renderer)
251
322
renderer .close_group ('table' )
252
323
253
324
def _get_grid_bbox (self , renderer ):
@@ -273,8 +344,8 @@ def contains(self, mouseevent):
273
344
# doesn't have to bind to each one individually.
274
345
if self ._cachedRenderer is not None :
275
346
boxes = [self ._cells [pos ].get_window_extent (self ._cachedRenderer )
276
- for pos in six .iterkeys (self ._cells )
277
- if pos [0 ] >= 0 and pos [1 ] >= 0 ]
347
+ for pos in six .iterkeys (self ._cells )
348
+ if pos [0 ] >= 0 and pos [1 ] >= 0 ]
278
349
bbox = Bbox .union (boxes )
279
350
return bbox .contains (mouseevent .x , mouseevent .y ), {}
280
351
else :
@@ -455,23 +526,24 @@ def get_celld(self):
455
526
456
527
457
528
def table (ax ,
458
- cellText = None , cellColours = None ,
459
- cellLoc = 'right' , colWidths = None ,
460
- rowLabels = None , rowColours = None , rowLoc = 'left' ,
461
- colLabels = None , colColours = None , colLoc = 'center' ,
462
- loc = 'bottom' , bbox = None ,
463
- ** kwargs ):
529
+ cellText = None , cellColours = None ,
530
+ cellLoc = 'right' , colWidths = None ,
531
+ rowLabels = None , rowColours = None , rowLoc = 'left' ,
532
+ colLabels = None , colColours = None , colLoc = 'center' ,
533
+ loc = 'bottom' , bbox = None , edges = 'closed' ,
534
+ ** kwargs ):
464
535
"""
465
536
TABLE(cellText=None, cellColours=None,
466
537
cellLoc='right', colWidths=None,
467
538
rowLabels=None, rowColours=None, rowLoc='left',
468
539
colLabels=None, colColours=None, colLoc='center',
469
- loc='bottom', bbox=None)
540
+ loc='bottom', bbox=None, edges='closed' )
470
541
471
542
Factory function to generate a Table instance.
472
543
473
544
Thanks to John Gill for providing the class and table.
474
545
"""
546
+
475
547
# Check we have some cellText
476
548
if cellText is None :
477
549
# assume just colours are needed
@@ -531,6 +603,7 @@ def table(ax,
531
603
532
604
# Now create the table
533
605
table = Table (ax , loc , bbox , ** kwargs )
606
+ table .edges = edges
534
607
height = table ._approx_text_height ()
535
608
536
609
# Add the cells
0 commit comments