Skip to content

closes #24617. adds figsize alias and fixed #25091 subplot param getter #28936

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 3 commits 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
122 changes: 122 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 @@ -288,7 +288,65 @@
doc=("The root `Figure`. To get the parent of a `SubFigure`, "
"use the `get_figure` method."))

def set_subplotparams(self, subplotparams={}):
"""
.. Set the subplot layout parameters.
Accepts either a `.SubplotParams` object, from which the relevant
parameters are copied, or a dictionary of subplot layout parameters.
If a dictionary is provided, this function is a convenience wrapper for
`matplotlib.figure.Figure.subplots_adjust`
Parameters
----------
subplotparams : `~matplotlib.figure.SubplotParams` or dict with keys
"left", "bottom", "right", 'top", "wspace", "hspace"] , optional
SubplotParams object to copy new subplot parameters from, or a dict
of SubplotParams constructor arguments.
By default, an empty dictionary is passed, which maintains the
current state of the figure's `.SubplotParams`
See Also
--------
matplotlib.figure.Figure.subplots_adjust
matplotlib.figure.Figure.get_subplotparams
"""

subplotparams_args = ["left", "bottom", "right",
"top", "wspace", "hspace"]
kwargs = {}
if isinstance(subplotparams, SubplotParams):
for key in subplotparams_args:
kwargs[key] = getattr(subplotparams, key)

Check warning on line 317 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L317

Added line #L317 was not covered by tests
elif isinstance(subplotparams, dict):
for key in subplotparams.keys():
if key in subplotparams_args:
kwargs[key] = subplotparams[key]
else:
_api.warn_external(

Check warning on line 323 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L323

Added line #L323 was not covered by tests
f"'{key}' is not a valid key for set_subplotparams;"
" this key was ignored.")
else:
raise TypeError(

Check warning on line 327 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L327

Added line #L327 was not covered by tests
"subplotparams must be a dictionary of keyword-argument pairs or"
" an instance of SubplotParams()")
if kwargs == {}:
self.set_subplotparams(self.get_subplotparams())
self.subplots_adjust(**kwargs)

Check warning on line 332 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L331-L332

Added lines #L331 - L332 were not covered by tests

def get_subplotparams(self):
"""

Check failure on line 335 in lib/matplotlib/figure.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 D411 Missing blank line before section Raw Output: ./lib/matplotlib/figure.py:335:1: D411 Missing blank line before section

Check failure on line 335 in lib/matplotlib/figure.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 D411 Missing blank line before section Raw Output: ./lib/matplotlib/figure.py:335:1: D411 Missing blank line before section
Return the `.SubplotParams` object associated with the Figure.
Returns
-------
.SubplotParams`
See Also
--------
matplotlib.figure.Figure.subplots_adjust
matplotlib.figure.Figure.get_subplotparams
"""
subplotparms = []

Check warning on line 345 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L345

Added line #L345 was not covered by tests
for subfig in self.subfigs:
subplotparms.append(subfig.get_subplotparams())
return subplotparms

Check warning on line 348 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L347-L348

Added lines #L347 - L348 were not covered by tests
def contains(self, mouseevent):

Check failure on line 349 in lib/matplotlib/figure.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 E301 expected 1 blank line, found 0 Raw Output: ./lib/matplotlib/figure.py:349:5: E301 expected 1 blank line, found 0

Check failure on line 349 in lib/matplotlib/figure.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 E301 expected 1 blank line, found 0 Raw Output: ./lib/matplotlib/figure.py:349:5: E301 expected 1 blank line, found 0
"""
Test whether the mouse event occurred on the figure.

Expand Down Expand Up @@ -2866,7 +2924,71 @@
self.set_layout_engine(_tight, **_tight_parameters)
self.stale = True

def get_subplotparams(self):
"""

Check failure on line 2928 in lib/matplotlib/figure.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 D411 Missing blank line before section Raw Output: ./lib/matplotlib/figure.py:2928:1: D411 Missing blank line before section

