matplotlib / matplotlib/matplotlib
[Bug]: add_subfigure does not respect GridSpec parameters
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 23.2k
- Forks
- 8.5k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 66
Description
### Bug summary
According the documentation for `Figure.add_subfigure`, you should be able to use `GridSpec` to create a set of subfigures to fill a certain space of a parent figure. However, no matter what settings are input into `GridSpec`, the parent figure is divided into equally sized subfigures. Doing this same process with `Figure.add_subplot` produces the expected results with the axes bounded by the input extent and separated by the correct `hspace` and `wspace`.
### Code for reproduction
```Python
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import numpy as np
dpi = 100
width = 600
height = 600
main = plt.figure(figsize=(width / dpi, height / dpi), dpi=100,
facecolor='#9a9a9a', frameon=False)
gs = GridSpec(2, 4, left=.1, right=.9, bottom=0.1,
top=0.9, wspace=0.15, hspace=0.15)
colors = np.array([
['red', 'black', 'green', 'blue'],
['brown', 'gray', 'white', 'gold']
])
panels = []
for row in range(2):
for col in range(4):
main.add_subfigure(gs[row, col], facecolor=colors[row][col])
plt.show()
```
### Actual outcome
The output is equally sized subfigures without any space between them and the full extent being used despite the input restrictions.

### Expected outcome
What is expected is a properly spaced areas with the proper extent. This can be produced when using `add_subplot` instead of `add_subfigure`.

### Additional information
_No response_
### Operating system
_No response_
### Matplotlib Version
3.8.3
### Matplotlib Backend
_No response_
### Python version
3.12.2
### Jupyter version
_No response_
### Installation
conda
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided Python reproduction with Matplotlib 3.8.3, then inspect Figure.add_subfigure and its GridSpec handling. Compare the result with Figure.add_subplot and verify that GridSpec extents, wspace, and hspace control the subfigure layout as documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100