matplotlib.table#

Tables drawing.

Note

The table implementation in Matplotlib is lightly maintained. For a morefeatureful table implementation, you may wish to tryblume.

Use the factory functiontable to create a ready-madetable from texts. If you need more control, use theTable class and itsmethods.

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.

classmatplotlib.table.Cell(xy,width,height,*,edgecolor='k',facecolor='w',fill=True,text='',loc='right',fontproperties=None,visible_edges='closed')[source]#

Bases:Rectangle

A cell is aRectangle with some associatedText.

As a user, you'll most likely not creates cells yourself. Instead, youshould use either thetable factory function orTable.add_cell.

Parameters:
xy2-tuple

The position of the bottom left corner of the cell.

widthfloat

The cell width.

heightfloat

The cell height.

edgecolorcolor, default: 'k'

The color of the cell border.

facecolorcolor, default: 'w'

The cell facecolor.

fillbool, default: True

Whether the cell background is filled.

textstr, optional

The cell text.

loc{'right', 'center', 'left'}

The alignment of the text within the cell.

fontpropertiesdict, optional

A dict defining the font properties of the text. Supported keys andvalues are the keyword arguments accepted byFontProperties.

visible_edges{'closed', 'open', 'horizontal', 'vertical'} or substring of 'BRTL'

The cell edges to be drawn with a line: a substring of 'BRTL'(bottom, right, top, left), or one of 'open' (no edges drawn),'closed' (all edges drawn), 'horizontal' (bottom and top),'vertical' (right and left).

PAD=0.1#

Padding between text and rectangle.

auto_set_font_size(renderer)[source]#

Shrink font size until the text fits into the cell width.

draw(renderer)[source]#

Draw the Artist (and its children) using the given renderer.

This has no effect if the artist is not visible (Artist.get_visiblereturns False).

Parameters:
rendererRendererBase subclass.

Notes

This method is overridden in the Artist subclasses.

get_fontsize()[source]#

Return the cell fontsize.

get_path()[source]#

Return aPath for thevisible_edges.

get_required_width(renderer)[source]#

Return the minimal required width for the cell.

get_text()[source]#

Return the cellText instance.

get_text_bounds(renderer)[source]#

Return the text bounds as(x, y, width, height) in table coordinates.

set(*,agg_filter=<UNSET>,alpha=<UNSET>,angle=<UNSET>,animated=<UNSET>,antialiased=<UNSET>,bounds=<UNSET>,capstyle=<UNSET>,clip_box=<UNSET>,clip_on=<UNSET>,clip_path=<UNSET>,color=<UNSET>,edgecolor=<UNSET>,facecolor=<UNSET>,fill=<UNSET>,fontsize=<UNSET>,gid=<UNSET>,hatch=<UNSET>,hatch_linewidth=<UNSET>,height=<UNSET>,in_layout=<UNSET>,joinstyle=<UNSET>,label=<UNSET>,linestyle=<UNSET>,linewidth=<UNSET>,mouseover=<UNSET>,path_effects=<UNSET>,picker=<UNSET>,rasterized=<UNSET>,sketch_params=<UNSET>,snap=<UNSET>,text_props=<UNSET>,transform=<UNSET>,url=<UNSET>,visible=<UNSET>,width=<UNSET>,x=<UNSET>,xy=<UNSET>,y=<UNSET>,zorder=<UNSET>)[source]#

Set multiple properties at once.

Supported properties are

Property

Description

agg_filter

a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array and two offsets from the bottom left corner of the image

alpha

float or None

angle

unknown

animated

bool

antialiased oraa

bool or None

bounds

(left, bottom, width, height)

capstyle

CapStyle or {'butt', 'projecting', 'round'}

clip_box

BboxBase or None

clip_on

bool

clip_path

Patch or (Path, Transform) or None

color

color

edgecolor orec

color or None

facecolor orfc

color or None

figure

unknown

fill

bool

fontsize

unknown

gid

str

hatch