Check failure on line 2928 in lib/matplotlib/figure.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 D411 Missing blank line before section Raw Output: ./lib/matplotlib/figure.py:2928:1: D411 Missing blank line before section
Return the `.SubplotParams` object associated with the Figure.
Returns
-------
.SubplotParams`
See Also
--------
matplotlib.figure.Figure.subplots_adjust
matplotlib.figure.Figure.get_subplotparams
"""
return self.subplotpars

def set_subplotparams(self, subplotparams={}):
"""
.. Set the subplot layout parameters.
Accepts either a `.SubplotParams` object, from which the relevant
parameters are copied, or a dictionary of subplot layout parameters.
If a dictionary is provided, this function is a convenience wrapper for
`matplotlib.figure.Figure.subplots_adjust`
Parameters
----------
subplotparams : `~matplotlib.figure.SubplotParams` or dict with keys
"left", "bottom", "right", 'top", "wspace", "hspace"] , optional
SubplotParams object to copy new subplot parameters from, or a dict
of SubplotParams constructor arguments.
By default, an empty dictionary is passed, which maintains the
current state of the figure's `.SubplotParams`
See Also
--------
matplotlib.figure.Figure.subplots_adjust
matplotlib.figure.Figure.get_subplotparams
"""
subplotparams_args = ["left", "bottom", "right",
"top", "wspace", "hspace"]
kwargs = {}
if isinstance(subplotparams, SubplotParams):
for key in subplotparams_args:
kwargs[key] = getattr(subplotparams, key)
elif isinstance(subplotparams, dict):
for key in subplotparams.keys():
if key in subplotparams_args:
kwargs[key] = subplotparams[key]
else:
_api.warn_external(
f"'{key}' is not a valid key for set_subplotparams;"
" this key was ignored.")
else:
raise TypeError(
"subplotparams must be a dictionary of keyword-argument pairs or"
" an instance of SubplotParams()")
if kwargs == {}:
self.set_subplotparams(self.get_subplotparams())
self.subplots_adjust(**kwargs)

@property
def get_figsize(self):
return self.get_size_inches()

Check warning on line 2984 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2984

Added line #L2984 was not covered by tests

@property
def set_figsize(self, w, h=None, forward=True):
self.set_size_inches(self, w, h=None, forward=True)

Check warning on line 2988 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2988

Added line #L2988 was not covered by tests


def get_constrained_layout(self):

Check failure on line 2991 in lib/matplotlib/figure.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 E303 too many blank lines (2) Raw Output: ./lib/matplotlib/figure.py:2991:5: E303 too many blank lines (2)

Check failure on line 2991 in lib/matplotlib/figure.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 E303 too many blank lines (2) Raw Output: ./lib/matplotlib/figure.py:2991:5: E303 too many blank lines (2)
"""
Return whether constrained layout is being used.

Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/figure.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections.abc import Callable, Hashable, Iterable, Sequence

Check failure on line 1 in lib/matplotlib/figure.pyi

View workflow job for this annotation

GitHub Actions / mypy-stubtest

[mypy-stubtest] reported by reviewdog 🐶 matplotlib.figure.Figure.get_figsize is not present in stub MISSING Runtime: <property object at 0x7f072d60cd10> Raw Output: error: matplotlib.figure.Figure.get_figsize is not present in stub Stub: in file /home/runner/work/matplotlib/matplotlib/lib/matplotlib/figure.pyi MISSING Runtime: <property object at 0x7f072d60cd10>

Check failure on line 1 in lib/matplotlib/figure.pyi

View workflow job for this annotation

GitHub Actions / mypy-stubtest

[mypy-stubtest] reported by reviewdog 🐶 matplotlib.figure.Figure.set_figsize is not present in stub MISSING Runtime: <property object at 0x7f070f13c0e0> Raw Output: error: matplotlib.figure.Figure.set_figsize is not present in stub Stub: in file /home/runner/work/matplotlib/matplotlib/lib/matplotlib/figure.pyi MISSING Runtime: <property object at 0x7f070f13c0e0>

Check failure on line 1 in lib/matplotlib/figure.pyi

View workflow job for this annotation

GitHub Actions / mypy-stubtest

[mypy-stubtest] reported by reviewdog 🐶 matplotlib.figure.FigureBase.set_subplotparams is not present in stub MISSING Runtime: in file /home/runner/work/matplotlib/matplotlib/lib/matplotlib/figure.py:291 def (self, subplotparams={}) Raw Output: error: matplotlib.figure.FigureBase.set_subplotparams is not present in stub Stub: in file /home/runner/work/matplotlib/matplotlib/lib/matplotlib/figure.pyi MISSING Runtime: in file /home/runner/work/matplotlib/matplotlib/lib/matplotlib/figure.py:291 def (self, subplotparams={})

Check failure on line 1 in lib/matplotlib/figure.pyi

View workflow job for this annotation

GitHub Actions / mypy-stubtest

[mypy-stubtest] reported by reviewdog 🐶 matplotlib.figure.Figure.get_figsize is not present in stub MISSING Runtime: <property object at 0x7fd35616acf0> Raw Output: error: matplotlib.figure.Figure.get_figsize is not present in stub Stub: in file /home/runner/work/matplotlib/matplotlib/lib/matplotlib/figure.pyi MISSING Runtime: <property object at 0x7fd35616acf0>

Check failure on line 1 in lib/matplotlib/figure.pyi

View workflow job for this annotation

GitHub Actions / mypy-stubtest

[mypy-stubtest] reported by reviewdog 🐶 matplotlib.figure.Figure.set_figsize is not present in stub MISSING Runtime: <property object at 0x7fd381b6c4f0> Raw Output: error: matplotlib.figure.Figure.set_figsize is not present in stub Stub: in file /home/runner/work/matplotlib/matplotlib/lib/matplotlib/figure.pyi MISSING Runtime: <property object at 0x7fd381b6c4f0>

Check failure on line 1 in lib/matplotlib/figure.pyi

View workflow job for this annotation

GitHub Actions / mypy-stubtest

[mypy-stubtest] reported by reviewdog 🐶 matplotlib.figure.FigureBase.set_subplotparams is not present in stub MISSING Runtime: in file /home/runner/work/matplotlib/matplotlib/lib/matplotlib/figure.py:291 def (self, subplotparams={}) Raw Output: error: matplotlib.figure.FigureBase.set_subplotparams is not present in stub Stub: in file /home/runner/work/matplotlib/matplotlib/lib/matplotlib/figure.pyi MISSING Runtime: in file /home/runner/work/matplotlib/matplotlib/lib/matplotlib/figure.py:291 def (self, subplotparams={})
import os
from typing import Any, IO, Literal, TypeVar, overload

Expand Down Expand Up @@ -67,6 +67,8 @@
def get_figure(self, root: Literal[False]) -> Figure | SubFigure: ...
@overload
def get_figure(self, root: bool = ...) -> Figure | SubFigure: ...

def get_subplotparams(self) -> SubplotParams: ...
def set_frameon(self, b: bool) -> None: ...
@property
def frameon(self) -> bool: ...
Expand Down
26 changes: 26 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,34 @@
assert fig.axes == [ax0, ax1]
assert fig.gca() is ax1

def test_get_subplot_params():

Check failure on line 244 in lib/matplotlib/tests/test_figure.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 E302 expected 2 blank lines, found 1 Raw Output: ./lib/matplotlib/tests/test_figure.py:244:1: E302 expected 2 blank lines, found 1

Check failure on line 244 in lib/matplotlib/tests/test_figure.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 E302 expected 2 blank lines, found 1 Raw Output: ./lib/matplotlib/tests/test_figure.py:244:1: E302 expected 2 blank lines, found 1
fig = plt.figure()
subplotparams_keys = ["left", "bottom", "right", "top", "wspace", "hspace"]
subplotparams = fig.get_subplotparams()
test_dict = {}
for key in subplotparams_keys:
attr = getattr(subplotparams, key)
assert attr == mpl.rcParams[f"figure.subplot.{key}"]
test_dict[key] = attr * 2

fig.set_subplotparams(test_dict)
for key, value in test_dict.items():
assert getattr(fig.get_subplotparams(), key) == value

test_dict['foo'] = 'bar'

Check warning on line 258 in lib/matplotlib/tests/test_figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/tests/test_figure.py#L258

Added line #L258 was not covered by tests
with pytest.warns(UserWarning,
match="'foo' is not a valid key for set_subplotparams;"
" this key was ignored"):
fig.set_subplotparams(test_dict)

Check warning on line 262 in lib/matplotlib/tests/test_figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/tests/test_figure.py#L262

Added line #L262 was not covered by tests

with pytest.raises(TypeError,
match="subplotparams must be a dictionary of "
"keyword-argument pairs or "
"an instance of SubplotParams()"):
fig.set_subplotparams(['foo'])

Check warning on line 268 in lib/matplotlib/tests/test_figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/tests/test_figure.py#L268

Added line #L268 was not covered by tests

assert fig.subplotpars == fig.get_subplotparams()

Check warning on line 270 in lib/matplotlib/tests/test_figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/tests/test_figure.py#L270

Added line #L270 was not covered by tests
def test_add_subplot_subclass():

Check failure on line 271 in lib/matplotlib/tests/test_figure.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 E302 expected 2 blank lines, found 0 Raw Output: ./lib/matplotlib/tests/test_figure.py:271:1: E302 expected 2 blank lines, found 0

Check failure on line 271 in lib/matplotlib/tests/test_figure.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 E302 expected 2 blank lines, found 0 Raw Output: ./lib/matplotlib/tests/test_figure.py:271:1: E302 expected 2 blank lines, found 0
fig = plt.figure()
fig.add_subplot(axes_class=Axes)
with pytest.raises(ValueError):
Expand Down
Loading