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

Skip to content

Commit 9c56881

Browse files
authored
Merge pull request #12880 from timhoffm/doc-table-2
More table documentation
2 parents 72d9a86 + 131f52b commit 9c56881

File tree

1 file changed

+159
-49
lines changed

1 file changed

+159
-49
lines changed

lib/matplotlib/table.py

Lines changed: 159 additions & 49 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 `.Cell` subclass 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
@@ -215,9 +224,7 @@ def visible_edges(self, value):
215224
self.stale = True
216225

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

223230
for edge in self._edges:
@@ -238,13 +245,7 @@ def get_path(self):
238245

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

280282
FONTSIZE = 10
281-
AXESPAD = 0.02 # the border between the axes and table edge
283+
284+
AXESPAD = 0.02
285+
"""The border between the Axes and the table edge in Axes units."""
282286

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

285306
Artist.__init__(self)
286307

@@ -313,18 +334,21 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs):
313334

314335
def add_cell(self, row, col, *args, **kwargs):
315336
"""
316-
Add a cell to the table.
337+
Create a cell and add it to the table.
317338
318339
Parameters
319340
----------
320341
row : int
321342
Row index.
322343
col : int
323344
Column index.
345+
*args, **kwargs
346+
All other parameters are passed on to `Cell`.
324347
325348
Returns
326349
-------
327-
`CustomCell`: Automatically created cell
350+
cell : `.CustomCell`
351+
The created cell.
328352
329353
"""
330354
xy = (0, 0)
@@ -354,6 +378,21 @@ def __getitem__(self, position):
354378

355379
@property
356380
def edges(self):
381+
"""
382+
The default value of `~.CustomCell.visible_edges` for newly added
383+
cells using `.add_cell`.
384+
385+
Notes
386+
-----
387+
This setting does currently only affect newly created cells using
388+
`.add_cell`.
389+
390+
To change existing cells, you have to set their edges explicitly::
391+
392+
for c in tab.get_celld().values():
393+
c.visible_edges = 'horizontal'
394+
395+
"""
357396
return self._edges
358397

359398
@edges.setter
@@ -367,6 +406,8 @@ def _approx_text_height(self):
367406

368407
@allow_rasterization
369408
def draw(self, renderer):
409+
# docstring inherited
410+
370411
# Need a renderer to do hit tests on mouseevent; assume the last one
371412
# will do
372413
if renderer is None:
@@ -396,10 +437,7 @@ def _get_grid_bbox(self, renderer):
396437
return bbox.inverse_transformed(self.get_transform())
397438

398439
def contains(self, mouseevent):
399-
"""Test whether the mouse event occurred in the table.
400-
401-
Returns T/F, {}
402-
"""
440+
# docstring inherited
403441
if callable(self._contains):
404442
return self._contains(self, mouseevent)
405443

@@ -459,26 +497,13 @@ def _do_cell_alignment(self):
459497
cell.set_y(bottoms[row])
460498

