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

Skip to content

Commit 4542ee7

Browse files
Zair Mubashardkua
authored andcommitted
Reworked feature 4044 so that all cell boundries can be toggled into view or out of it
1 parent d2c3158 commit 4542ee7

1 file changed

Lines changed: 62 additions & 25 deletions

File tree

lib/matplotlib/table.py

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,48 @@ def set_text_props(self, **kwargs):
147147
self._text.update(kwargs)
148148

149149

150-
class ScientificCell(Cell):
150+
class FancyCell(Cell):
151151
"""
152-
A subclass of Cell where vertical lines are ommitted.
153-
152+
A subclass of Cell where vertical and horizontal lines may
153+
be selectively ommited.
154154
"""
155155

156+
def __init__(self, xy, width, height,
157+
edgecolor='k', facecolor='w',
158+
fill=True,
159+
text='',
160+
loc=None,
161+
fontproperties=None,
162+
edgeVisibility="LRBT"
163+
):
164+
Cell.__init__(self, xy, width, height, edgecolor, facecolor, fill,
165+
text, loc, fontproperties)
166+
for letter in edgeVisibility:
167+
if letter not in "LRBT":
168+
msg = ('Invalid edgeVisibility params for FancyCell:' +
169+
'{0}, must only consist of {1}.').format(
170+
value,
171+
", ".join({'T', 'B', 'L', 'R'}),
172+
)
173+
raise ValueError(msg)
174+
self._edgeVisibility = edgeVisibility
175+
156176
def get_path(self):
157-
'Return a path where vertical lines are not drawn'
177+
'Return a path where the edges specificed by edgeVisibility are drawn'
178+
edgeCodes = [Path.MOVETO, Path.MOVETO, Path.MOVETO,
179+
Path.MOVETO, Path.MOVETO]
180+
if 'B' in self._edgeVisibility:
181+
edgeCodes[1] = Path.LINETO
182+
if 'R' in self._edgeVisibility:
183+
edgeCodes[2] = Path.LINETO
184+
if 'T' in self._edgeVisibility:
185+
edgeCodes[3] = Path.LINETO
186+
if 'L' in self._edgeVisibility:
187+
edgeCodes[4] = Path.LINETO
188+
158189
path = Path([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0],
159190
[0.0, 0.0]],
160-
[Path.MOVETO, Path.LINETO, Path.MOVETO, Path.LINETO,
161-
Path.CLOSEPOLY],
191+
edgeCodes,
162192
readonly=True
163193
)
164194
return path
@@ -197,9 +227,11 @@ class Table(Artist):
197227
'bottom': 17,
198228
}
199229

200-
CELLTYPES = {'default': Cell,
201-
'scientific': ScientificCell,
202-
}
230+
DRAWLINE_ALIASES = {'open': '',
231+
'closed': 'LRBT', # default
232+
'horizontal': 'TB',
233+
'vertical': 'LR'
234+
}
203235

204236
FONTSIZE = 10
205237
AXESPAD = 0.02 # the border between the axes and table edge
@@ -225,7 +257,7 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs):
225257

226258
self._texts = []
227259
self._cells = {}
228-
self._cellType = 'default'
260+
self._drawLines = "LTRB"
229261
self._autoRows = []
230262
self._autoColumns = []
231263
self._autoFontsize = True
@@ -239,29 +271,34 @@ def add_cell(self, row, col, *args, **kwargs):
239271
""" Add a cell to the table. """
240272
xy = (0, 0)
241273

242-
cell = self.CELLTYPES[self.cellType](xy, *args, **kwargs)
274+
cell = FancyCell(xy, edgeVisibility=self._drawLines, *args, **kwargs)
243275
cell.set_figure(self.figure)
244276
cell.set_transform(self.get_transform())
245277

246278
cell.set_clip_on(False)
247279
self._cells[(row, col)] = cell
248280

249281
@property
250-
def cellType(self):
251-
return self._cellType
282+
def drawLines(self):
283+
return self._drawLines
252284

253-
@cellType.setter
254-
def cellType(self, value):
285+
@drawLines.setter
286+
def drawLines(self, value):
255287
if value is None:
256288
pass # Leave as previously set
257-
elif value in self.CELLTYPES:
258-
self._cellType = value
289+
elif value in self.DRAWLINE_ALIASES.keys():
290+
self._drawLines = self.DRAWLINE_ALIASES[value]
259291
else:
260-
msg = 'Unrecognized type of Cell: {0}, must be one of {1}.'.format(
261-
value,
262-
", ".join(self.CELLTYPES.keys()),
263-
)
264-
raise ValueError(msg)
292+
for letter in value:
293+
if letter not in "LRBT":
294+
msg = ('Unrecognized draw lines for Cell:' +
295+
' {0}, must be one of {1}.').format(
296+
value,
297+
", ".join({'open', 'closed', 'horizontal',
298+
'vertical', 'string consisting of {T, B, R, L}'}),
299+
)
300+
raise ValueError(msg)
301+
self._drawLines = value
265302

266303
def _approx_text_height(self):
267304
return (self.FONTSIZE / 72.0 * self.figure.dpi /
@@ -499,14 +536,14 @@ def table(ax,
499536
cellLoc='right', colWidths=None,
500537
rowLabels=None, rowColours=None, rowLoc='left',
501538
colLabels=None, colColours=None, colLoc='center',
502-
loc='bottom', bbox=None, cellType=None,
539+
loc='bottom', bbox=None, drawLines='LRBT',
503540
**kwargs):
504541
"""
505542
TABLE(cellText=None, cellColours=None,
506543
cellLoc='right', colWidths=None,
507544
rowLabels=None, rowColours=None, rowLoc='left',
508545
colLabels=None, colColours=None, colLoc='center',
509-
loc='bottom', bbox=None, cellType=None)
546+
loc='bottom', bbox=None, drawLines='LRBT')
510547
511548
Factory function to generate a Table instance.
512549
@@ -569,7 +606,7 @@ def table(ax,
569606

570607
# Now create the table
571608
table = Table(ax, loc, bbox, **kwargs)
572-
table.cellType = cellType
609+
table.drawLines = drawLines
573610
height = table._approx_text_height()
574611

575612
# Add the cells

0 commit comments

Comments
 (0)