From 9eb3204a558afdc1ece1a1173c941a67b3d96db5 Mon Sep 17 00:00:00 2001 From: Anthony Cho Date: Wed, 18 Mar 2015 17:32:52 -0400 Subject: [PATCH 1/6] Implemented issue 4044. Created a Cell subclass, SciCell, to override the default draw function. SciCell draws a polygon (line) instead of the rectangle --- lib/matplotlib/table.py | 55 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index 497c1eb49ad4..9cb7aa558a1e 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -29,7 +29,7 @@ from . import artist from .artist import Artist, allow_rasterization -from .patches import Rectangle +from .patches import Rectangle, Polygon from .cbook import is_string_like from matplotlib import docstring from .text import Text @@ -145,6 +145,31 @@ def set_text_props(self, **kwargs): 'update the text properties with kwargs' self._text.update(kwargs) +class SciCell(Cell): + + @allow_rasterization + def draw(self, renderer): + if not self.get_visible(): + return + + bbox = Rectangle.get_bbox(self) + x, y, w, h = bbox.bounds + + topLineVertices = [[x, y],[x + w, y]] + botLineVertices = [[x, y + h],[x + w, y + h]] + + topLine = Polygon(topLineVertices) + botLine = Polygon(botLineVertices) + + topLine.update_from(self) + botLine.update_from(self) + + topLine.draw(renderer) + botLine.draw(renderer) + + # position the text + self._set_text_position(renderer) + self._text.draw(renderer) class Table(Artist): """ @@ -211,12 +236,17 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs): self.set_clip_on(False) self._cachedRenderer = None + self._cellType = 'default' def add_cell(self, row, col, *args, **kwargs): """ Add a cell to the table. """ xy = (0, 0) - cell = Cell(xy, *args, **kwargs) + if self._cellType == 'default': + cell = Cell(xy, *args, **kwargs) + else: + cell = SciCell(xy, *args, **kwargs) + cell.set_figure(self.figure) cell.set_transform(self.get_transform()) @@ -453,16 +483,27 @@ def get_celld(self): 'return a dict of cells in the table' return self._cells + def get_cell_type(self): + return self._cellType + + def set_cell_type(self, cellType): + if cellType in ('default', 'scicell'): + self._cellType = cellType + + else: + raise ValueError('Unrecognized cell type %s; ' + 'try default or scicell' % cellType) + def table(ax, - cellText=None, cellColours=None, + cellType=None, cellText=None, cellColours=None, cellLoc='right', colWidths=None, rowLabels=None, rowColours=None, rowLoc='left', colLabels=None, colColours=None, colLoc='center', loc='bottom', bbox=None, **kwargs): """ - TABLE(cellText=None, cellColours=None, + TABLE(cellType='default', cellText=None, cellColours=None, cellLoc='right', colWidths=None, rowLabels=None, rowColours=None, rowLoc='left', colLabels=None, colColours=None, colLoc='center', @@ -472,6 +513,9 @@ def table(ax, Thanks to John Gill for providing the class and table. """ + if cellType is not None: + assert cellType in ('default', 'scicell') + # Check we have some cellText if cellText is None: # assume just colours are needed @@ -529,6 +573,9 @@ def table(ax, # Now create the table table = Table(ax, loc, bbox, **kwargs) height = table._approx_text_height() + + if cellType is not None: + table.set_cell_type(cellType) # Add the cells for row in xrange(rows): From 3249f50d59515deca3c32f9faf6be26dda56f5b3 Mon Sep 17 00:00:00 2001 From: Anthony Cho Date: Wed, 18 Mar 2015 19:26:31 -0400 Subject: [PATCH 2/6] Fixed whitespace, added constructor to SciCell --- lib/matplotlib/table.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index 9cb7aa558a1e..c870da041dff 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -145,19 +145,29 @@ def set_text_props(self, **kwargs): 'update the text properties with kwargs' self._text.update(kwargs) + class SciCell(Cell): - + """ + A SciCell is a Cell, but it only draws the horizontal lines of the Cell + + """ + + def __init__(self, *args, **kwargs): + + # Call base + Cell.__init__(self, *args, **kwargs) + @allow_rasterization def draw(self, renderer): if not self.get_visible(): return - + bbox = Rectangle.get_bbox(self) x, y, w, h = bbox.bounds - topLineVertices = [[x, y],[x + w, y]] - botLineVertices = [[x, y + h],[x + w, y + h]] - + topLineVertices = [[x, y], [x + w, y]] + botLineVertices = [[x, y + h], [x + w, y + h]] + topLine = Polygon(topLineVertices) botLine = Polygon(botLineVertices) @@ -171,6 +181,7 @@ def draw(self, renderer): self._set_text_position(renderer) self._text.draw(renderer) + class Table(Artist): """ Create a table of cells. @@ -492,7 +503,7 @@ def set_cell_type(self, cellType): else: raise ValueError('Unrecognized cell type %s; ' - 'try default or scicell' % cellType) + 'Has to be default or scicell' % cellType) def table(ax, @@ -573,7 +584,7 @@ def table(ax, # Now create the table table = Table(ax, loc, bbox, **kwargs) height = table._approx_text_height() - + if cellType is not None: table.set_cell_type(cellType) From fbdc81a335e371d4f620218b984b24236530c675 Mon Sep 17 00:00:00 2001 From: Anthony Cho Date: Thu, 19 Mar 2015 23:54:55 -0400 Subject: [PATCH 3/6] pull request fixes for issue #4044 --- lib/matplotlib/table.py | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index c870da041dff..b0283b2e3e6c 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -152,11 +152,6 @@ class SciCell(Cell): """ - def __init__(self, *args, **kwargs): - - # Call base - Cell.__init__(self, *args, **kwargs) - @allow_rasterization def draw(self, renderer): if not self.get_visible(): @@ -248,15 +243,13 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs): self._cachedRenderer = None self._cellType = 'default' + self._cellCreation = {"default" : Cell, "scicell" : SciCell} def add_cell(self, row, col, *args, **kwargs): """ Add a cell to the table. """ xy = (0, 0) - if self._cellType == 'default': - cell = Cell(xy, *args, **kwargs) - else: - cell = SciCell(xy, *args, **kwargs) + cell = self._cellCreation[self._cellType](xy, *args, **kwargs) cell.set_figure(self.figure) cell.set_transform(self.get_transform()) @@ -494,24 +487,29 @@ def get_celld(self): 'return a dict of cells in the table' return self._cells - def get_cell_type(self): + @property + def cellType(self): return self._cellType - def set_cell_type(self, cellType): - if cellType in ('default', 'scicell'): + @cellType.setter + def cellType(self, cellType): + if cellType is None: + self._cellType = 'default' + + elif cellType in ('default', 'scicell'): self._cellType = cellType else: - raise ValueError('Unrecognized cell type %s; ' - 'Has to be default or scicell' % cellType) + raise ValueError('Unrecognized cellType %s; ' + 'must be "default" or "scicell"' % cellType) def table(ax, - cellType=None, cellText=None, cellColours=None, + cellText=None, cellColours=None, cellLoc='right', colWidths=None, rowLabels=None, rowColours=None, rowLoc='left', colLabels=None, colColours=None, colLoc='center', - loc='bottom', bbox=None, + loc='bottom', bbox=None, cellType=None, **kwargs): """ TABLE(cellType='default', cellText=None, cellColours=None, @@ -524,8 +522,8 @@ def table(ax, Thanks to John Gill for providing the class and table. """ - if cellType is not None: - assert cellType in ('default', 'scicell') + if cellType is not None and cellType not in ('default', 'scicell'): + raise ValueError('cellType must be "default" or "scicell" instead of %s ' % cellType) # Check we have some cellText if cellText is None: @@ -584,9 +582,7 @@ def table(ax, # Now create the table table = Table(ax, loc, bbox, **kwargs) height = table._approx_text_height() - - if cellType is not None: - table.set_cell_type(cellType) + table.cellType = cellType # Add the cells for row in xrange(rows): From 3efbf16123928bd0abb2c0b9f1a9df2763c11195 Mon Sep 17 00:00:00 2001 From: Anthony Cho Date: Fri, 20 Mar 2015 00:27:08 -0400 Subject: [PATCH 4/6] conform to pep8 --- lib/matplotlib/table.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index b0283b2e3e6c..c6e51984b2fd 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -243,7 +243,7 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs): self._cachedRenderer = None self._cellType = 'default' - self._cellCreation = {"default" : Cell, "scicell" : SciCell} + self._cellCreation = {"default": Cell, "scicell": SciCell} def add_cell(self, row, col, *args, **kwargs): """ Add a cell to the table. """ @@ -523,7 +523,8 @@ def table(ax, Thanks to John Gill for providing the class and table. """ if cellType is not None and cellType not in ('default', 'scicell'): - raise ValueError('cellType must be "default" or "scicell" instead of %s ' % cellType) + raise ValueError('cellType must be "default" or "scicell" ' + 'instead of %s ' % cellType) # Check we have some cellText if cellText is None: From 74676a4c1ea56c494734e5f16ae4ee330fc57afd Mon Sep 17 00:00:00 2001 From: Anthony Cho Date: Fri, 20 Mar 2015 11:56:42 -0400 Subject: [PATCH 5/6] pull request changes for #4044, made the available cell types a class level dictionary and modified code to use it. --- lib/matplotlib/table.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index c6e51984b2fd..afa6c47d7ada 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -212,6 +212,7 @@ class Table(Artist): FONTSIZE = 10 AXESPAD = 0.02 # the border between the axes and table edge + AVAILABLECELLTYPES = {"default": 0, "scicell": 1} def __init__(self, ax, loc=None, bbox=None, **kwargs): @@ -243,13 +244,15 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs): self._cachedRenderer = None self._cellType = 'default' - self._cellCreation = {"default": Cell, "scicell": SciCell} def add_cell(self, row, col, *args, **kwargs): """ Add a cell to the table. """ xy = (0, 0) - cell = self._cellCreation[self._cellType](xy, *args, **kwargs) + if self.AVAILABLECELLTYPES[self._cellType] == 1: + cell = SciCell(xy, *args, **kwargs) + else: + cell = Cell(xy, *args, **kwargs) cell.set_figure(self.figure) cell.set_transform(self.get_transform()) @@ -496,10 +499,11 @@ def cellType(self, cellType): if cellType is None: self._cellType = 'default' - elif cellType in ('default', 'scicell'): + elif cellType in self.AVAILABLECELLTYPES: self._cellType = cellType else: + raise ValueError('Unrecognized cellType %s; ' 'must be "default" or "scicell"' % cellType) @@ -522,7 +526,7 @@ def table(ax, Thanks to John Gill for providing the class and table. """ - if cellType is not None and cellType not in ('default', 'scicell'): + if cellType is not None and cellType not in Table.AVAILABLECELLTYPES: raise ValueError('cellType must be "default" or "scicell" ' 'instead of %s ' % cellType) From 0fa3b196af9774e8fb8ed44c4188c582807d869d Mon Sep 17 00:00:00 2001 From: Anthony Cho Date: Fri, 20 Mar 2015 13:51:09 -0400 Subject: [PATCH 6/6] pull request changes for #4044 --- lib/matplotlib/table.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index afa6c47d7ada..2d51aa9f70cf 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -212,7 +212,7 @@ class Table(Artist): FONTSIZE = 10 AXESPAD = 0.02 # the border between the axes and table edge - AVAILABLECELLTYPES = {"default": 0, "scicell": 1} + AVAILABLECELLTYPES = {"default": Cell, "scicell": SciCell} def __init__(self, ax, loc=None, bbox=None, **kwargs): @@ -249,10 +249,7 @@ def add_cell(self, row, col, *args, **kwargs): """ Add a cell to the table. """ xy = (0, 0) - if self.AVAILABLECELLTYPES[self._cellType] == 1: - cell = SciCell(xy, *args, **kwargs) - else: - cell = Cell(xy, *args, **kwargs) + cell = self.AVAILABLECELLTYPES[self.cellType](xy, *args, **kwargs) cell.set_figure(self.figure) cell.set_transform(self.get_transform()) @@ -503,9 +500,9 @@ def cellType(self, cellType): self._cellType = cellType else: - + cellTypes = ', '.join([k for k in self.AVAILABLECELLTYPES.keys()]) raise ValueError('Unrecognized cellType %s; ' - 'must be "default" or "scicell"' % cellType) + 'must be one of the following: %s' % (cellType, cellTypes)) def table(ax, @@ -527,8 +524,9 @@ def table(ax, Thanks to John Gill for providing the class and table. """ if cellType is not None and cellType not in Table.AVAILABLECELLTYPES: - raise ValueError('cellType must be "default" or "scicell" ' - 'instead of %s ' % cellType) + cellTypes = ', '.join([k for k in Table.AVAILABLECELLTYPES.keys()]) + raise ValueError('Unrecognized cellType %s; ' + 'must be one of the following: %s' % (cellType, cellTypes)) # Check we have some cellText if cellText is None: