Skip to content

Commit 75e2d22

Browse files
authored
Merge pull request #25052 from oscargus/tablebbox
Support both Bbox and list for bbox to table/Table
2 parents c9d7bff + 51aba38 commit 75e2d22

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

lib/matplotlib/table.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs):
285285
loc : str
286286
The position of the cell with respect to *ax*. This must be one of
287287
the `~.Table.codes`.
288-
bbox : `.Bbox` or None
288+
bbox : `.Bbox` or [xmin, ymin, width, height], optional
289289
A bounding box to draw the table into. If this is not *None*, this
290290
overrides *loc*.
291291
@@ -595,7 +595,10 @@ def _update_positions(self, renderer):
595595

596596
if self._bbox is not None:
597597
# Position according to bbox
598-
rl, rb, rw, rh = self._bbox
598+
if isinstance(self._bbox, Bbox):
599+
rl, rb, rw, rh = self._bbox.bounds
600+
else:
601+
rl, rb, rw, rh = self._bbox
599602
self.scale(rw / w, rh / h)
600603
ox = rl - l
601604
oy = rb - b
@@ -710,7 +713,7 @@ def table(ax,
710713
The position of the cell with respect to *ax*. This must be one of
711714
the `~.Table.codes`.
712715
713-
bbox : `.Bbox`, optional
716+
bbox : `.Bbox` or [xmin, ymin, width, height], optional
714717
A bounding box to draw the table into. If this is not *None*, this
715718
overrides *loc*.
716719

lib/matplotlib/tests/test_table.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import matplotlib.pyplot as plt
22
import numpy as np
3-
from matplotlib.testing.decorators import image_comparison
3+
from matplotlib.testing.decorators import image_comparison, check_figures_equal
44

55
from matplotlib.table import CustomCell, Table
66
from matplotlib.path import Path
7+
from matplotlib.transforms import Bbox
78

89

910
def test_non_square():
@@ -194,3 +195,30 @@ def test_table_cells():
194195
# properties and setp
195196
table.properties()
196197
plt.setp(table)
198+
199+
200+
@check_figures_equal(extensions=["png"])
201+
def test_table_bbox(fig_test, fig_ref):
202+
data = [[2, 3],
203+
[4, 5]]
204+
205+
col_labels = ('Foo', 'Bar')
206+
row_labels = ('Ada', 'Bob')
207+
208+
cell_text = [[f"{x}" for x in row] for row in data]
209+
210+
ax_list = fig_test.subplots()
211+
ax_list.table(cellText=cell_text,
212+
rowLabels=row_labels,
213+
colLabels=col_labels,
214+
loc='center',
215+
bbox=[0.1, 0.2, 0.8, 0.6]
216+
)
217+
218+
ax_bbox = fig_ref.subplots()
219+
ax_bbox.table(cellText=cell_text,
220+
rowLabels=row_labels,
221+
colLabels=col_labels,
222+
loc='center',
223+
bbox=Bbox.from_extents(0.1, 0.2, 0.9, 0.8)
224+
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy