Skip to content

Adding figratio to matplotlib to allow dynamic change of figure size based upon width/height and aspect #29262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""
`matplotlib.figure` implements the following classes:

Expand Down Expand Up @@ -3220,6 +3220,44 @@
"""
self.set_size_inches(self.get_figwidth(), val, forward=forward)

def set_figratio(self, width=None, height=None, aspect=None, forward=True):
"""
Set the height of the figure in inches.

Parameters
----------
height : float
width : float
aspect: float
forward : bool
See `set_size_inches`.

See Also
--------
matplotlib.figure.Figure.set_figwidth
matplotlib.figure.Figure.set_figheight
matplotlib.figure.Figure.set_size_inches
"""
if height and width:
self.set_size_inches(width, height, forward=forward)
elif aspect:
if height:
self.set_size_inches(
self.get_figwidth()*aspect, height, forward=forward
)
elif width:
self.set_size_inches(
width, self.get_figheight()*aspect, forward=forward
)
else:
self.set_size_inches(
self.get_figwidth()*aspect,
self.get_figheight()*aspect,
forward=forward
)
else:
raise ValueError("No value passed for aspect")

def clear(self, keep_observers=False):
# docstring inherited
super().clear(keep_observers=keep_observers)
Expand Down
43 changes: 43 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,49 @@ def test_set_fig_size():
assert fig.get_figheight() == 3


def test_set_fig_ratio():
# defining plt fig
fig = plt.figure()

# setting width and height individually
fig.set_figwidth(5)
fig.set_figheight(1)

# setting width and height with figratio
fig.set_figratio(2, 4)
assert fig.get_figwidth() == 2
assert fig.get_figheight() == 4

# setting width with value and height with aspect
fig.set_figratio(width=3, aspect=2)
assert fig.get_figwidth() == 3
assert fig.get_figheight() == 8

# setting height with value and width with aspect
fig.set_figratio(height=3, aspect=2)
assert fig.get_figwidth() == 6
assert fig.get_figheight() == 3

# upsizing width and height with an aspect of 4
fig.set_figratio(aspect=4)
assert fig.get_figwidth() == 24
assert fig.get_figheight() == 12

# error checking for when only width is passed in
try:
fig.set_figratio(width=4)
except ValueError:
assert fig.get_figwidth() == 24
assert fig.get_figheight() == 12

# error checking for when only height is passed in
try:
fig.set_figratio(height=4)
except ValueError:
assert fig.get_figwidth() == 24
assert fig.get_figheight() == 12


def test_axes_remove():
fig, axs = plt.subplots(2, 2)
axs[-1, -1].remove()
Expand Down
Loading