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
9
8
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.
13
11
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.
16
15
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.
20
18
19
+ Thanks to John Gill for providing the class and table.
21
20
"""
22
21
import warnings
23
22
31
30
32
31
class Cell (Rectangle ):
33
32
"""
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`.
35
61
"""
36
- PAD = 0.1 # padding between text and rectangle
62
+
63
+ PAD = 0.1
64
+ """Padding between text and rectangle."""
37
65
38
66
def __init__ (self , xy , width , height ,
39
67
edgecolor = 'k' , facecolor = 'w' ,
@@ -44,7 +72,7 @@ def __init__(self, xy, width, height,
44
72
):
45
73
46
74
# Call base
47
- Rectangle .__init__ (self , xy , width = width , height = height ,
75
+ Rectangle .__init__ (self , xy , width = width , height = height , fill = fill ,
48
76
edgecolor = edgecolor , facecolor = facecolor )
49
77
self .set_clip_on (False )
50
78
@@ -70,6 +98,7 @@ def get_text(self):
70
98
return self ._text
71
99
72
100
def set_fontsize (self , size ):
101
+ """Set the text fontsize."""
73
102
self ._text .set_fontsize (size )
74
103
self .stale = True
75
104
@@ -78,7 +107,7 @@ def get_fontsize(self):
78
107
return self ._text .get_fontsize ()
79
108
80
109
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. """
82
111
fontsize = self .get_fontsize ()
83
112
required = self .get_required_width (renderer )
84
113
while fontsize > 1 and required > self .get_width ():
@@ -101,7 +130,7 @@ def draw(self, renderer):
101
130
self .stale = False
102
131
103
132
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.
105
134
106
135
Currently support 'left', 'center' and 'right'
107
136
"""
@@ -126,18 +155,27 @@ def _set_text_position(self, renderer):
126
155
self ._text .set_position ((x , y ))
127
156
128
157
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
+ """
130
162
bbox = self ._text .get_window_extent (renderer )
131
163
bboxa = bbox .inverse_transformed (self .get_data_transform ())
132
164
return bboxa .bounds
133
165
134
166
def get_required_width (self , renderer ):
135
- """ Get width required for this cell. """
167
+ """Return the minimal required width for the cell."""
136
168
l , b , w , h = self .get_text_bounds (renderer )
137
169
return w * (1.0 + (2.0 * self .PAD ))
138
170
171
+ @docstring .dedent_interpd
139
172
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
+ """
141
179
self ._text .update (kwargs )
142
180
self .stale = True
143
181
@@ -211,6 +249,14 @@ class Table(Artist):
211
249
212
250
Column widths and row heights for the table can be specified.
213
251
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
+
214
260
Return value is a sequence of text, line and patch instances that make
215
261
up the table
216
262
"""
@@ -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