Skip to content

Commit 3c702c4

Browse files
authored
Merge pull request matplotlib#12218 from timhoffm/doc-table
Improve table docs
2 parents f8b425d + ba4ad6d commit 3c702c4

File tree

1 file changed

+69
-25
lines changed

1 file changed

+69
-25
lines changed

lib/matplotlib/table.py

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
"""
2-
Place a table below the x-axis at location loc.
3-
4-
The table consists of a grid of cells.
1+
# Original code by:
2+
# John Gill <jng@europe.renre.com>
3+
# Copyright 2004 John Gill and John Hunter
4+
#
5+
# Subsequent changes:
6+
# The Matplotlib development team
7+
# Copyright The Matplotlib development team
58

6-
The grid need not be rectangular and can have holes.
7-
8-
Cells are added by specifying their row and column.
9+
"""
10+
This module provides functionality to add a table to a plot.
911
10-
For the purposes of positioning the cell at (0, 0) is
11-
assumed to be at the top left and the cell at (max_row, max_col)
12-
is assumed to be at bottom right.
12+
Use the factory function `~matplotlib.table.table` to create a ready-made
13+
table from texts. If you need more control, use the `.Table` class and its
14+
methods.
1315
14-
You can add additional cells outside this range to have convenient
15-
ways of positioning more interesting grids.
16+
The table consists of a grid of cells, which are indexed by (row, column).
17+
The cell (0, 0) is positioned at the top left.
1618
17-
Author : John Gill <jng@europe.renre.com>
18-
Copyright : 2004 John Gill and John Hunter
19-
License : matplotlib license
19+
Thanks to John Gill for providing the class and table.
2020
"""
2121

2222
from . import artist, cbook, docstring
@@ -29,9 +29,38 @@
2929

3030
class Cell(Rectangle):
3131
"""
32-
A cell is a `.Rectangle` with some associated text.
32+
A cell is a `.Rectangle` with some associated `.Text`.
33+
34+
.. note:
35+
As a user, you'll most likely not creates cells yourself. Instead, you
36+
should use either the `~matplotlib.table.table` factory function or
37+
`.Table.add_cell`.
38+
39+
Parameters
40+
----------
41+
xy : 2-tuple
42+
The position of the bottom left corner of the cell.
43+
width : float
44+
The cell width.
45+
height : float
46+
The cell height.
47+
edgecolor : color spec
48+
The color of the cell border.
49+
facecolor : color spec
50+
The cell facecolor.
51+
fill : bool
52+
Whether the cell background is filled.
53+
text : str
54+
The cell text.
55+
loc : {'left', 'center', 'right'}, default: 'right'
56+
The alignment of the text within the cell.
57+
fontproperties : dict
58+
A dict defining the font properties of the text. Supported keys and
59+
values are the keyword arguments accepted by `.FontProperties`.
3360
"""
34-
PAD = 0.1 # padding between text and rectangle
61+
62+
PAD = 0.1
63+
"""Padding between text and rectangle."""
3564

3665
def __init__(self, xy, width, height,
3766
edgecolor='k', facecolor='w',
@@ -42,7 +71,7 @@ def __init__(self, xy, width, height,
4271
):
4372

4473
# Call base
45-
Rectangle.__init__(self, xy, width=width, height=height,
74+
Rectangle.__init__(self, xy, width=width, height=height, fill=fill,
4675
edgecolor=edgecolor, facecolor=facecolor)
4776
self.set_clip_on(False)
4877

@@ -68,6 +97,7 @@ def get_text(self):
6897
return self._text
6998

7099
def set_fontsize(self, size):
100+
"""Set the text fontsize."""
71101
self._text.set_fontsize(size)
72102
self.stale = True
73103

@@ -76,7 +106,7 @@ def get_fontsize(self):
76106
return self._text.get_fontsize()
77107

78108
def auto_set_font_size(self, renderer):
79-
""" Shrink font size until text fits. """
109+
"""Shrink font size until the text fits into the cell width."""
80110
fontsize = self.get_fontsize()
81111
required = self.get_required_width(renderer)
82112
while fontsize > 1 and required > self.get_width():
@@ -99,7 +129,7 @@ def draw(self, renderer):
99129
self.stale = False
100130

101131
def _set_text_position(self, renderer):
102-
""" Set text up so it draws in the right place.
132+
"""Set text up so it draws in the right place.
103133
104134
Currently support 'left', 'center' and 'right'
105135
"""
@@ -124,18 +154,26 @@ def _set_text_position(self, renderer):
124154
self._text.set_position((x, y))
125155

126156
def get_text_bounds(self, renderer):
127-
""" Get text bounds in axes co-ordinates. """
157+
"""
158+
Return the text bounds as *(x, y, width, height)* in table coordinates.
159+
"""
128160
bbox = self._text.get_window_extent(renderer)
129161
bboxa = bbox.inverse_transformed(self.get_data_transform())
130162
return bboxa.bounds
131163

132164
def get_required_width(self, renderer):
133-
""" Get width required for this cell. """
165+
"""Return the minimal required width for the cell."""
134166
l, b, w, h = self.get_text_bounds(renderer)
135167
return w * (1.0 + (2.0 * self.PAD))
136168

169+
@docstring.dedent_interpd
137170
def set_text_props(self, **kwargs):
138-
'update the text properties with kwargs'
171+
"""
172+
Update the text properties.
173+
174+
Valid kwargs are
175+
%(Text)s
176+
"""
139177
self._text.update(kwargs)
140178
self.stale = True
141179

@@ -209,6 +247,14 @@ class Table(Artist):
209247
210248
Column widths and row heights for the table can be specified.
211249
250+
The table consists of a grid of cells, which are indexed by (row, column).
251+
252+
For a simple table, you'll have a full grid of cells with indices from
253+
(0, 0) to (num_rows-1, num_cols-1), in which the cell (0, 0) is positioned
254+
at the top left. However, you can also add cells with negative indices.
255+
You don't have to add a cell to every grid position, so you can create
256+
tables that have holes.
257+
212258
Return value is a sequence of text, line and patch instances that make
213259
up the table
214260
"""
@@ -590,8 +636,6 @@ def table(ax,
590636
loc='bottom', bbox=None, edges='closed')
591637
592638
Factory function to generate a Table instance.
593-
594-
Thanks to John Gill for providing the class and table.
595639
"""
596640

597641
if cellColours is None and cellText is None:

0 commit comments

Comments
 (0)