Skip to content

Commit f11bb1a

Browse files
committed
More table documentation
1 parent 96522ff commit f11bb1a

File tree

1 file changed

+157
-39
lines changed

1 file changed

+157
-39
lines changed

lib/matplotlib/table.py

Lines changed: 157 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def set_text_props(self, **kwargs):
180180

181181
class CustomCell(Cell):
182182
"""
183-
A subclass of Cell where the sides may be visibly toggled.
183+
A subclass of `.Cell` with configurable edge visibility.
184184
"""
185185

186186
_edges = 'BRTL'
@@ -196,6 +196,15 @@ def __init__(self, *args, visible_edges, **kwargs):
196196

197197
@property
198198
def visible_edges(self):
199+
"""
200+
The cell edges to be drawn with a line.
201+
202+
Reading this property returns a substring of 'BRTL' (bottom, right,
203+
top, left').
204+
205+
When setting this property, you can use a substring of 'BRTL' or one
206+
of {'open', 'closed', 'horizontal', 'vertical'}.
207+
"""
199208
return self._visible_edges
200209

201210
@visible_edges.setter
@@ -216,9 +225,7 @@ def visible_edges(self, value):
216225
self.stale = True
217226

218227
def get_path(self):
219-
"""
220-
Return a path where the edges specified by _visible_edges are drawn.
221-
"""
228+
"""Return a `.Path` for the `.visible_edges`."""
222229
codes = [Path.MOVETO]
223230

224231
for edge in self._edges:
@@ -239,13 +246,7 @@ def get_path(self):
239246

240247
class Table(Artist):
241248
"""
242-
Create a table of cells.
243-
244-
Table can have (optional) row and column headers.
245-
246-
Each entry in the table can be either text or patches.
247-
248-
Column widths and row heights for the table can be specified.
249+
A table of cells.
249250
250251
The table consists of a grid of cells, which are indexed by (row, column).
251252
@@ -255,8 +256,8 @@ class Table(Artist):
255256
You don't have to add a cell to every grid position, so you can create
256257
tables that have holes.
257258
258-
Return value is a sequence of text, line and patch instances that make
259-
up the table
259+
*Note*: You'll usually not create an empty table from scratch. Instead use
260+
`~matplotlib.table.table` to create a table from data.
260261
"""
261262
codes = {'best': 0,
262263
'upper right': 1, # default
@@ -277,11 +278,32 @@ class Table(Artist):
277278
'top': 16,
278279
'bottom': 17,
279280
}
281+
"""Possible values where to place the table relative to the Axes."""
280282

281283
FONTSIZE = 10
282-
AXESPAD = 0.02 # the border between the axes and table edge
284+
285+
AXESPAD = 0.02
286+
"""The border between the Axes and the table edge."""
283287

284288
def __init__(self, ax, loc=None, bbox=None, **kwargs):
289+
"""
290+
Parameters
291+
----------
292+
ax : `matplotlib.axes.Axes`
293+
The `~axes.Axes` to plot the table into.
294+
loc : str
295+
The position of the cell with respect to *ax*. This must be one of
296+
the `~.Table.codes`.
297+
bbox : `.Bbox` or None
298+
A bounding box to draw the table into. If this is not *None*, this
299+
overrides *loc*.
300+
301+
Other Parameters
302+
----------------
303+
**kwargs
304+
`.Artist` properties.
305+
306+
"""
285307

286308
Artist.__init__(self)
287309

@@ -314,18 +336,21 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs):
314336

315337
def add_cell(self, row, col, *args, **kwargs):
316338
"""
317-
Add a cell to the table.
339+
Create a cell and add it to the table.
318340
319341
Parameters
320342
----------
321343
row : int
322344
Row index.
323345
col : int
324346
Column index.
347+
*args, **kwargs
348+
All other parameters are passed on to `Cell`.
325349
326350
Returns
327351
-------
328-
`CustomCell`: Automatically created cell
352+
`CustomCell`
353+
The created cell.
329354
330355
"""
331356
xy = (0, 0)
@@ -361,6 +386,21 @@ def __getitem__(self, position):
361386

362387
@property
363388
def edges(self):
389+
"""
390+
The default value of `~.CustomCell.visible_edges` for newly added
391+
cells using `.add_cell`.
392+
393+
Notes
394+
-----
395+
This setting does currently only affect newly created cells using
396+
`.add_cell`.
397+
398+
To change existing cells, you have to set their edges explicitly:
399+
400+
>>> for c in tab.get_celld().values():
401+
>>> c.visible_edges = 'horizontal'
402+
403+
"""
364404
return self._edges
365405

366406
@edges.setter
@@ -374,6 +414,8 @@ def _approx_text_height(self):
374414

375415
@allow_rasterization
376416
def draw(self, renderer):
417+
# docstring inherited
418+
377419
# Need a renderer to do hit tests on mouseevent; assume the last one
378420
# will do
379421
if renderer is None:
@@ -403,10 +445,7 @@ def _get_grid_bbox(self, renderer):
403445
return bbox.inverse_transformed(self.get_transform())
404446

405447
def contains(self, mouseevent):
406-
"""Test whether the mouse event occurred in the table.
407-
408-
Returns T/F, {}
409-
"""
448+
# docstring inherited
410449
if callable(self._contains):
411450
return self._contains(self, mouseevent)
412451

