From 0e995cee47595295cddd0889856661db416ac0db Mon Sep 17 00:00:00 2001 From: saikarna913 Date: Mon, 16 Dec 2024 19:53:44 +0530 Subject: [PATCH] Backport PR #29258: Adding font Size as default parameter --- lib/matplotlib/table.py | 4 ++++ lib/matplotlib/tests/test_table.py | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index 21518c4c6726..370ce9fe922f 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -838,5 +838,9 @@ def table(ax, if rowLabelWidth == 0: table.auto_set_column_width(-1) + # set_fontsize is only effective after cells are added + if "fontsize" in kwargs: + table.set_fontsize(kwargs["fontsize"]) + ax.add_table(table) return table diff --git a/lib/matplotlib/tests/test_table.py b/lib/matplotlib/tests/test_table.py index ee974f3cd8f9..783be25376be 100644 --- a/lib/matplotlib/tests/test_table.py +++ b/lib/matplotlib/tests/test_table.py @@ -270,3 +270,15 @@ def test_table_dataframe(pd): for r, (index, row) in enumerate(df.iterrows()): for c, col in enumerate(df.columns if r == 0 else row.values): assert table[r if r == 0 else r+1, c].get_text().get_text() == str(col) + + +def test_table_fontsize(): + # Test that the passed fontsize propagates to cells + tableData = [['a', 1], ['b', 2]] + fig, ax = plt.subplots() + test_fontsize = 20 + t = ax.table(cellText=tableData, loc='top', fontsize=test_fontsize) + cell_fontsize = t[(0, 0)].get_fontsize() + assert cell_fontsize == test_fontsize, f"Actual:{test_fontsize},got:{cell_fontsize}" + cell_fontsize = t[(1, 1)].get_fontsize() + assert cell_fontsize == test_fontsize, f"Actual:{test_fontsize},got:{cell_fontsize}"