8000 Improve table docs · matplotlib/matplotlib@1c28034 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1c28034

Browse files
committed
Improve table docs
1 parent 03b07fb commit 1c28034

File tree

1 file changed

+64
-25
lines changed

1 file changed

+64
-25
lines changed

lib/matplotlib/table.py

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
"""
2-
Place a table below the x-axis at location loc.
2+
:Author: John Gill <jng@europe.renre.com> and others
3+
:Copyright: 2004 John Gill and John Hunter
34
4-
The table consists of a grid of cells.
5+
This module provides functionality to add a table to a plot.
56
6-
The grid need not be rectangular and can have holes.
7+
Use the factory function `~matplotlib.table.table` to create a ready-made
8+
table from texts. If you need more control, use the `.Table` class and its
9+
methods.
710
8-
Cells are added by specifying their row and column.
9-
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.
13-
14-
You can add additional cells outside this range to have convenient
15-
ways of positioning more interesting grids.
16-
17-
Author : John Gill <jng@europe.renre.com>
18-
Copyright : 2004 John Gill and John Hunter
19-
License : matplotlib license
11+
The table consists of a grid of cells, which are indexed by (row, column).
12+
The cell (0, 0) is positioned at the top left.
2013
14+
Thanks to John Gill for providing the class and table.
2115
"""
2216
import warnings
2317

@@ -31,9 +25,38 @@
3125

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

3861
def __init__(self, xy, width, height,
3962
edgecolor='k', facecolor='w',
@@ -44,7 +67,7 @@ def __init__(self, xy, width, height,
4467
):
4568

4669
# Call base
47-
Rectangle.__init__(self, xy, width=width, height=height,
70+
Rectangle.__init__(self, xy, width=width, height=height, fill=fill,
4871
edgecolor=edgecolor, facecolor=facecolor)
4972
self.set_clip_on(False)
5073

@@ -70,6 +93,7 @@ def get_text(self):
7093
return self._text
7194

7295
def set_fontsize(self, size):
96+
"""Set the text fontsize."""
7397
self._text.set_fontsize(size)
7498
self.stale = True
7599

@@ -78,7 +102,7 @@ def get_fontsize(self):
78102
return self._text.get_fontsize()
79103

80104
def auto_set_font_size(self, renderer):
81-
""" Shrink font size until text fits. """
105+
"""Shrink font size until the text fits into the cell width."""
82106
fontsize = self.get_fontsize()
83107
required = self.get_required_width(renderer)
84108
while fontsize > 1 and required > self.get_width():
@@ -101,7 +125,7 @@ def draw(self, renderer):
101125
self.stale = False
102126

103127
def _set_text_position(self, renderer):
104-
""" Set text up so it draws in the right place.
128+
"""Set text up so it draws in the right place.
105129
106130
Currently support 'left', 'center' and 'right'
107131
"""
@@ -126,18 +150,27 @@ def _set_text_position(self, renderer):
126150
self._text.set_position((x, y))
127151

128152
def get_text_bounds(self, renderer):
129-
""" Get text bounds in axes co-ordinates. """
153+
"""
154+
Return the bounds of the text as *(x, y, width, height)* in table
155+
coordinates.
156+
"""
130157
bbox = self._text.get_window_extent(renderer)
131158
bboxa = bbox.inverse_transformed(self.get_data_transform())
132159
return bboxa.bounds
133160

134161
def get_required_width(self, renderer):
135-
""" Get width required for this cell. """
162+
"""Return the minimal required width for the cell."""
136163
l, b, w, h = self.get_text_bounds(renderer)
137164
return w * (1.0 + (2.0 * self.PAD))
138165

166+
@docstring.dedent_interpd
139167
def set_text_props(self, **kwargs):
140-
'update the text properties with kwargs'
168+
"""
169+
Update the text properties.
170+
171+
Valid kwargs are
172+
%(Text)s
173+
"""
141174
self._text.update(kwargs)
142175
self.stale = True
143176

@@ -211,6 +244,14 @@ class Table(Artist):
211244
212245
Column widths and row heights for the table can be specified.
213246
247+
The table consists of a grid of cells, which are indexed by (row, column).
248+
249+
For a simple table, you'll have a full grid of cells with indices from
250+
(0, 0) to (max_rows-1, max_cols-1), in which the cell (0, 0) is positioned
251+
at the top left. However, you can also add cells with negative indices.
252+
You don't have to add a cell to every grid position, so you can create
253+
tables that have holes.
254+
214255
Return value is a sequence of text, line and patch instances that make
215256
up the table
216257
"""
@@ -590,8 +631,6 @@ def table(ax,
590631
loc='bottom', bbox=None, edges='closed')
591632
592633
Factory function to generate a Table instance.
593-
594-
Thanks to John Gill for providing the class and table.
595634
"""
596635

597636
if cellColours is None and cellText is None:

0 commit comments

Comments
 (0)
0