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

Skip to content

Support both Bbox and list for bbox to table/Table #25052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/matplotlib/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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*.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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*.

Expand Down
30 changes: 29 additions & 1 deletion lib/matplotlib/tests/test_table.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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)
)