-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ENH: Allow tuple for borderpad in AnchoredOffsetbox #30359
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
base: main
Are you sure you want to change the base?
ENH: Allow tuple for borderpad in AnchoredOffsetbox #30359
Conversation
98cfadf
to
8df4614
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Aaratrika-Shelly, nice work! I have confirmed this solves the inset_axes
issue:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
fig, ax = plt.subplots()
axins = inset_axes(ax, width="30%", height=1., loc="upper left", borderpad=(3, 0))
plt.show()

Please could you update the inset_axes docstring to match what you have done here? This should also get a what's new entry and "added" directive.
8df4614
to
c84ef64
Compare
All the suggested changes have been made. Thanks for the suggestions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Aaratrika-Shelly, this will still need a new entry in https://github.com/matplotlib/matplotlib/tree/main/doc/users/next_whats_new
Also I just remembered that the type annotation will need updating in offsetbox.pyi. We do not yet have typing stubs for anything under toolkits
so there is nothing to do for type annotations for inset_axes
.
ax.add_artist(text_tuple_equal) | ||
|
||
# Case 3: Test that an asymmetric tuple with larger values works as expected. | ||
text_tuple_asym = AnchoredText("tuple_asym", loc='lower left', borderpad=(10, 15)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest making one of these less than 5. At the moment the below assertions would pass even if only one element of the tuple mistakenly got used for both x and y.
versionchanged:: 3.11 | ||
The *borderpad* parameter now accepts a tuple of (x, y) paddings. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be versionadded
because you are introducing a new option but the old option still works. Also it should go under the parameter description. For a similar example see
matplotlib/lib/matplotlib/stackplot.py
Lines 67 to 68 in 011d12f
.. versionadded:: 3.9 | |
Support for list input |
c84ef64
to
2b60116
Compare
5bd8215
to
b98dbdc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only minor nits left.
lib/matplotlib/offsetbox.py
Outdated
the *loc* code with the *borderpad*. | ||
The paddings are specified by *pad_x* and *pad_y*. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the *loc* code with the *borderpad*. | |
The paddings are specified by *pad_x* and *pad_y*. | |
the *loc* code and padding *pad_x*, *pad_y*. |
lib/matplotlib/legend.py
Outdated
return offsetbox._get_anchored_bbox( | ||
loc, bbox, parentbbox, | ||
self.borderaxespad * renderer.points_to_pixels(self._fontsize)) | ||
pad,pad) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pad,pad) | |
pad, pad) |
Could also be inlined to the line before.
b98dbdc
to
abbc352
Compare
text_tuple_equal = AnchoredText("tuple", loc='lower left', borderpad=(5, 5)) | ||
ax.add_artist(text_tuple_equal) | ||
|
||
# Case 3: Test that an asymmetric tuple with larger values works as expected. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Case 3: Test that an asymmetric tuple with larger values works as expected. | |
# Case 3: Test that an asymmetric tuple with different values works as expected. |
# Assertion 2: Prove that the larger, asymmetric padding moved the box | ||
# further from the origin than the baseline. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Assertion 2: Prove that the larger, asymmetric padding moved the box | |
# further from the origin than the baseline. | |
# Assertion 2: Prove that the asymmetric padding moved the box | |
# further from the origin than the baseline in the x-direction and less far in the y-direction. |
The units are axes font size, i.e. for a default font size of 10 points | ||
*borderpad = 0.5* is equivalent to a padding of 5 points. | ||
.. versionadded:: 3.10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.. versionadded:: 3.10 | |
.. versionadded:: 3.11 |
v3.11 is the next version. This should also go in the AnchoredOffsetbox
docstring.
@@ -256,6 +256,7 @@ def inset_axes(parent_axes, width, height, loc='upper right', | |||
axes_class=None, axes_kwargs=None, | |||
borderpad=0.5): | |||
""" | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accidentally added line I think
@@ -157,7 +157,7 @@ class AnchoredOffsetbox(OffsetBox): | |||
loc: str, | |||
*, | |||
pad: float = ..., | |||
borderpad: float = ..., | |||
borderpad: float | tuple[float, float] = ..., |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same change should be made for AnchoredText
below.
Since you are amending the one commit, it would be good to also correct the typo: "tupple" -> "tuple" |
abbc352
to
ca8f475
Compare
This PR closes #30331 .
PR summary
This PR enhances
AnchoredOffsetbox
to allow theborderpad
parameter to accept a tuple of(x_pad, y_pad)
in addition to a single float value.Why is this change necessary? Currently,
borderpad
applies the same padding in both x and y directions. This can be inflexible, especially when placing inset axes in a corner. For example, a user may want horizontal padding to avoid overlapping the main plot's y-axis labels, but no vertical padding in order to keep the inset flush with the top of the plot area.What problem does it solve? It gives users finer control over the positioning of anchored elements, allowing for separate horizontal and vertical padding.
What is the reasoning for this implementation?
AnchoredOffsetbox.get_offset
.self.borderpad
is a tuple and calculates separatepad_x_pixels
andpad_y_pixels
.A new unit test has been added to verify this functionality, and the docstring for
AnchoredOffsetbox
has been updated to reflect the new API.PR checklist