Skip to content

Add set_ & get_size_pixels #18611

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

Closed
wants to merge 1 commit into from
Closed
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
49 changes: 49 additions & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,55 @@ def get_size_inches(self):
"""
return np.array(self.bbox_inches.p1)

def set_size_pixels(self, w, h=None, forward=True):
"""
Set the figure size in pixels.

Call signatures::

fig.set_size_pixels(w, h) # OR
fig.set_size_pixels((w, h))

Parameters
----------
w : (float, float) or float
Width and height in pixels (if height not specified as a
separate argument) or width.
h : float
Height in pixels.
forward : bool, default: True
If ``True``, the canvas size is automatically updated, e.g.,
you can resize the figure window from the shell.

See Also
--------
matplotlib.figure.Figure.set_size_inches
matplotlib.figure.Figure.set_dpi
matplotlib.figure.Figure.set_figheight
"""
if h is None: # Got called with a single pair as argument.
w, h = w
size = np.array([w, h])
if not np.isfinite(size).all() or (size < 0).any():
raise ValueError(f'figure size must be positive finite not {size}')
self.set_size_inches(size / self.dpi, forward=forward)

def get_size_pixels(self):
"""
Return the current size of the figure in pixels.

Returns
-------
ndarray
The size (width, height) of the figure in pixels.

See Also
--------
matplotlib.figure.Figure.get_size_inches
matplotlib.figure.Figure.get_dpi
"""
return np.array(self.bbox_inches.p1) * self.dpi

def get_edgecolor(self):
"""Get the edge color of the Figure rectangle."""
return self.patch.get_edgecolor()
Expand Down