@@ -475,17 +514,11 @@ def auto_set_column_width(self, col):
475514
0: the 1st column
476515
1: the 2nd column
477516
478-
Args:
479-
col(List): list of indexs
480-
>>>table.auto_set_column_width([-1,0,1])
481-
482-
col(Tuple): tuple of indexs
483-
>>>table.auto_set_column_width((-1,0,1))
517+
Parameters
518+
----------
519+
col : int or sequence of ints
520+
The indices of the columns to auto-scale.
484521
485-
col(int): index integer
486-
>>>table.auto_set_column_width(-1)
487-
>>>table.auto_set_column_width(0)
488-
>>>table.auto_set_column_width(1)
489522
"""
490523
# check for col possibility on iteration
491524
try:
@@ -536,7 +569,7 @@ def _auto_set_font_size(self, renderer):
536569
cell.set_fontsize(fontsize)
537570

538571
def scale(self, xscale, yscale):
539-
""" Scale column widths by xscale and row heights by yscale. """
572+
"""Scale column widths by *xscale* and row heights by *yscale*."""
540573
for c in self._cells.values():
541574
c.set_width(c.get_width() * xscale)
542575
c.set_height(c.get_height() * yscale)
@@ -548,8 +581,20 @@ def set_fontsize(self, size):
548581
Parameters
549582
----------
550583
size : float
551-
"""
552584
585+
Notes
586+
-----
587+
As long as auto font size has not been disabled, the value will be
588+
clipped such that the text will still fit into the table cell.
589+
590+
You can disable this behavior Using `.auto_set_font_size`.
591+
592+
>>> the_table.auto_set_font_size(False)
593+
>>> the_table.set_fontsize(20)
594+
595+
However, there is no automatic scaling of the row height so that the
596+
text may exceed the cell boundary.
597+
"""
553598
for cell in self._cells.values():
554599
cell.set_fontsize(size)
555600
self.stale = True
@@ -617,7 +662,18 @@ def _update_positions(self, renderer):
617662
self._offset(ox, oy)
618663

619664
def get_celld(self):
620-
"""Return a dict of cells in the table."""
665+
r"""
666+
Return a dict of cells in the table mapping indices *(row, column)* to
667+
`.Cell`\s.
668+
669+
Notes
670+
-----
671+
You can also directly index into the Table object to access individual
672+
cells::
673+
674+
cell = table[row, col]
675+
676+
"""
621677
return self._cells
622678

623679

@@ -629,13 +685,75 @@ def table(ax,
629685
loc='bottom', bbox=None, edges='closed',
630686
**kwargs):
631687
"""
632-
TABLE(cellText=None, cellColours=None,
633-
cellLoc='right', colWidths=None,
634-
rowLabels=None, rowColours=None, rowLoc='left',
635-
colLabels=None, colColours=None, colLoc='center',
636-
loc='bottom', bbox=None, edges='closed')
688+
Add a table to an `~.axes.Axes`.
689+
690+
You have to specify at least one of *cellText* or *cellColours*. These
691+
parameters must be 2D lists, in which the outer lists define the rows and
692+
the inner list define the column values per row. Each row must have the
693+
same number of elements.
694+
695+
The table can optionally have row and column headers, which are configured
696+
using *rowLabels*, *rowColours*, *rowLoc* and *colLabels*, *colColours*,
697+
*colLoc* respectively.
698+
699+
Parameters
700+
----------
701+
cellText : 2D list of str, optional
702+
The texts to place into the table cells.
703+
704+
*Note*: Line breaks in the strings are currently not accounted for and
705+
will result in the text exceeding the cell boundaries.
706+
707+
cellColours : 2D list of matplotlib color specs, optional
708+
The background colors of the cells.
709+
710+
cellLoc : {'left', 'center', 'right'}, default: 'right'
711+
The alignment of the text within the cells.
712+
713+
colWidths : list of float, optional
714+
The column widths in units of the axes. If not given, all columns will
715+
have a width of *1 / ncols*.
716+
717+
rowLabels : list of str, optional
718+
The text of the row header cells.
719+
720+
rowColours : list of matplotlib color specs, optional
721+
The colors of the row header cells.
722+
723+
rowLoc : {'left', 'center', 'right'}, optional, default: 'left'
724+
The text alignment of the row header cells.
725+
726+
colLabels : list of str, optional
727+
The text of the column header cells.
728+
729+
colColours : list of matplotlib color specs, optional
730+
The colors of the column header cells.
731+
732+
rowLoc : {'left', 'center', 'right'}, optional, default: 'left'
733+
The text alignment of the column header cells.
734+
735+
loc : str, optional
736+
The position of the cell with respect to *ax*. This must be one of
737+
the `~.Table.codes`.
738+
739+
bbox : `.Bbox`, optional
740+
A bounding box to draw the table into. If this is not *None*, this
741+
overrides *loc*.
742+
743+
edges : substring of 'BRTL' or {'open', 'closed', 'horizontal', 'vertical'}
744+
The cell edges to be drawn with a line. See also
745+
`~.CustomCell.visible_edges`.
746+
747+
Other Parameters
748+
----------------
749+
**kwargs
750+
`.Artist` properties.
751+
752+
Returns
753+
-------
754+
table : `~matplotlib.table.Table`
755+
The created table.
637756
638-
Factory function to generate a Table instance.
639757
"""
640758

641759
if cellColours is None and cellText is None:

0 commit comments

Comments
 (0)