1
1
"""
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
3
4
4
- The table consists of a grid of cells .
5
+ This module provides functionality to add a table to a plot .
5
6
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.
7
10
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.
20
13
14
+ Thanks to John Gill for providing the class and table.
21
15
"""
22
16
import warnings
23
17
31
25
32
26
class Cell (Rectangle ):
33
27
"""
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`.
35
56
"""
36
- PAD = 0.1 # padding between text and rectangle
57
+
58
+ PAD = 0.1
59
+ """Padding between text and rectangle."""
37
60
38
61
def __init__ (self , xy , width , height ,
39
62
edgecolor = 'k' , facecolor = 'w' ,
@@ -44,7 +67,7 @@ def __init__(self, xy, width, height,
44
67
):
45
68
46
69
# Call base
47
- Rectangle .__init__ (self , xy , width = width , height = height ,
70
+ Rectangle .__init__ (self , xy , width = width , height = height , fill = fill ,
48
71
edgecolor = edgecolor , facecolor = facecolor )
49
72
self .set_clip_on (False )
50
73
@@ -70,6 +93,7 @@ def get_text(self):
70
93
return self ._text
71
94
72
95
def set_fontsize (self , size ):
96
+ """Set the text fontsize."""
73
97
self ._text .set_fontsize (size )
74
98
self .stale = True
75
99
@@ -78,7 +102,7 @@ def get_fontsize(self):
78
102
return self ._text .get_fontsize ()
79
103
80
104
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. """
82
106
fontsize = self .get_fontsize ()
83
107
required = self .get_required_width (renderer )
84
108
while fontsize > 1 and required > self .get_width ():
@@ -101,7 +125,7 @@ def draw(self, renderer):
101
125
self .stale = False
102
126
103
127
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.
105
129
106
130
Currently support 'left', 'center' and 'right'
107
131
"""
@@ -126,18 +150,27 @@ def _set_text_position(self, renderer):
126
150
self ._text .set_position ((x , y ))
127
151
128
152
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
+ """
130
157
bbox = self ._text .get_window_extent (renderer )
131
158
bboxa = bbox .inverse_transformed (self .get_data_transform ())
132
159
return bboxa .bounds
133
160
134
161
def get_required_width (self , renderer ):
135
- """ Get width required for this cell. """
162
+ """Return the minimal required width for the cell."""
136
163
l , b , w , h = self .get_text_bounds (renderer )
137
164
return w * (1.0 + (2.0 * self .PAD ))
138
165
166
+ @docstring .dedent_interpd
139
167
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
+ """
141
174
self ._text .update (kwargs )
142
175
self .stale = True
143
176
@@ -211,6 +244,14 @@ class Table(Artist):
211
244
212
245
Column widths and row heights for the table can be specified.
213
246
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
+
214
255
Return value is a sequence of text, line and patch instances that make
215
256
up the table
216
257
"""
@@ -590,8 +631,6 @@ def table(ax,
590
631
loc='bottom', bbox=None, edges='closed')
591
632
592
633
Factory function to generate a Table instance.
593
-
594
- Thanks to John Gill for providing the class and table.
595
634
"""
596
635
597
636
if cellColours is None and cellText is None :
0 commit comments