-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix behaviour of Figure.clear() for SubplotParams #27183
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
Changes from 26 commits
cf6032e
68386fa
035a9c0
c2c13ff
247833c
e2039f8
1b76817
51876fc
1470bf2
199cc4a
9772578
88eed8c
da31555
12f26bd
e39db47
03ccff6
0086dcf
e9b5a61
ded306c
5e40238
e0b75ec
ddcc0b4
aa740f3
0766ec0
50e9b46
3690d94
e119ab0
275825f
b2cf506
7665053
10d8216
d3f8db1
6725328
a91275e
7d3e386
9d772c4
98df0da
fac13d1
49d1eca
5c9674a
78d699c
5ad2d55
d5c9f7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Resetting the subplot parameters for figure.clear() | ||
--------------------------------------------------- | ||
|
||
When calling `.Figure.clear()` the settings for `.gridspec.SubplotParams` are restored to the default values. | ||
|
||
The `.gridspec.SubplotParams` object has a new get method :meth:`~.SubplotParams.get_subplot_params` and a | ||
method to reset the parameters to the defaults :meth:`~.SubplotParams.reset` | ||
|
||
|
||
(contributed by @eendebakpt based on work by @fredrik-1) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
methods like `~.pyplot.subplots`, `~.pyplot.subplot_mosaic` and | ||
`~.Figure.subfigures`. See the tutorial :ref:`arranging_axes` for a guide. | ||
""" | ||
|
||
from typing import Any | ||
eendebakpt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import copy | ||
import logging | ||
from numbers import Integral | ||
|
@@ -740,29 +740,36 @@ def __init__(self, left=None, bottom=None, right=None, top=None, | |
|
||
Parameters | ||
---------- | ||
left : float | ||
left : float, optional | ||
The position of the left edge of the subplots, | ||
as a fraction of the figure width. | ||
right : float | ||
right : float, optional | ||
The position of the right edge of the subplots, | ||
as a fraction of the figure width. | ||
bottom : float | ||
bottom : float, optional | ||
The position of the bottom edge of the subplots, | ||
as a fraction of the figure height. | ||
top : float | ||
top : float, optional | ||
The position of the top edge of the subplots, | ||
as a fraction of the figure height. | ||
wspace : float | ||
wspace : float, optional | ||
The width of the padding between subplots, | ||
as a fraction of the average Axes width. | ||
hspace : float | ||
hspace : float, optional | ||
The height of the padding between subplots, | ||
as a fraction of the average Axes height. | ||
""" | ||
for key in ["left", "bottom", "right", "top", "wspace", "hspace"]: | ||
setattr(self, key, mpl.rcParams[f"figure.subplot.{key}"]) | ||
self.update(left, bottom, right, top, wspace, hspace) | ||
|
||
def _repr_pretty_(self, p: Any, cycle: bool) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not simply define There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I do not think this change belongs in this PR though, so I removed it. Let me know if you want to to open a separate PR for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's rare that we/users ever access this, so I don't really care. But I think if we do this, we can afford to do |
||
del cycle | ||
name = self.__class__.__name__ | ||
s = f"{name}(left={self.left}, bottom={self.bottom}, right={self.right}, " | ||
s += f" top={self.top}, wspace={self.wspace}, hspace={self.hspace})" | ||
p.text(s) | ||
|
||
def update(self, left=None, bottom=None, right=None, top=None, | ||
wspace=None, hspace=None): | ||
""" | ||
|
@@ -774,15 +781,23 @@ def update(self, left=None, bottom=None, right=None, top=None, | |
if ((bottom if bottom is not None else self.bottom) | ||
>= (top if top is not None else self.top)): | ||
raise ValueError('bottom cannot be >= top') | ||
if left is not None: | ||
self.left = left | ||
if right is not None: | ||
self.right = right | ||
if bottom is not None: | ||
self.bottom = bottom | ||
if top is not None: | ||
self.top = top | ||
if wspace is not None: | ||
self.wspace = wspace | ||
if hspace is not None: | ||
self.hspace = hspace | ||
|
||
attributes = {'left': left, 'right': right, 'bottom': bottom, 'top': top, | ||
'hspace': hspace, 'wspace': wspace} | ||
for key, value in attributes.items(): | ||
if value is not None: | ||
setattr(self, key, value) | ||
eendebakpt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def reset(self): | ||
"""Restore the positioning parameters to the default values""" | ||
eendebakpt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for key in self.get_subplot_params().keys(): | ||
setattr(self, key, mpl.rcParams[f'figure.subplot.{key}']) | ||
|
||
def get_subplot_params(self) -> dict[str, float]: | ||
""" | ||
Returns | ||
------- | ||
subplot_params : dictionary | ||
A dictionary with the subplot parameters | ||
""" | ||
return self.__dict__.copy() | ||
eendebakpt marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.