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
5
8
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.
9
11
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 .
13
15
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 .
16
18
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.
20
20
"""
21
21
22
22
from . import artist , cbook , docstring
29
29
30
30
class Cell (Rectangle ):
31
31
"""
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`.
33
60
"""
34
- PAD = 0.1 # padding between text and rectangle
61
+
62
+ PAD = 0.1
63
+ """Padding between text and rectangle."""
35
64
36
65
def __init__ (self , xy , width , height ,
37
66
edgecolor = 'k' , facecolor = 'w' ,
@@ -42,7 +71,7 @@ def __init__(self, xy, width, height,
42
71
):
43
72
44
73
# Call base
45
- Rectangle .__init__ (self , xy , width = width , height = height ,
74
+ Rectangle .__init__ (self , xy , width = width , height = height , fill = fill ,
46
75
edgecolor = edgecolor , facecolor = facecolor )
47
76
self .set_clip_on (False )
48
77
@@ -68,6 +97,7 @@ def get_text(self):
68
97
return self ._text
69
98
70
99
def set_fontsize (self , size ):
100
+ """Set the text fontsize."""
71
101
self ._text .set_fontsize (size )
72
102
self .stale = True
73
103
@@ -76,7 +106,7 @@ def get_fontsize(self):
76
106
return self ._text .get_fontsize ()
77
107
78
108
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. """
80
110
fontsize = self .get_fontsize ()
81
111
required = self .get_required_width (renderer )
82
112
while fontsize > 1 and required > self .get_width ():
@@ -99,7 +129,7 @@ def draw(self, renderer):
99
129
self .stale = False
100
130
101
131
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.
103
133
104
134
Currently support 'left', 'center' and 'right'
105
135
"""
@@ -124,18 +154,26 @@ def _set_text_position(self, renderer):
124
154
self ._text .set_position ((x , y ))
125
155
126
156
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
+ """
128
160
bbox = self ._text .get_window_extent (renderer )
129
161
bboxa = bbox .inverse_transformed (self .get_data_transform ())
130
162
return bboxa .bounds
131
163
132
164
def get_required_width (self , renderer ):
133
- """ Get width required for this cell. """
165
+ """Return the minimal required width for the cell."""
134
166
l , b , w , h = self .get_text_bounds (renderer )
135
167
return w * (1.0 + (2.0 * self .PAD ))
136
168
169
+ @docstring .dedent_interpd
137
170
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
+ """
139
177
self ._text .update (kwargs )
140
178
self .stale = True
141
179
@@ -209,6 +247,14 @@ class Table(Artist):
209
247
210
248
Column widths and row heights for the table can be specified.
211
249
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
+
212
258
Return value is a sequence of text, line and patch instances that make
213
259
up the table
214
260
"""
@@ -590,8 +636,6 @@ def table(ax,
590
636
loc='bottom', bbox=None, edges='closed')
591
637
592
638
Factory function to generate a Table instance.
593
-
594
- Thanks to John Gill for providing the class and table.
595
639
"""
596
640
597
641
if cellColours is None and cellText is None :
0 commit comments