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

Skip to content

Commit 7c9bd99

Browse files
Federico Arizafariza
authored andcommitted
acces table with getitem and setitem
1 parent d979874 commit 7c9bd99

5 files changed

Lines changed: 71 additions & 7 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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,17 +276,51 @@ 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
281288
289+
Returns
290+
-------
291+
`CustomCell`: Automatically created cell
292+
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 ValueError('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
289-
return cell
314+
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]
290324

291325
@property
292326
def edges(self):

lib/matplotlib/tests/test_table.py

Lines changed: 12 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,14 @@ 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 = plt.figure()
188+
ax = fig.add_subplot(1, 1, 1)
189+
table = Table(ax)
190+
191+
cell = table.add_cell(1, 2, 1, 1)
192+
assert isinstance(cell, CustomCell)
193+
table[2, 1] = cell
194+
assert table[2, 1] is cell

0 commit comments

Comments
 (0)