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

Skip to content

Commit 5afe234

Browse files
committed
Improve table docs
1 parent 03b07fb commit 5afe234

File tree

1 file changed

+70
-26
lines changed

1 file changed

+70
-26
lines changed

lib/matplotlib/table.py

Lines changed: 70 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
"""
2-
Place a table below the x-axis at location loc.
3-
4-
The table consists of a grid of cells.
5-
6-
The grid need not be rectangular and can have holes.
7-
8-
Cells are added by specifying their row and column.
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
98

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.
9+
"""
10+
This module provides functionality to add a table to a plot.
1311
14-
You can add additional cells outside this range to have convenient
15-
ways of positioning more interesting grids.
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.
1615
17-
Author : John Gill <jng@europe.renre.com>
18-
Copyright : 2004 John Gill and John Hunter
19-
License : matplotlib license
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.
2018
19+
Thanks to John Gill for providing the class and table.
2120
"""
2221
import warnings
2322

@@ -31,9 +30,38 @@
3130

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

3866
def __init__(self, xy, width, height,
3967
edgecolor='k', facecolor='w',
@@ -44,7 +72,7 @@ def __init__(self, xy, width, height,
4472
):
4573

4674
# Call base
47-
Rectangle.__init__(self, xy, width=width, height=height,
75+
Rectangle.__init__(self, xy, width=width, height=height, fill=fill,
4876
edgecolor=edgecolor, facecolor=facecolor)
4977
self.set_clip_on(False)
5078

@@ -70,6 +98,7 @@ def get_text(self):
7098
return self._text
7199

72100
def set_fontsize(self, size):
101+
"""Set the text fontsize."""
73102
self._text.set_fontsize(size)
74103
self.stale = True
75104

@@ -78,7 +107,7 @@ def get_fontsize(self):
78107
return self._text.get_fontsize()
79108

80109
def auto_set_font_size(self, renderer):
81-
""" Shrink font size until text fits. """
110+
"""Shrink font size until the text fits into the cell width."""
82111
fontsize = self.get_fontsize()
83112
required = self.get_required_width(renderer)
84113
while fontsize > 1 and required > self.get_width():
@@ -101,7 +130,7 @@ def draw(self, renderer):
101130
self.stale = False
102131

103132
def _set_text_position(self, renderer):
104-
""" Set text up so it draws in the right place.
133+
"""Set text up so it draws in the right place.
105134
106135
Currently support 'left', 'center' and 'right'
107136
"""
@@ -126,18 +155,27 @@ def _set_text_position(self, renderer):
126155
self._text.set_position((x, y))
127156

128157
def get_text_bounds(self, renderer):
129-
""" Get text bounds in axes co-ordinates. """
158+
"""
159+
Return the bounds of the text as *(x, y, width, height)* in table
160+
coordinates.
161+
"""
130162
bbox = self._text.get_window_extent(renderer)
131163
bboxa = bbox.inverse_transformed(self.get_data_transform())
132164
return bboxa.bounds
133165

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

171+
@docstring.dedent_interpd
139172
def set_text_props(self, **kwargs):
140-
'update the text properties with kwargs'
173+
"""
174+
Update the text properties.
175+
176+
Valid kwargs are
177+
%(Text)s
178+
"""
141179
self._text.update(kwargs)
142180
self.stale = True
143181

@@ -211,6 +249,14 @@ class Table(Artist):
211249
212250
Column widths and row heights for the table can be specified.
213251
252+
The table consists of a grid of cells, which are indexed by (row, column).
253+
254+
For a simple table, you'll have a full grid of cells with indices from
255+
(0, 0) to (max_rows-1, max_cols-1), in which the cell (0, 0) is positioned
256+
at the top left. However, you can also add cells with negative indices.
257+
You don't have to add a cell to every grid position, so you can create
258+
tables that have holes.
259+
214260
Return value is a sequence of text, line and patch instances that make
215261
up the table
216262
"""
@@ -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)
0