{'/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}

hatch_linewidth

unknown

height

unknown

in_layout

bool

joinstyle

JoinStyle or {'miter', 'round', 'bevel'}

label

object

linestyle orls

{'-', '--', '-.', ':', '', (offset, on-off-seq), ...}

linewidth orlw

float or None

mouseover

bool

path_effects

list ofAbstractPathEffect

picker

None or bool or float or callable

rasterized

bool

sketch_params

(scale: float, length: float, randomness: float)

snap

bool or None

text_props

unknown

transform

unknown

url

str

visible

bool

width

unknown

x

unknown

xy

(float, float)

y

unknown

zorder

float

set_figure(fig)[source]#

Set theFigure orSubFigure instance the artist belongs to.

Parameters:
figFigure orSubFigure
set_fontsize(size)[source]#

Set the text fontsize.

set_text_props(**kwargs)[source]#

Update the text properties.

Valid keyword arguments are:

Property

Description

agg_filter

a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array and two offsets from the bottom left corner of the image

alpha

float or None

animated

bool

antialiased

bool

backgroundcolor

color

bbox

dict with properties forpatches.FancyBboxPatch

clip_box

unknown

clip_on

unknown

clip_path

unknown

color orc

color

figure

Figure orSubFigure

fontfamily orfamily orfontname

{FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}

fontproperties orfont orfont_properties

font_manager.FontProperties orstr orpathlib.Path

fontsize orsize

float or {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}

fontstretch orstretch

{a numeric value in range 0-1000, 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}

fontstyle orstyle

{'normal', 'italic', 'oblique'}

fontvariant orvariant

{'normal', 'small-caps'}

fontweight orweight

{a numeric value in range 0-1000, 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'}

gid

str

horizontalalignment orha

{'left', 'center', 'right'}

in_layout

bool

label

object

linespacing

float (multiple of font size)

math_fontfamily

str

mouseover

bool

multialignment orma

{'left', 'right', 'center'}

parse_math

bool

path_effects

list ofAbstractPathEffect

picker

None or bool or float or callable

position

(float, float)

rasterized

bool

rotation

float or {'vertical', 'horizontal'}

rotation_mode

{None, 'default', 'anchor'}

sketch_params

(scale: float, length: float, randomness: float)

snap

bool or None

text

object

transform

Transform

transform_rotates_text

bool

url

str

usetex

bool, default:rcParams["text.usetex"] (default:False)

verticalalignment orva

{'baseline', 'bottom', 'center', 'center_baseline', 'top'}

visible

bool

wrap

bool

x

float

y

float

zorder

float

set_transform(t)[source]#

Set the artist transform.

Parameters:
tTransform
propertyvisible_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 oneof {'open', 'closed', 'horizontal', 'vertical'}.

matplotlib.table.CustomCell[source]#

alias ofCell

classmatplotlib.table.Table(ax,loc=None,bbox=None,**kwargs)[source]#

Bases:Artist

A 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 positionedat 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 createtables that have holes.

Note: You'll usually not create an empty table from scratch. Instead usetable to create a table from data.

Parameters:
axAxes

TheAxes to plot the table into.

locstr, optional

The position of the cell with respect toax. This must be one ofthecodes.

bboxBbox or [xmin, ymin, width, height], optional

A bounding box to draw the table into. If this is notNone, thisoverridesloc.

Other Parameters:
**kwargs

Artist properties.

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:
rowint

Row index.

colint

Column index.

*args, **kwargs

All other parameters are passed on toCell.

Returns:
Cell

The created cell.

auto_set_column_width(col)[source]#

Automatically set the widths of given columns to optimal sizes.

Parameters:
colint or sequence of ints

The indices of the columns to auto-scale.

auto_set_font_size(value=True)[source]#

Automatically set font size.

codes={'best':0,'bottom':17,'bottomleft':12,'bottomright':13,'center':9,'centerleft':5,'centerright':6,'left':15,'lowercenter':7,'lowerleft':3,'lowerright':4,'right':14,'top':16,'topleft':11,'topright':10,'uppercenter':8,'upperleft':2,'upperright':1}#

Possible values where to place the table relative to the Axes.

contains(mouseevent)[source]#

Test whether the artist contains the mouse event.

Parameters:
mouseeventMouseEvent
Returns:
containsbool

Whether any values are within the radius.

detailsdict

An artist-specific dictionary of details of the event context,such as which points are contained in the pick radius. See theindividual Artist subclasses for details.

draw(renderer)[source]#

Draw the Artist (and its children) using the given renderer.

This has no effect if the artist is not visible (Artist.get_visiblereturns False).

Parameters:
rendererRendererBase subclass.

Notes

This method is overridden in the Artist subclasses.

propertyedges#

The default value ofvisible_edges for newly addedcells usingadd_cell.

Notes

This setting does currently only affect newly created cells usingadd_cell.

To change existing cells, you have to set their edges explicitly:

forcintab.get_celld().values():c.visible_edges='horizontal'
get_celld()[source]#

Return a dict of cells in the table mapping(row, column) toCells.

Notes

You can also directly index into the Table object to access individualcells:

cell=table[row,col]
get_children()[source]#

Return the Artists contained by the table.

get_window_extent(renderer=None)[source]#

Get the artist's bounding box in display space.

The bounding box' width and height are nonnegative.

Subclasses should override for inclusion in the bounding box"tight" calculation. Default is to return an empty boundingbox at 0, 0.

Be careful when using this function, the results will not updateif the artist window extent of the artist changes. The extentcan change due to any changes in the transform stack, such aschanging the Axes limits, the figure size, or the canvas used(as is done when saving a figure). This can lead to unexpectedbehavior where interactive figures will look fine on the screen,but will save incorrectly.

scale(xscale,yscale)[source]#

Scale column widths byxscale and row heights byyscale.

set(*,agg_filter=<UNSET>,alpha=<UNSET>,animated=<UNSET>,clip_box=<UNSET>,clip_on=<UNSET>,clip_path=<UNSET>,fontsize=<UNSET>,gid=<UNSET>,in_layout=<UNSET>,label=<UNSET>,mouseover=<UNSET>,path_effects=<UNSET>,picker=<UNSET>,rasterized=<UNSET>,sketch_params=<UNSET>,snap=<UNSET>,transform=<UNSET>,url=<UNSET>,visible=<UNSET>,zorder=<UNSET>)[source]#

Set multiple properties at once.

Supported properties are

Property

Description

agg_filter

a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array and two offsets from the bottom left corner of the image

alpha

float or None

animated

bool

clip_box

BboxBase or None

clip_on

bool

clip_path

Patch or (Path, Transform) or None

figure

Figure orSubFigure

fontsize

float

gid

str

in_layout

bool

label

object

mouseover

bool

path_effects

list ofAbstractPathEffect

picker

None or bool or float or callable

rasterized

bool

sketch_params

(scale: float, length: float, randomness: float)

snap

bool or None

transform

Transform

url

str

visible

bool

zorder

float

set_fontsize(size)[source]#

Set the font size, in points, of the cell text.

Parameters:
sizefloat

Notes

As long as auto font size has not been disabled, the value will beclipped such that the text fits horizontally into the cell.

You can disable this behavior usingauto_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 thetext 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 anAxes.

At least one ofcellText orcellColours must be specified. Theseparameters must be 2D lists, in which the outer lists define the rows andthe inner list define the column values per row. Each row must have thesame number of elements.

The table can optionally have row and column headers, which are configuredusingrowLabels,rowColours,rowLoc andcolLabels,colColours,colLoc respectively.

For finer grained control over tables, use theTable class and add it tothe Axes withAxes.add_table.

Parameters:
cellText2D list of str or pandas.DataFrame, optional

The texts to place into the table cells.

Note: Line breaks in the strings are currently not accounted for andwill result in the text exceeding the cell boundaries.

cellColours2D list ofcolor, optional

The background colors of the cells.

cellLoc{'right', 'center', 'left'}

The alignment of the text within the cells.

colWidthslist of float, optional

The column widths in units of the axes. If not given, all columns willhave a width of1 / ncols.

rowLabelslist of str, optional

The text of the row header cells.

rowColourslist ofcolor, optional

The colors of the row header cells.

rowLoc{'left', 'center', 'right'}

The text alignment of the row header cells.

colLabelslist of str, optional

The text of the column header cells.

colColourslist ofcolor, optional

The colors of the column header cells.

colLoc{'center', 'left', 'right'}

The text alignment of the column header cells.

locstr, default: 'bottom'

The position of the cell with respect toax. This must be one ofthecodes.

bboxBbox or [xmin, ymin, width, height], optional

A bounding box to draw the table into. If this is notNone, thisoverridesloc.

edges{'closed', 'open', 'horizontal', 'vertical'} or substring of 'BRTL'

The cell edges to be drawn with a line. See alsovisible_edges.

Returns:
Table

The created table.

Other Parameters:
**kwargs

Table properties.

Property

Description

agg_filter

a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array and two offsets from the bottom left corner of the image

alpha

float or None

animated

bool

clip_box

BboxBase or None

clip_on

bool

clip_path

Patch or (Path, Transform) or None

figure

Figure orSubFigure

fontsize

float

gid

str

in_layout

bool

label

object

mouseover

bool

path_effects

list ofAbstractPathEffect

picker

None or bool or float or callable

rasterized

bool

sketch_params

(scale: float, length: float, randomness: float)

snap

bool or None

transform

Transform

url

str

visible

bool

zorder

float