Skip to content

Commit bb4e7d4

Browse files
dkuaDavid
authored andcommitted
Implemented different cell type selection method.
Instead of having a separate subclass of Table, this commit implements a new method for the Table class to select a kind of cell type. This way was preferred by @tacaswell.
1 parent af8497e commit bb4e7d4

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

lib/matplotlib/table.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ class Table(Artist):
195195
'bottom': 17,
196196
}
197197

198+
CELLTYPES = {'default': Cell,
199+
'scientific': ScientificCell,
200+
}
201+
198202
FONTSIZE = 10
199203
AXESPAD = 0.02 # the border between the axes and table edge
200204

@@ -219,6 +223,7 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs):
219223

220224
self._texts = []
221225
self._cells = {}
226+
self._cellType = 'default'
222227
self._autoRows = []
223228
self._autoColumns = []
224229
self._autoFontsize = True
@@ -232,13 +237,30 @@ def add_cell(self, row, col, *args, **kwargs):
232237
""" Add a cell to the table. """
233238
xy = (0, 0)
234239

235-
cell = Cell(xy, *args, **kwargs)
240+
cell = self.CELLTYPES[self.cellType](xy, *args, **kwargs)
236241
cell.set_figure(self.figure)
237242
cell.set_transform(self.get_transform())
238243

239244
cell.set_clip_on(False)
240245
self._cells[(row, col)] = cell
241246

247+
@property
248+
def cellType(self):
249+
return self._cellType
250+
251+
@cellType.setter
252+
def cellType(self, value):
253+
if value is None:
254+
pass # Leave as previously set
255+
elif value in self.CELLTYPES:
256+
self._cellType = value
257+
else:
258+
msg = 'Unrecognized type of Cell: {0}, must be one of {1}.'.format(
259+
value,
260+
", ".join(self.CELLTYPES.keys()),
261+
)
262+
raise ValueError(msg)
263+
242264
def _approx_text_height(self):
243265
return (self.FONTSIZE / 72.0 * self.figure.dpi /
244266
self._axes.bbox.height * 1.2)
@@ -470,37 +492,19 @@ def get_celld(self):
470492
return self._cells
471493

472494

473-
class ScientificTable(Table):
474-
"""
475-
A subclass of Table which uses ScientificCell instead of Cell.
476-
477-
"""
478-
479-
def add_cell(self, row, col, *args, **kwargs):
480-
'Add a scientific cell to the table.'
481-
xy = (0, 0)
482-
483-
cell = ScientificCell(xy, *args, **kwargs)
484-
cell.set_figure(self.figure)
485-
cell.set_transform(self.get_transform())
486-
487-
cell.set_clip_on(False)
488-
self._cells[(row, col)] = cell
489-
490-
491495
def table(ax,
492496
cellText=None, cellColours=None,
493497
cellLoc='right', colWidths=None,
494498
rowLabels=None, rowColours=None, rowLoc='left',
495499
colLabels=None, colColours=None, colLoc='center',
496-
loc='bottom', bbox=None, tableType=None,
500+
loc='bottom', bbox=None, cellType=None,
497501
**kwargs):
498502
"""
499503
TABLE(cellText=None, cellColours=None,
500504
cellLoc='right', colWidths=None,
501505
rowLabels=None, rowColours=None, rowLoc='left',
502506
colLabels=None, colColours=None, colLoc='center',
503-
loc='bottom', bbox=None, tableType=None)
507+
loc='bottom', bbox=None, cellType=None)
504508
505509
Factory function to generate a Table instance.
506510
@@ -562,11 +566,8 @@ def table(ax,
562566
cellColours = ['w' * cols] * rows
563567

564568
# Now create the table
565-
if tableType == "scientific":
566-
table = ScientificTable(ax, loc, bbox, **kwargs)
567-
else:
568-
table = Table(ax, loc, bbox, **kwargs)
569-
569+
table = Table(ax, loc, bbox, **kwargs)
570+
table.cellType = cellType
570571
height = table._approx_text_height()
571572

572573
# Add the cells

lib/matplotlib/tests/test_table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,5 @@ def test_table_types():
105105
loc="upper center",
106106
colLoc="center",
107107
rowLoc="center",
108-
tableType="scientific",
108+
cellType="scientific",
109109
)

0 commit comments

Comments
 (0)