Construct a `.SubplotSpec` from a parent `.Figure` and either - a `.SubplotSpec` -- returned as is; - one or three numbers -- a MATLAB-style subplot specifier.
(figure, args)
| 560 | |
| 561 | @staticmethod |
| 562 | def _from_subplot_args(figure, args): |
| 563 | """ |
| 564 | Construct a `.SubplotSpec` from a parent `.Figure` and either |
| 565 | |
| 566 | - a `.SubplotSpec` -- returned as is; |
| 567 | - one or three numbers -- a MATLAB-style subplot specifier. |
| 568 | """ |
| 569 | if len(args) == 1: |
| 570 | arg, = args |
| 571 | if isinstance(arg, SubplotSpec): |
| 572 | return arg |
| 573 | elif not isinstance(arg, Integral): |
| 574 | raise ValueError( |
| 575 | f"Single argument to subplot must be a three-digit " |
| 576 | f"integer, not {arg!r}") |
| 577 | try: |
| 578 | rows, cols, num = map(int, str(arg)) |
| 579 | except ValueError: |
| 580 | raise ValueError( |
| 581 | f"Single argument to subplot must be a three-digit " |
| 582 | f"integer, not {arg!r}") from None |
| 583 | elif len(args) == 3: |
| 584 | rows, cols, num = args |
| 585 | else: |
| 586 | raise _api.nargs_error("subplot", takes="1 or 3", given=len(args)) |
| 587 | |
| 588 | gs = GridSpec._check_gridspec_exists(figure, rows, cols) |
| 589 | if gs is None: |
| 590 | gs = GridSpec(rows, cols, figure=figure) |
| 591 | if isinstance(num, tuple) and len(num) == 2: |
| 592 | if not all(isinstance(n, Integral) for n in num): |
| 593 | raise ValueError( |
| 594 | f"Subplot specifier tuple must contain integers, not {num}" |
| 595 | ) |
| 596 | i, j = num |
| 597 | else: |
| 598 | if not isinstance(num, Integral) or num < 1 or num > rows*cols: |
| 599 | raise ValueError( |
| 600 | f"num must be an integer with 1 <= num <= {rows*cols}, " |
| 601 | f"not {num!r}" |
| 602 | ) |
| 603 | i = j = num |
| 604 | return gs[i-1:j] |
| 605 | |
| 606 | # num2 is a property only to handle the case where it is None and someone |
| 607 | # mutates num1. |
no test coverage detected