Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9eb3204

Browse files
author
Anthony Cho
committed
Implemented issue 4044. Created a Cell subclass, SciCell, to override the default draw function. SciCell draws a polygon (line) instead of the rectangle
1 parent 66c5aa6 commit 9eb3204

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

lib/matplotlib/table.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
from . import artist
3131
from .artist import Artist, allow_rasterization
32-
from .patches import Rectangle
32+
from .patches import Rectangle, Polygon
3333
from .cbook import is_string_like
3434
from matplotlib import docstring
3535
from .text import Text
@@ -145,6 +145,31 @@ def set_text_props(self, **kwargs):
145145
'update the text properties with kwargs'
146146
self._text.update(kwargs)
147147

148+
class SciCell(Cell):
149+
150+
@allow_rasterization
151+
def draw(self, renderer):
152+
if not self.get_visible():
153+
return
154+
155+
bbox = Rectangle.get_bbox(self)
156+
x, y, w, h = bbox.bounds
157+
158+
topLineVertices = [[x, y],[x + w, y]]
159+
botLineVertices = [[x, y + h],[x + w, y + h]]
160+
161+
topLine = Polygon(topLineVertices)
162+
botLine = Polygon(botLineVertices)
163+
164+
topLine.update_from(self)
165+
botLine.update_from(self)
166+
167+
topLine.draw(renderer)
168+
botLine.draw(renderer)
169+
170+
# position the text
171+
self._set_text_position(renderer)
172+
self._text.draw(renderer)
148173

149174
class Table(Artist):
150175
"""
@@ -211,12 +236,17 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs):
211236
self.set_clip_on(False)
212237

213238
self._cachedRenderer = None
239+
self._cellType = 'default'
214240

215241
def add_cell(self, row, col, *args, **kwargs):
216242
""" Add a cell to the table. """
217243
xy = (0, 0)
218244

219-
cell = Cell(xy, *args, **kwargs)
245+
if self._cellType == 'default':
246+
cell = Cell(xy, *args, **kwargs)
247+
else:
248+
cell = SciCell(xy, *args, **kwargs)
249+
220250
cell.set_figure(self.figure)
221251
cell.set_transform(self.get_transform())
222252

@@ -453,16 +483,27 @@ def get_celld(self):
453483
'return a dict of cells in the table'
454484
return self._cells
455485

486+
def get_cell_type(self):
487+
return self._cellType
488+
489+
def set_cell_type(self, cellType):
490+
if cellType in ('default', 'scicell'):
491+
self._cellType = cellType
492+
493+
else:
494+
raise ValueError('Unrecognized cell type %s; '
495+
'try default or scicell' % cellType)
496+
456497

457498
def table(ax,
458-
cellText=None, cellColours=None,
499+
cellType=None, cellText=None, cellColours=None,
459500
cellLoc='right', colWidths=None,
460501
rowLabels=None, rowColours=None, rowLoc='left',
461502
colLabels=None, colColours=None, colLoc='center',
462503
loc='bottom', bbox=None,
463504
**kwargs):
464505
"""
465-
TABLE(cellText=None, cellColours=None,
506+
TABLE(cellType='default', cellText=None, cellColours=None,
466507
cellLoc='right', colWidths=None,
467508
rowLabels=None, rowColours=None, rowLoc='left',
468509
colLabels=None, colColours=None, colLoc='center',
@@ -472,6 +513,9 @@ def table(ax,
472513
473514
Thanks to John Gill for providing the class and table.
474515
"""
516+
if cellType is not None:
517+
assert cellType in ('default', 'scicell')
518+
475519
# Check we have some cellText
476520
if cellText is None:
477521
# assume just colours are needed
@@ -529,6 +573,9 @@ def table(ax,
529573
# Now create the table
530574
table = Table(ax, loc, bbox, **kwargs)
531575
height = table._approx_text_height()
576+
577+
if cellType is not None:
578+
table.set_cell_type(cellType)
532579

533580
# Add the cells
534581
for row in xrange(rows):

0 commit comments

Comments
 (0)