| 57 | |
| 58 | |
| 59 | def _auto_grid( |
| 60 | nfacet: int, figsize: tuple[float, ...] | None, aspect: float |
| 61 | ) -> tuple[int, int]: |
| 62 | |
| 63 | # Try to align the grid to the figsize. If figsize is unknown it gets |
| 64 | # computed from the grid, so lets keep it as square as possible |
| 65 | faspect = 1 if figsize is None else figsize[0] / figsize[1] |
| 66 | |
| 67 | # Only wrap if > 3 images |
| 68 | if nfacet <= 3: |
| 69 | return nfacet, 1 |
| 70 | |
| 71 | # Geometric ideal case |
| 72 | ncol = int(np.ceil(np.sqrt(nfacet * faspect / aspect))) |
| 73 | ncol = max(1, min(ncol, nfacet)) |
| 74 | nrow = int(np.ceil(nfacet / ncol)) |
| 75 | |
| 76 | # Reduce columns as long as we don't need more rows |
| 77 | # This eliminates empty slots in the last row if aspect < 1 |
| 78 | while ncol > 1 and (ncol - 1) * nrow >= nfacet: |
| 79 | ncol -= 1 |
| 80 | |
| 81 | return ncol, nrow |
| 82 | |
| 83 | |
| 84 | T_FacetGrid = TypeVar("T_FacetGrid", bound="FacetGrid") |