Calculate the width and height for a figure with a specified aspect ratio. While the height is taken from :rc:`figure.figsize`, the width is adjusted to match the desired aspect ratio. Additionally, it is ensured that the width is in the range [4., 16.] and the height is in the ran
(arg)
| 3672 | |
| 3673 | |
| 3674 | def figaspect(arg): |
| 3675 | """ |
| 3676 | Calculate the width and height for a figure with a specified aspect ratio. |
| 3677 | |
| 3678 | While the height is taken from :rc:`figure.figsize`, the width is |
| 3679 | adjusted to match the desired aspect ratio. Additionally, it is ensured |
| 3680 | that the width is in the range [4., 16.] and the height is in the range |
| 3681 | [2., 16.]. If necessary, the default height is adjusted to ensure this. |
| 3682 | |
| 3683 | Parameters |
| 3684 | ---------- |
| 3685 | arg : float or 2D array |
| 3686 | If a float, this defines the aspect ratio (i.e. the ratio height / |
| 3687 | width). |
| 3688 | In case of an array the aspect ratio is number of rows / number of |
| 3689 | columns, so that the array could be fitted in the figure undistorted. |
| 3690 | |
| 3691 | Returns |
| 3692 | ------- |
| 3693 | size : (2,) array |
| 3694 | The width and height of the figure in inches. |
| 3695 | |
| 3696 | Notes |
| 3697 | ----- |
| 3698 | If you want to create an Axes within the figure, that still preserves the |
| 3699 | aspect ratio, be sure to create it with equal width and height. See |
| 3700 | examples below. |
| 3701 | |
| 3702 | Thanks to Fernando Perez for this function. |
| 3703 | |
| 3704 | Examples |
| 3705 | -------- |
| 3706 | Make a figure twice as tall as it is wide:: |
| 3707 | |
| 3708 | w, h = figaspect(2.) |
| 3709 | fig = Figure(figsize=(w, h)) |
| 3710 | ax = fig.add_axes((0.1, 0.1, 0.8, 0.8)) |
| 3711 | ax.imshow(A, **kwargs) |
| 3712 | |
| 3713 | Make a figure with the proper aspect for an array:: |
| 3714 | |
| 3715 | A = rand(5, 3) |
| 3716 | w, h = figaspect(A) |
| 3717 | fig = Figure(figsize=(w, h)) |
| 3718 | ax = fig.add_axes((0.1, 0.1, 0.8, 0.8)) |
| 3719 | ax.imshow(A, **kwargs) |
| 3720 | """ |
| 3721 | |
| 3722 | isarray = hasattr(arg, 'shape') and not np.isscalar(arg) |
| 3723 | |
| 3724 | # min/max sizes to respect when autoscaling. If John likes the idea, they |
| 3725 | # could become rc parameters, for now they're hardwired. |
| 3726 | figsize_min = np.array((4.0, 2.0)) # min length for width/height |
| 3727 | figsize_max = np.array((16.0, 16.0)) # max length for width/height |
| 3728 | |
| 3729 | # Extract the aspect ratio of the array |
| 3730 | if isarray: |
| 3731 | nr, nc = arg.shape[:2] |