461499
def auto_set_column_width(self, col):
462-
""" Given column indexs in either List, Tuple or int. Will be able to
463-
automatically set the columns into optimal sizes.
464-
465-
Here is the example of the input, which triger automatic adjustment on
466-
columns to optimal size by given index numbers.
467-
-1: the row labling
468-
0: the 1st column
469-
1: the 2nd column
470-
471-
Args:
472-
col(List): list of indexs
473-
>>>table.auto_set_column_width([-1,0,1])
474-
475-
col(Tuple): tuple of indexs
476-
>>>table.auto_set_column_width((-1,0,1))
477-
478-
col(int): index integer
479-
>>>table.auto_set_column_width(-1)
480-
>>>table.auto_set_column_width(0)
481-
>>>table.auto_set_column_width(1)
500+
"""
501+
Automatically set the widths of given columns to optimal sizes.
502+
503+
Parameters
504+
----------
505+
col : int or sequence of ints
506+
The indices of the columns to auto-scale.
482507
"""
483508
# check for col possibility on iteration
484509
try:
@@ -529,7 +554,7 @@ def _auto_set_font_size(self, renderer):
529554
cell.set_fontsize(fontsize)
530555

531556
def scale(self, xscale, yscale):
532-
""" Scale column widths by xscale and row heights by yscale. """
557+
"""Scale column widths by *xscale* and row heights by *yscale*."""
533558
for c in self._cells.values():
534559
c.set_width(c.get_width() * xscale)
535560
c.set_height(c.get_height() * yscale)
@@ -541,8 +566,20 @@ def set_fontsize(self, size):
541566
Parameters
542567
----------
543568
size : float
544-
"""
545569
570+
Notes
571+
-----
572+
As long as auto font size has not been disabled, the value will be
573+
clipped such that the text fits horizontally into the cell.
574+
575+
You can disable this behavior using `.auto_set_font_size`.
576+
577+
>>> the_table.auto_set_font_size(False)
578+
>>> the_table.set_fontsize(20)
579+
580+
However, there is no automatic scaling of the row height so that the
581+
text may exceed the cell boundary.
582+
"""
546583
for cell in self._cells.values():
547584
cell.set_fontsize(size)
548585
self.stale = True
@@ -610,7 +647,18 @@ def _update_positions(self, renderer):
610647
self._offset(ox, oy)
611648

612649
def get_celld(self):
613-
"""Return a dict of cells in the table."""
650+
r"""
651+
Return a dict of cells in the table mapping *(row, column)* to
652+
`.Cell`\s.
653+
654+
Notes
655+
-----
656+
You can also directly index into the Table object to access individual
657+
cells::
658+
659+
cell = table[row, col]
660+
661+
"""
614662
return self._cells
615663

616664

@@ -622,13 +670,75 @@ def table(ax,
622670
loc='bottom', bbox=None, edges='closed',
623671
**kwargs):
624672
"""
625-
TABLE(cellText=None, cellColours=None,
626-
cellLoc='right', colWidths=None,
627-
rowLabels=None, rowColours=None, rowLoc='left',
628-
colLabels=None, colColours=None, colLoc='center',
629-
loc='bottom', bbox=None, edges='closed')
673+
Add a table to an `~.axes.Axes`.
674+
675+
At least one of *cellText* or *cellColours* must be specified. These
676+
parameters must be 2D lists, in which the outer lists define the rows and
677+
the inner list define the column values per row. Each row must have the
678+
same number of elements.
679+
680+
The table can optionally have row and column headers, which are configured
681+
using *rowLabels*, *rowColours*, *rowLoc* and *colLabels*, *colColours*,
682+
*colLoc* respectively.
683+
684+
Parameters
685+
----------
686+
cellText : 2D list of str, optional
687+
The texts to place into the table cells.
688+
689+
*Note*: Line breaks in the strings are currently not accounted for and
690+
will result in the text exceeding the cell boundaries.
691+
692+
cellColours : 2D list of matplotlib color specs, optional
693+
The background colors of the cells.
694+
695+
cellLoc : {'left', 'center', 'right'}, default: 'right'
696+
The alignment of the text within the cells.
697+
698+
colWidths : list of float, optional
699+
The column widths in units of the axes. If not given, all columns will
700+
have a width of *1 / ncols*.
701+
702+
rowLabels : list of str, optional
703+
The text of the row header cells.
704+
705+
rowColours : list of matplotlib color specs, optional
706+
The colors of the row header cells.
707+
708+
rowLoc : {'left', 'center', 'right'}, optional, default: 'left'
709+
The text alignment of the row header cells.
710+
711+
colLabels : list of str, optional
712+
The text of the column header cells.
713+
714+
colColours : list of matplotlib color specs, optional
715+
The colors of the column header cells.
716+
717+
rowLoc : {'left', 'center', 'right'}, optional, default: 'left'
718+
The text alignment of the column header cells.
719+
720+
loc : str, optional
721+
The position of the cell with respect to *ax*. This must be one of
722+
the `~.Table.codes`.
723+
724+
bbox : `.Bbox`, optional
725+
A bounding box to draw the table into. If this is not *None*, this
726+
overrides *loc*.
727+
728+
edges : substring of 'BRTL' or {'open', 'closed', 'horizontal', 'vertical'}
729+
The cell edges to be drawn with a line. See also
730+
`~.CustomCell.visible_edges`.
731+
732+
Other Parameters
733+
----------------
734+
**kwargs
735+
`.Artist` properties.
736+
737+
Returns
738+
-------
739+
table : `~matplotlib.table.Table`
740+
The created table.
630741
631-
Factory function to generate a Table instance.
632742
"""
633743

634744
if cellColours is None and cellText is None:

0 commit comments

Comments
 (0)