diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index ed9ab6d10bfb..2fb6df83ffe1 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -285,7 +285,7 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs): loc : str The position of the cell with respect to *ax*. This must be one of the `~.Table.codes`. - bbox : `.Bbox` or None + bbox : `.Bbox` or [xmin, ymin, width, height], optional A bounding box to draw the table into. If this is not *None*, this overrides *loc*. @@ -595,7 +595,10 @@ def _update_positions(self, renderer): if self._bbox is not None: # Position according to bbox - rl, rb, rw, rh = self._bbox + if isinstance(self._bbox, Bbox): + rl, rb, rw, rh = self._bbox.bounds + else: + rl, rb, rw, rh = self._bbox self.scale(rw / w, rh / h) ox = rl - l oy = rb - b @@ -710,7 +713,7 @@ def table(ax, The position of the cell with respect to *ax*. This must be one of the `~.Table.codes`. - bbox : `.Bbox`, optional + bbox : `.Bbox` or [xmin, ymin, width, height], optional A bounding box to draw the table into. If this is not *None*, this overrides *loc*. diff --git a/lib/matplotlib/tests/test_table.py b/lib/matplotlib/tests/test_table.py index d4f68ea1805d..9b2cb96ea037 100644 --- a/lib/matplotlib/tests/test_table.py +++ b/lib/matplotlib/tests/test_table.py @@ -1,9 +1,10 @@ import matplotlib.pyplot as plt import numpy as np -from matplotlib.testing.decorators import image_comparison +from matplotlib.testing.decorators import image_comparison, check_figures_equal from matplotlib.table import CustomCell, Table from matplotlib.path import Path +from matplotlib.transforms import Bbox def test_non_square(): @@ -194,3 +195,30 @@ def test_table_cells(): # properties and setp table.properties() plt.setp(table) + + +@check_figures_equal(extensions=["png"]) +def test_table_bbox(fig_test, fig_ref): + data = [[2, 3], + [4, 5]] + + col_labels = ('Foo', 'Bar') + row_labels = ('Ada', 'Bob') + + cell_text = [[f"{x}" for x in row] for row in data] + + ax_list = fig_test.subplots() + ax_list.table(cellText=cell_text, + rowLabels=row_labels, + colLabels=col_labels, + loc='center', + bbox=[0.1, 0.2, 0.8, 0.6] + ) + + ax_bbox = fig_ref.subplots() + ax_bbox.table(cellText=cell_text, + rowLabels=row_labels, + colLabels=col_labels, + loc='center', + bbox=Bbox.from_extents(0.1, 0.2, 0.9, 0.8) + )