Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 93c0753

Browse files
committed
Support both Bbox and list for bbox to table/Table
1 parent 8ed4c02 commit 93c0753

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-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] or None
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: 33 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,34 @@ 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 = [[66386, 174296],
203+
[58230, 381139]]
204+
205+
colLabels = ('Freeze', 'Wind')
206+
rowLabels = ['%d year' % x for x in (100, 50)]
207+
208+
cellText = []
209+
yoff = np.zeros(len(colLabels))
210+
for row in reversed(data):
211+
yoff += row
212+
cellText.append(['%1.1f' % (x/1000.0) for x in yoff])
213+
214+
ax_list = fig_test.subplots()
215+
ax_list.table(cellText=cellText,
216+
rowLabels=rowLabels,
217+
colLabels=colLabels,
218+
loc='center',
219+
bbox=[0.1, 0.2, 0.8, 0.6]
220+
)
221+
222+
ax_bbox = fig_ref.subplots()
223+
ax_bbox.table(cellText=cellText,
224+
rowLabels=rowLabels,
225+
colLabels=colLabels,
226+
loc='center',
227+
bbox=Bbox.from_extents(0.1, 0.2, 0.9, 0.8)
228+
)

0 commit comments

Comments
 (0)