Convert a figsize expression to (width, height) in inches. Parameters ---------- figsize : (float, float) or (float, float, str) This can be - a tuple ``(width, height, unit)``, where *unit* is one of "in" (inch), "cm" (centimenter), "px" (pixel).
(figsize, dpi)
| 3752 | |
| 3753 | |
| 3754 | def _parse_figsize(figsize, dpi): |
| 3755 | """ |
| 3756 | Convert a figsize expression to (width, height) in inches. |
| 3757 | |
| 3758 | Parameters |
| 3759 | ---------- |
| 3760 | figsize : (float, float) or (float, float, str) |
| 3761 | This can be |
| 3762 | |
| 3763 | - a tuple ``(width, height, unit)``, where *unit* is one of "in" (inch), |
| 3764 | "cm" (centimenter), "px" (pixel). |
| 3765 | - a tuple ``(width, height)``, which is interpreted in inches, i.e. as |
| 3766 | ``(width, height, "in")``. |
| 3767 | |
| 3768 | dpi : float |
| 3769 | The dots-per-inch; used for converting 'px' to 'in'. |
| 3770 | """ |
| 3771 | num_parts = len(figsize) |
| 3772 | if num_parts == 2: |
| 3773 | x, y = figsize |
| 3774 | elif num_parts == 3: |
| 3775 | x, y, unit = figsize |
| 3776 | if unit == 'in': |
| 3777 | pass |
| 3778 | elif unit == 'cm': |
| 3779 | if x is not None: |
| 3780 | x /= 2.54 |
| 3781 | if y is not None: |
| 3782 | y /= 2.54 |
| 3783 | elif unit == 'px': |
| 3784 | if x is not None: |
| 3785 | x /= dpi |
| 3786 | if y is not None: |
| 3787 | y /= dpi |
| 3788 | else: |
| 3789 | raise ValueError( |
| 3790 | f"Invalid unit {unit!r} in 'figsize'; " |
| 3791 | "supported units are 'in', 'cm', 'px'" |
| 3792 | ) |
| 3793 | else: |
| 3794 | raise ValueError( |
| 3795 | "Invalid figsize format, expected (x, y) or (x, y, unit) but got " |
| 3796 | f"{figsize!r}" |
| 3797 | ) |
| 3798 | |
| 3799 | if x is None and y is None: |
| 3800 | raise ValueError( |
| 3801 | "figsize=(None, None) is invalid; at least one of width or " |
| 3802 | "height must be provided") |
| 3803 | |
| 3804 | default_width, default_height = mpl.rcParams["figure.figsize"] |
| 3805 | if x is None: |
| 3806 | x = default_width |
| 3807 | if y is None: |
| 3808 | y = default_height |
| 3809 | return x, y |
no outgoing calls
no test coverage detected
searching dependent graphs…