Repository: matplotlib/matplotlib

Issue:
[Bug]: `fontsize` in tables not working
### Bug summary

Specifying `fontsize` kwarg in `matplotlib.pyplot.table` doesn't have any effect.

### Code for reproduction

```Python
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = x + 1
tableData = [['a', 1], ['b', 1]]

fig, ax = plt.subplots()
ax.plot(x, y)
t = ax.table(
    cellText=tableData,
    loc='top',
    cellLoc='center',
    fontsize=30
)
plt.show()
```


### Actual outcome

![Figure_1](https://github.com/user-attachments/assets/4a60237b-4060-4438-854e-ba2b7a5b73e1)


### Expected outcome

A table with bigger font size.

### Additional information

This works:
```
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = x + 1
tableData = [['a', 1], ['b', 1]]

fig, ax = plt.subplots()
ax.plot(x, y)
t = ax.table(
    cellText=tableData,
    loc='top',
    cellLoc='center'
)
t.set_fontsize(30)  # <----------------
plt.show()
```
![Figure_1](https://github.com/user-attachments/assets/b668e57f-e676-40bc-98cb-c3e35ede6ad8)

### Operating system

Windows 11

### Matplotlib Version

3.8.2

### Matplotlib Backend

QtAgg

### Python version

3.12.0

### Jupyter version

_No response_

### Installation

pip


Discussion:
### Good first issue - notes for new contributors

This issue is suited to new contributors because it does not require understanding of the
Matplotlib internals. To get started, please see our [contributing
guide](https://matplotlib.org/stable/devel/index).

**We do not assign issues**. Check the *Development* section in the sidebar for linked pull
requests (PRs). If there are none, feel free to start working on it. If there is an open PR, please
collaborate on the work by reviewing it rather than duplicating it in a competing PR.

If something is unclear, please reach out on any of our [communication
channels](https://matplotlib.org/stable/devel/contributing.html#get-connected).
It appears that `fontsize` is documented as a property because `Table.set_fontsize` exists. However, `Table.set_fontsize` does _not_ set some direct attribute, but sets the font size on all `Cell`s of the table, so this value would never be known later. From `matplotlib.table.table`, the `Table` is created first (passing through the `fontsize` keyword argument), and the `Cell`s are added afterwards, meaning the `fontsize` is never given to the `Cell`s.

To get this working, the `fontsize` argument will need to be passed through [the `add_cell` calls somewhere here](https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/table.py#L815-L839). This seems like a fairly straightforward Good First Issue.
Hi @QuLogic can I make a PR?
@Kaustbh, can I create the pull request? Since I am a beginner, this can motivate me to Contribute.
![image](https://github.com/user-attachments/assets/11725323-9c4e-44ab-8035-3becacb431b3)


No problem you can work on it 👍, let me know if you need any help.
@Kaustbh, I’m facing issues installing Matplotlib in the development environment, both on my local desktop and in GitHub Codespaces. It’s throwing an import error, so I’m unable to test the code changes I’ve made.  

Here’s what I’ve done so far:  
1. Forked the Matplotlib repository.  
2. Created a new environment and installed dependencies using:  
   ```bash
   pip install -r requirements/dev/dev-requirements.txt
   ```  
also tried in GitHub Codespaces by activating the environment with:  
   ```bash
   conda activate mpl-dev
   ```  
4. Installed Matplotlib in editable mode with:  
   ```bash
   python -m pip install --verbose --no-build-isolation --editable ".[dev]"
   ```  
5. Also tried:  
   ```bash
   python -m pip install --no-build-isolation --config-settings=editable-verbose=true --editable .
   ```  

Despite these steps, I’m still encountering the import error. Could you help me figure out why this might be happening?

> also tried in GitHub Codespaces by activating the environment

@saikarna913 Just to confirm, you activated your virtual environment when you were trying locally?

Also, please provide the error messages you're getting.
@NGWi, it is working after clearing my old versions and the above code runs, so please review my pull request and suggest the changes



Patch:
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


Test patch:
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}"