31
31
from transforms import Bbox
32
32
33
33
34
-
35
34
class Cell (Rectangle ):
36
35
"""
37
36
A cell is a Rectangle with some associated text.
@@ -49,18 +48,17 @@ def __init__(self, xy, width, height,
49
48
50
49
# Call base
51
50
Rectangle .__init__ (self , xy , width = width , height = height ,
52
- edgecolor = edgecolor , facecolor = facecolor ,
53
- )
51
+ edgecolor = edgecolor , facecolor = facecolor )
54
52
self .set_clip_on (False )
55
53
56
54
# Create tex
8000
t object
57
- if loc is None : loc = 'right'
55
+ if loc is None :
56
+ loc = 'right'
58
57
self ._loc = loc
59
58
self ._text = Text (x = xy [0 ], y = xy [1 ], text = text ,
60
59
fontproperties = fontproperties )
61
60
self ._text .set_clip_on (False )
62
61
63
-
64
62
def set_transform (self , trans ):
65
63
Rectangle .set_transform (self , trans )
66
64
# the text does not get the transform!
@@ -93,7 +91,8 @@ def auto_set_font_size(self, renderer):
93
91
94
92
@allow_rasterization
95
93
def draw (self , renderer ):
96
- if not self .get_visible (): return
94
+ if not self .get_visible ():
95
+ return
97
96
# draw the rectangle
98
97
Rectangle .draw (self , renderer )
99
98
@@ -134,14 +133,14 @@ def get_text_bounds(self, renderer):
134
133
135
134
def get_required_width (self , renderer ):
136
135
""" Get width required for this cell. """
137
- l ,b , w , h = self .get_text_bounds (renderer )
136
+ l , b , w , h = self .get_text_bounds (renderer )
138
137
return w * (1.0 + (2.0 * self .PAD ))
139
138
140
-
141
139
def set_text_props (self , ** kwargs ):
142
140
'update the text properties with kwargs'
143
141
self ._text .update (kwargs )
144
142
143
+
145
144
class Table (Artist ):
146
145
"""
147
146
Create a table of cells.
@@ -155,25 +154,24 @@ class Table(Artist):
155
154
Return value is a sequence of text, line and patch instances that make
156
155
up the table
157
156
"""
158
- codes = {'best' : 0 ,
159
- 'upper right' : 1 , # default
160
- 'upper left' : 2 ,
161
- 'lower left' : 3 ,
162
- 'lower right' : 4 ,
163
- 'center left' : 5 ,
164
- 'center right' : 6 ,
165
- 'lower center' : 7 ,
166
- 'upper center' : 8 ,
167
- 'center' : 9 ,
168
-
169
- 'top right' : 10 ,
170
- 'top left' : 11 ,
171
- 'bottom left' : 12 ,
172
- 'bottom right' : 13 ,
173
- 'right' : 14 ,
174
- 'left' : 15 ,
175
- 'top' : 16 ,
176
- 'bottom' : 17 ,
157
+ codes = {'best' : 0 ,
158
+ 'upper right' : 1 , # default
159
+ 'upper left' : 2 ,
160
+ 'lower left' : 3 ,
161
+ 'lower right' : 4 ,
162
+ 'center left' : 5 ,
163
+ 'center right' : 6 ,
164
+ 'lower center' : 7 ,
165
+ 'upper center' : 8 ,
166
+ 'center' : 9 ,
167
+ 'top right' : 10 ,
168
+ 'top left' : 11 ,
169
+ 'bottom left' : 12 ,
170
+ 'bottom right' : 13 ,
171
+ 'right' : 14 ,
172
+ 'left' : 15 ,
173
+ 'top' : 16 ,
174
+ 'bottom' : 17 ,
177
175
}
178
176
179
177
FONTSIZE = 10
@@ -184,9 +182,12 @@ def __init__(self, ax, loc=None, bbox=None):
184
182
Artist .__init__ (self )
185
183
186
184
if is_string_like (loc ) and loc not in self .codes :
187
- warnings .warn ('Unrecognized location %s. Falling back on bottom; valid locations are\n %s\t ' % (loc , '\n \t ' .join (self .codes .iterkeys ())))
185
+ warnings .warn ('Unrecognized location %s. Falling back on '
186
+ 'bottom; valid locations are\n %s\t ' %
187
+ (loc , '\n \t ' .join (self .codes .iterkeys ())))
188
188
loc = 'bottom'
189
- if is_string_like (loc ): loc = self .codes .get (loc , 1 )
189
+ if is_string_like (loc ):
190
+ loc = self .codes .get (loc , 1 )
190
191
self .set_figure (ax .figure )
191
192
self ._axes = ax
192
193
self ._loc = loc
@@ -205,7 +206,7 @@ def __init__(self, ax, loc=None, bbox=None):
205
206
206
207
def add_cell (self , row , col , * args , ** kwargs ):
207
208
""" Add a cell to the table. """
208
- xy = (0 ,0 )
209
+ xy = (0 , 0 )
209
210
210
211
cell = Cell (xy , * args , ** kwargs )
211
212
cell .set_figure (self .figure )
@@ -215,18 +216,21 @@ def add_cell(self, row, col, *args, **kwargs):
215
216
self ._cells [(row , col )] = cell
216
217
217
218
def _approx_text_height (self ):
218
- return self .FONTSIZE / 72.0 * self .figure .dpi / self ._axes .bbox .height * 1.2
219
+ return (self .FONTSIZE / 72.0 * self .figure .dpi /
220
+ self ._axes .bbox .height * 1.2 )
219
221
220
222
@allow_rasterization
221
223
def draw (self , renderer ):
222
- # Need a renderer to do hit tests on mouseevent; assume the last one will do
224
+ # Need a renderer to do hit tests on mouseevent; assume the last one
225
+ # will do
223
226
if renderer is None :
224
227
renderer = self ._cachedRenderer
225
228
if renderer is None :
226
229
raise RuntimeError ('No renderer defined' )
227
230
self ._cachedRenderer = renderer
228
231
229
- if not self .get_visible (): return
232
+ if not self .get_visible ():
233
+ return
230
234
renderer .open_group ('table' )
231
235
self ._update_positions (renderer )
232
236
@@ -249,24 +253,25 @@ def _get_grid_bbox(self, renderer):
249
253
bbox = Bbox .union (boxes )
250
254
return bbox .inverse_transformed (self .get_transform ())
251
255
252
- def contains (self ,mouseevent ):
256
+ def contains (self , mouseevent ):
253
257
"""Test whether the mouse event occurred in the table.
254
258
255
259
Returns T/F, {}
256
260
"""
257
261
if callable (self ._contains ):
258
- return self ._contains (self ,mouseevent )
262
+ return self ._contains (self , mouseevent )
259
263
260
264
# TODO: Return index of the cell containing the cursor so that the user
261
265
# doesn't have to bind to each one individually.
262
266
if self ._cachedRenderer is not None :
263
267
boxes = [self ._cells [pos ].get_window_extent (self ._cachedRenderer )
264
268
for pos in self ._cells .iterkeys ()
265
269
if pos [0 ] >= 0 and pos [1 ] >= 0 ]
270
+ # FIXME bbox_all is not defined.
266
271
bbox = bbox_all (boxes )
267
- return bbox .contains (mouseevent .x ,mouseevent .y ),{}
272
+ return bbox .contains (mouseevent .x , mouseevent .y ), {}
268
273
else :
269
- return False ,{}
274
+ return False , {}
270
275
271
276
def get_children (self ):
272
277
'Return the Artists contained by the table'
@@ -276,6 +281,7 @@ def get_children(self):
276
281
def get_window_extent (self , renderer ):
277
282
'Return the bounding box of the table in window coords'
278
283
boxes = [c .get_window_extent (renderer ) for c in self ._cells ]
284
+ # FIXME bbox_all is not defined
279
285
return bbox_all (boxes )
280
286
281
287
def _do_cell_alignment (self ):
@@ -346,7 +352,8 @@ def _auto_set_font_size(self, renderer):
346
352
cells = []
347
353
for key , cell in self ._cells .iteritems ():
348
354
# ignore auto-sized columns
349
- if key [1 ] in self ._autoColumns : continue
355
+ if key [1 ] in self ._autoColumns :
356
+ continue
350
357
size = cell .auto_set_font_size (renderer )
351
358
fontsize = min (fontsize , size )
352
359
cells .append(cell )
@@ -376,8 +383,8 @@ def _offset(self, ox, oy):
376
383
377
384
for c in self ._cells .itervalues ():
378
385
x , y = c .get_x (), c .get_y ()
379
- c .set_x (x + ox )
380
- c .set_y (y + oy )
386
+ c .set_x (x + ox )
387
+ c .set_y (y + oy )
381
388
382
389
def _update_positions (self , renderer ):
383
390
# called from renderer to allow more precise estimates of
@@ -394,12 +401,12 @@ def _update_positions(self, renderer):
394
401
self ._do_cell_alignment ()
395
402
396
403
bbox = self ._get_grid_bbox (renderer )
397
- l ,b , w , h = bbox .bounds
404
+ l , b , w , h = bbox .bounds
398
405
399
406
if self ._bbox is not None :
400
407
# Position according to bbox
401
408
rl , rb , rw , rh = self ._bbox
402
- self .scale (rw / w , rh / h )
409
+ self .scale (rw / w , rh / h )
403
410
ox = rl - l
404
411
oy = rb - b
405
412
self ._do_cell_alignment ()
@@ -408,8 +415,8 @@ def _update_positions(self, renderer):
408
415
(BEST , UR , UL , LL , LR , CL , CR , LC , UC , C ,
409
416
TR , TL , BL , BR , R , L , T , B ) = range (len (self .codes ))
410
417
# defaults for center
411
- ox = (0.5 - w / 2 ) - l
412
- oy = (0.5 - h / 2 ) - b
418
+ ox = (0.5 - w / 2 ) - l
419
+ oy = (0.5 - h / 2 ) - b
413
420
if self ._loc in (UL , LL , CL ): # left
414
421
ox = self .AXESPAD - l
415
422
if self ._loc in (BEST , UR , LR , R , CR ): # right
@@ -419,26 +426,26 @@ def _update_positions(self, renderer):
419
426
if self ._loc in (LL , LR , LC ): # lower
420
427
oy = self .AXESPAD - b
421
428
if self ._loc in (LC , UC , C ): # center x
422
- ox = (0.5 - w / 2 ) - l
429
+ ox = (0.5 - w / 2 ) - l
423
430
if self ._loc in (CL , CR , C ): # center y
424
- oy = (0.5 - h / 2 ) - b
431
+ oy = (0.5 - h / 2 ) - b
425
432
426
433
if self ._loc in (TL , BL , L ): # out left
427
- ox = - (l + w )
434
+ ox = - (l + w )
428
435
if self ._loc in (TR , BR , R ): # out right
429
436
ox = 1.0 - l
430
437
if self ._loc in (TR , TL , T ): # out top
431
438
oy = 1.0 - b
432
439
if self ._loc in (BL , BR , B ): # out bottom
433
- oy = - (b + h )
440
+ oy = - (b + h )
434
441
435
442
self ._offset (ox , oy )
436
443
437
-
438
444
def get_celld (self ):
439
445
'return a dict of cells in the table'
440
446
return self ._cells
441
447
448
+
442
449
def table (ax ,
443
450
cellText = None , cellColours = None ,
444
451
cellLoc = 'right' , colWidths = None ,
@@ -477,7 +484,7 @@ def table(ax,
477
484
478
485
# Set colwidths if not given
479
486
if colWidths is None :
480
- colWidths = [1.0 / cols ] * cols
487
+ colWidths = [1.0 / cols ] * cols
481
488
482
489
# Check row and column labels
483
490
rowLabelWidth = 0
@@ -514,7 +521,7 @@ def table(ax,
514
521
# Add the cells
515
522
for row in xrange (rows ):
516
523
for col in xrange (cols ):
517
- table .add_cell (row + offset , col ,
524
+ table .add_cell (row + offset , col ,
518
525
width = colWidths [col ], height = height ,
519
526
text = cellText [row ][col ],
520
527
facecolor = cellColours [row ][col ],
@@ -530,7 +537,7 @@ def table(ax,
530
537
# Do row labels
531
538
if rowLabels is not None :
532
539
for row in xrange (rows ):
533
- table .add_cell (row + offset , - 1 ,
540
+ table .add_cell (row + offset , - 1 ,
534
541
width = rowLabelWidth or 1e-15 , height = height ,
535
542
text = rowLabels [row ], facecolor = rowColours [row ],
536
543
loc = rowLoc )
0 commit comments