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

Skip to content

Commit 5a75f5f

Browse files
authored
Merge pull request #9081 from fariza/table-array-like
cell returned when added to Table
2 parents bf2127b + 848ed7a commit 5a75f5f

5 files changed

Lines changed: 73 additions & 6 deletions

File tree

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
scale_api.rst
5050
spines_api.rst
5151
style_api.rst
52+
table_api.rst
5253
text_api.rst
5354
ticker_api.rst
5455
tight_layout_api.rst

doc/api/table_api.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*****
2+
table
3+
*****
4+
5+
:mod:`matplotlib.table`
6+
===================================
7+
8+
.. automodule:: matplotlib.table
9+
:members:
10+
:undoc-members:
11+
:show-inheritance:

lib/matplotlib/axes/_base.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,10 +2021,17 @@ def _update_patch_limits(self, patch):
20212021

20222022
def add_table(self, tab):
20232023
"""
2024-
Add a :class:`~matplotlib.tables.Table` instance to the
2024+
Add a :class:`~matplotlib.table.Table` instance to the
20252025
list of axes tables
20262026
2027-
Returns the table.
2027+
Parameters
2028+
----------
2029+
tab: `matplotlib.table.Table`
2030+
Table instance
2031+
2032+
Returns
2033+
-------
2034+
`matplotlib.table.Table`: the table.
20282035
"""
20292036
self._set_artist_props(tab)
20302037
self.tables.append(tab)

lib/matplotlib/table.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,17 +276,52 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs):
276276
self.set_clip_on(False)
277277

278278
def add_cell(self, row, col, *args, **kwargs):
279-
""" Add a cell to the table. """
280-
xy = (0, 0)
279+
"""
280+
Add a cell to the table.
281+
282+
Parameters
283+
----------
284+
row : int
285+
Row index
286+
col : int
287+
Column index
288+
289+
Returns
290+
-------
291+
`CustomCell`: Automatically created cell
281292
293+
"""
294+
xy = (0, 0)
282295
cell = CustomCell(xy, visible_edges=self.edges, *args, **kwargs)
296+
self[row, col] = cell
297+
return cell
298+
299+
def __setitem__(self, position, cell):
300+
"""
301+
Set a customcell in a given position
302+
"""
303+
if not isinstance(cell, CustomCell):
304+
raise TypeError('Table only accepts CustomCell')
305+
try:
306+
row, col = position[0], position[1]
307+
except Exception:
308+
raise KeyError('Only tuples length 2 are accepted as coordinates')
283309
cell.set_figure(self.figure)
284310
cell.set_transform(self.get_transform())
285-
286311
cell.set_clip_on(False)
287312
self._cells[row, col] = cell
288313
self.stale = True
289314

315+
def __getitem__(self, position):
316+
"""
317+
Retreive a custom cell from a given position
318+
"""
319+
try:
320+
row, col = position[0], position[1]
321+
except Exception:
322+
raise KeyError('Only tuples length 2 are accepted as coordinates')
323+
return self._cells[row, col]
324+
290325
@property
291326
def edges(self):
292327
return self._edges

lib/matplotlib/tests/test_table.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy as np
77
from matplotlib.testing.decorators import image_comparison
88

9-
from matplotlib.table import CustomCell
9+
from matplotlib.table import CustomCell, Table
1010
from matplotlib.path import Path
1111

1212

@@ -181,3 +181,16 @@ def test_auto_column():
181181
tb4.auto_set_font_size(False)
182182
tb4.set_fontsize(12)
183183
tb4.auto_set_column_width("-101")
184+
185+
186+
def test_table_cells():
187+
fig, ax = plt.subplots()
188+
table = Table(ax)
189+
190+
cell = table.add_cell(1, 2, 1, 1)
191+
assert isinstance(cell, CustomCell)
192+
assert cell is table[1, 2]
193+
194+
cell2 = CustomCell((0, 0), 1, 2, visible_edges=None)
195+
table[2, 1] = cell2
196+
assert table[2, 1] is cell2

0 commit comments

Comments
 (0)