matplotlib.table¶
This module provides functionality to add a table to a plot.
Use the factory function table to create a ready-made
table from texts. If you need more control, use the Table class and its
methods.
The table consists of a grid of cells, which are indexed by (row, column). The cell (0, 0) is positioned at the top left.
Thanks to John Gill for providing the class and table.
-
class
matplotlib.table.Cell(xy, width, height, edgecolor='k', facecolor='w', fill=True, text='', loc=None, fontproperties=None)[source]¶ Bases:
matplotlib.patches.RectangleA cell is a
Rectanglewith some associatedText.Parameters: xy : 2-tuple
The position of the bottom left corner of the cell.
width : float
The cell width.
height : float
The cell height.
edgecolor : color
The color of the cell border.
facecolor : color
The cell facecolor.
fill : bool
Whether the cell background is filled.
text : str
The cell text.
loc : {'left', 'center', 'right'}, default: 'right'
The alignment of the text within the cell.
fontproperties : dict
A dict defining the font properties of the text. Supported keys and values are the keyword arguments accepted by
FontProperties.Parameters: xy : (float, float)
The bottom and left rectangle coordinates
width : float
Rectangle width
height : float
Rectangle height
angle : float, optional
rotation in degrees anti-clockwise about xy (default is 0.0)
fill : bool, optional
Whether to fill the rectangle (default is
True)Notes
Valid keyword arguments are:
Property Description agg_filtera filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array alphafloat or None animatedbool antialiasedor aaunknown capstyle{'butt', 'round', 'projecting'} clip_boxBboxclip_onbool clip_pathPatch or (Path, Transform) or None colorcolor containscallable edgecoloror eccolor or None or 'auto' facecoloror fccolor or None figureFigurefillbool gidstr hatch{'/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*'} in_layoutbool joinstyle{'miter', 'round', 'bevel'} labelobject linestyleor ls{'-', '--', '-.', ':', '', (offset, on-off-seq), ...} linewidthor lwfloat or None path_effectsAbstractPathEffectpickerNone or bool or float or callable rasterizedbool or None sketch_params(scale: float, length: float, randomness: float) snapbool or None transformTransformurlstr visiblebool zorderfloat -
PAD= 0.1¶ Padding between text and rectangle.
-
get_text_bounds(renderer)[source]¶ Return the text bounds as (x, y, width, height) in table coordinates.
-
set_text_props(**kwargs)[source]¶ Update the text properties.
Valid keyword arguments are:
Property Description agg_filtera filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array alphafloat or None animatedbool backgroundcolorcolor bboxdict with properties for patches.FancyBboxPatchclip_boxBboxclip_onbool clip_pathPatch or (Path, Transform) or None coloror ccolor containscallable figureFigurefontfamilyor family{FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'} fontpropertiesor font_propertiesfont_manager.FontPropertiesfontsizeor size{size in points, 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'} fontstretchor stretch{a numeric value in range 0-1000, 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'} fontstyleor style{'normal', 'italic', 'oblique'} fontvariantor variant{'normal', 'small-caps'} fontweightor weight{a numeric value in range 0-1000, 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'} gidstr horizontalalignmentor ha{'center', 'right', 'left'} in_layoutbool labelobject linespacingfloat (multiple of font size) multialignmentor ma{'left', 'right', 'center'} path_effectsAbstractPathEffectpickerNone or bool or float or callable position(float, float) rasterizedbool or None rotation{angle in degrees, 'vertical', 'horizontal'} rotation_mode{None, 'default', 'anchor'} sketch_params(scale: float, length: float, randomness: float) snapbool or None textobject transformTransformurlstr usetexbool or None verticalalignmentor va{'center', 'top', 'bottom', 'baseline', 'center_baseline'} visiblebool wrapbool xfloat yfloat zorderfloat
-
-
class
matplotlib.table.CustomCell(*args, visible_edges, **kwargs)[source]¶ Bases:
matplotlib.table.CellA
Cellsubclass with configurable edge visibility.Parameters: xy : (float, float)
The bottom and left rectangle coordinates
width : float
Rectangle width
height : float
Rectangle height
angle : float, optional
rotation in degrees anti-clockwise about xy (default is 0.0)
fill : bool, optional
Whether to fill the rectangle (default is
True)Notes
Valid keyword arguments are:
Property Description agg_filtera filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array alphafloat or None animatedbool antialiasedor aaunknown capstyle{'butt', 'round', 'projecting'} clip_boxBboxclip_onbool clip_pathPatch or (Path, Transform) or None colorcolor containscallable edgecoloror eccolor or None or 'auto' facecoloror fccolor or None figureFigurefillbool gidstr hatch{'/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*'} in_layoutbool joinstyle{'miter', 'round', 'bevel'} labelobject linestyleor ls{'-', '--', '-.', ':', '', (offset, on-off-seq), ...} linewidthor lwfloat or None path_effectsAbstractPathEffectpickerNone or bool or float or callable rasterizedbool or None sketch_params(scale: float, length: float, randomness: float) snapbool or None transformTransformurlstr visiblebool zorderfloat -
get_path()[source]¶ Return a
Pathfor thevisible_edges.
-
property
visible_edges¶ The cell edges to be drawn with a line.
Reading this property returns a substring of 'BRTL' (bottom, right, top, left').
When setting this property, you can use a substring of 'BRTL' or one of {'open', 'closed', 'horizontal', 'vertical'}.
-
-
class
matplotlib.table.Table(ax, loc=None, bbox=None, **kwargs)[source]¶ Bases:
matplotlib.artist.ArtistA table of cells.
The table consists of a grid of cells, which are indexed by (row, column).
For a simple table, you'll have a full grid of cells with indices from (0, 0) to (num_rows-1, num_cols-1), in which the cell (0, 0) is positioned at the top left. However, you can also add cells with negative indices. You don't have to add a cell to every grid position, so you can create tables that have holes.
Note: You'll usually not create an empty table from scratch. Instead use
tableto create a table from data.Parameters: ax :
matplotlib.axes.AxesThe
Axesto plot the table into.loc : str
The position of the cell with respect to ax. This must be one of the
codes.bbox :
Bboxor NoneA bounding box to draw the table into. If this is not None, this overrides loc.
Other Parameters: **kwargs
Artistproperties.-
AXESPAD= 0.02¶ The border between the Axes and the table edge in Axes units.
-
FONTSIZE= 10¶
-
add_cell(row, col, *args, **kwargs)[source]¶ Create a cell and add it to the table.
Parameters: row : int
Row index.
col : int
Column index.
*args, **kwargs
All other parameters are passed on to
Cell.Returns: cell :
CustomCellThe created cell.
-
auto_set_column_width(col)[source]¶ Automatically set the widths of given columns to optimal sizes.
Parameters: col : int or sequence of ints
The indices of the columns to auto-scale.
-
codes= {'best': 0, 'bottom': 17, 'bottom left': 12, 'bottom right': 13, 'center': 9, 'center left': 5, 'center right': 6, 'left': 15, 'lower center': 7, 'lower left': 3, 'lower right': 4, 'right': 14, 'top': 16, 'top left': 11, 'top right': 10, 'upper center': 8, 'upper left': 2, 'upper right': 1}¶ Possible values where to place the table relative to the Axes.
-
contains(mouseevent)[source]¶ Test whether the artist contains the mouse event.
Parameters: mouseevent :
matplotlib.backend_bases.MouseEventReturns: contains : bool
Whether any values are within the radius.
details : dict
An artist-specific dictionary of details of the event context, such as which points are contained in the pick radius. See the individual Artist subclasses for details.
See also
set_contains,get_contains
-
draw(renderer)[source]¶ Draw the Artist using the given renderer.
This method will be overridden in the Artist subclasses. Typically, it is implemented to not have any effect if the Artist is not visible (
Artist.get_visibleis False).Parameters: renderer : RendererBasesubclass.
-
property
edges¶ The default value of
visible_edgesfor newly added cells usingadd_cell.Notes
This setting does currently only affect newly created cells using
add_cell.To change existing cells, you have to set their edges explicitly:
for c in tab.get_celld().values(): c.visible_edges = 'horizontal'
-
get_celld()[source]¶ Return a dict of cells in the table mapping (row, column) to
Cells.Notes
You can also directly index into the Table object to access individual cells:
cell = table[row, col]
-
set_fontsize(size)[source]¶ Set the font size, in points, of the cell text.
Parameters: size : float Notes
As long as auto font size has not been disabled, the value will be clipped such that the text fits horizontally into the cell.
You can disable this behavior using
auto_set_font_size.>>> the_table.auto_set_font_size(False) >>> the_table.set_fontsize(20)
However, there is no automatic scaling of the row height so that the text may exceed the cell boundary.
-
-
matplotlib.table.table(ax, cellText=None, cellColours=None, cellLoc='right', colWidths=None, rowLabels=None, rowColours=None, rowLoc='left', colLabels=None, colColours=None, colLoc='center', loc='bottom', bbox=None, edges='closed', **kwargs)[source]¶ Add a table to an
Axes.At least one of cellText or cellColours must be specified. These parameters must be 2D lists, in which the outer lists define the rows and the inner list define the column values per row. Each row must have the same number of elements.
The table can optionally have row and column headers, which are configured using rowLabels, rowColours, rowLoc and colLabels, colColours, colLoc respectively.
For finer grained control over tables, use the
Tableclass and add it to the axes withAxes.add_table.Parameters: cellText : 2D list of str, optional
The texts to place into the table cells.
Note: Line breaks in the strings are currently not accounted for and will result in the text exceeding the cell boundaries.
cellColours : 2D list of colors, optional
The background colors of the cells.
cellLoc : {'left', 'center', 'right'}, default: 'right'
The alignment of the text within the cells.
colWidths : list of float, optional
The column widths in units of the axes. If not given, all columns will have a width of 1 / ncols.
rowLabels : list of str, optional
The text of the row header cells.
rowColours : list of colors, optional
The colors of the row header cells.
rowLoc : {'left', 'center', 'right'}, optional, default: 'left'
The text alignment of the row header cells.
colLabels : list of str, optional
The text of the column header cells.
colColours : list of colors, optional
The colors of the column header cells.
colLoc : {'left', 'center', 'right'}, optional, default: 'left'
The text alignment of the column header cells.
loc : str, optional
The position of the cell with respect to ax. This must be one of the
codes.bbox :
Bbox, optionalA bounding box to draw the table into. If this is not None, this overrides loc.
edges : substring of 'BRTL' or {'open', 'closed', 'horizontal', 'vertical'}
The cell edges to be drawn with a line. See also
visible_edges.Returns: table :
TableThe created table.
Other Parameters: **kwargs
Tableproperties.Property Description agg_filtera filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array alphafloat or None animatedbool clip_boxBboxclip_onbool clip_pathPatch or (Path, Transform) or None containscallable figureFigurefontsizefloat gidstr in_layoutbool labelobject path_effectsAbstractPathEffectpickerNone or bool or float or callable rasterizedbool or None sketch_params(scale: float, length: float, randomness: float) snapbool or None transformTransformurlstr visiblebool zorderfloat