| 89 | |
| 90 | |
| 91 | def _get_layout( |
| 92 | nplots: int, |
| 93 | layout: tuple[int, int] | None = None, |
| 94 | layout_type: str = "box", |
| 95 | ) -> tuple[int, int]: |
| 96 | if layout is not None: |
| 97 | if not isinstance(layout, (tuple, list)) or len(layout) != 2: |
| 98 | raise ValueError("Layout must be a tuple of (rows, columns)") |
| 99 | |
| 100 | nrows, ncols = layout |
| 101 | |
| 102 | if nrows == -1 and ncols > 0: |
| 103 | layout = (ceil(nplots / ncols), ncols) |
| 104 | elif ncols == -1 and nrows > 0: |
| 105 | layout = (nrows, ceil(nplots / nrows)) |
| 106 | elif ncols <= 0 and nrows <= 0: |
| 107 | msg = "At least one dimension of layout must be positive" |
| 108 | raise ValueError(msg) |
| 109 | |
| 110 | nrows, ncols = layout |
| 111 | if nrows * ncols < nplots: |
| 112 | raise ValueError( |
| 113 | f"Layout of {nrows}x{ncols} must be larger than required size {nplots}" |
| 114 | ) |
| 115 | |
| 116 | return layout |
| 117 | |
| 118 | if layout_type == "single": |
| 119 | return (1, 1) |
| 120 | elif layout_type == "horizontal": |
| 121 | return (1, nplots) |
| 122 | elif layout_type == "vertical": |
| 123 | return (nplots, 1) |
| 124 | |
| 125 | layouts = {1: (1, 1), 2: (1, 2), 3: (2, 2), 4: (2, 2)} |
| 126 | try: |
| 127 | return layouts[nplots] |
| 128 | except KeyError: |
| 129 | k = 1 |
| 130 | while k**2 < nplots: |
| 131 | k += 1 |
| 132 | |
| 133 | if (k - 1) * k >= nplots: |
| 134 | return k, (k - 1) |
| 135 | else: |
| 136 | return k, k |
| 137 | |
| 138 | |
| 139 | # copied from matplotlib/pyplot.py and modified for pandas.plotting |