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

Skip to content

Implemented issue 4044. Created a Cell subclass, SciCell, to override th... #4245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 61 additions & 4 deletions lib/matplotlib/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -146,6 +146,37 @@ def set_text_props(self, **kwargs):
self._text.update(kwargs)


class SciCell(Cell):
"""
A SciCell is a Cell, but it only draws the horizontal lines of the 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):
"""
Create a table of cells.
Expand Down Expand Up @@ -181,6 +212,7 @@ class Table(Artist):

FONTSIZE = 10
AXESPAD = 0.02 # the border between the axes and table edge
AVAILABLECELLTYPES = {"default": Cell, "scicell": SciCell}

def __init__(self, ax, loc=None, bbox=None, **kwargs):

Expand Down Expand Up @@ -211,12 +243,14 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs):
self.set_clip_on(False)

self._cachedRenderer = None
self._cellType = 'default'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

underscores instead of camel case please. Yes, the code base is mixed, but lets not make it worse with new additions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, scratch that, better to match internally in this class.


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

cell = Cell(xy, *args, **kwargs)
cell = self.AVAILABLECELLTYPES[self.cellType](xy, *args, **kwargs)

cell.set_figure(self.figure)
cell.set_transform(self.get_transform())

Expand Down Expand Up @@ -453,16 +487,33 @@ def get_celld(self):
'return a dict of cells in the table'
return self._cells

@property
def cellType(self):
return self._cellType

@cellType.setter
def cellType(self, cellType):
if cellType is None:
self._cellType = 'default'

elif cellType in self.AVAILABLECELLTYPES:
self._cellType = cellType

else:
cellTypes = ', '.join([k for k in self.AVAILABLECELLTYPES.keys()])
raise ValueError('Unrecognized cellType %s; '
'must be one of the following: %s' % (cellType, cellTypes))


def table(ax,
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(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',
Expand All @@ -472,6 +523,11 @@ def table(ax,

Thanks to John Gill for providing the class and table.
"""
if cellType is not None and cellType not in Table.AVAILABLECELLTYPES:
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:
# assume just colours are needed
Expand Down Expand Up @@ -529,6 +585,7 @@ def table(ax,
# Now create the table
table = Table(ax, loc, bbox, **kwargs)
height = table._approx_text_height()
table.cellType = cellType

# Add the cells
for row in xrange(rows):
Expand Down