Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Aaratrika-Shelly
Copy link

This PR closes #30331 .

PR summary

This PR enhances AnchoredOffsetbox to allow the borderpad 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?

    • The implementation modifies AnchoredOffsetbox.get_offset.
    • It checks if self.borderpad is a tuple and calculates separate pad_x_pixels and pad_y_pixels.
    • These values are then used to create a manually padded anchor box, which is a robust approach that does not require changing downstream helper functions.
    • The implementation is fully backward-compatible, treating single float values as before.

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

@Aaratrika-Shelly Aaratrika-Shelly force-pushed the feature/tuple-borderpad branch 3 times, most recently from 98cfadf to 8df4614 Compare July 26, 2025 07:24
Copy link
Member

@rcomer rcomer left a 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()
Image

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.

@rcomer rcomer changed the title ENH: Allow tupple for borderpad in AnchoredOffsetbox ENH: Allow tuple for borderpad in AnchoredOffsetbox Jul 26, 2025
@melissawm melissawm moved this to Waiting for author in First Time Contributors Jul 26, 2025
@Aaratrika-Shelly Aaratrika-Shelly force-pushed the feature/tuple-borderpad branch from 8df4614 to c84ef64 Compare July 26, 2025 21:46
@Aaratrika-Shelly
Copy link
Author

All the suggested changes have been made. Thanks for the suggestions.

Copy link
Member

@rcomer rcomer left a 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.

@Aaratrika-Shelly Aaratrika-Shelly force-pushed the feature/tuple-borderpad branch from c84ef64 to 2b60116 Compare July 27, 2025 20:21
@Aaratrika-Shelly Aaratrika-Shelly force-pushed the feature/tuple-borderpad branch 2 times, most recently from 5bd8215 to b98dbdc Compare July 28, 2025 10:19
Copy link
Member

@timhoffm timhoffm left a 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.

Comment on lines 1102 to 1103
the *loc* code with the *borderpad*.
The paddings are specified by *pad_x* and *pad_y*.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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*.

return offsetbox._get_anchored_bbox(
loc, bbox, parentbbox,
self.borderaxespad * renderer.points_to_pixels(self._fontsize))
pad,pad)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pad,pad)
pad, pad)

Could also be inlined to the line before.

@Aaratrika-Shelly Aaratrika-Shelly force-pushed the feature/tuple-borderpad branch from b98dbdc to abbc352 Compare July 28, 2025 12:22
@rcomer
Copy link
Member

rcomer commented Jul 28, 2025

Since you are amending the one commit, it would be good to also correct the typo: "tupple" -> "tuple"

Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Aaratrika-Shelly, just one missing blank line now as far as I can see.

@Aaratrika-Shelly Aaratrika-Shelly force-pushed the feature/tuple-borderpad branch from ca8f475 to c3ce74a Compare July 29, 2025 09:23
@Aaratrika-Shelly Aaratrika-Shelly requested a review from rcomer July 29, 2025 10:18
@rcomer rcomer removed their request for review July 29, 2025 10:23
@Aaratrika-Shelly Aaratrika-Shelly force-pushed the feature/tuple-borderpad branch from c3ce74a to 2a5d41b Compare July 29, 2025 10:42
@Aaratrika-Shelly Aaratrika-Shelly requested a review from rcomer July 29, 2025 11:00
@Aaratrika-Shelly
Copy link
Author

"It looks like all the tests passed except for a single failure in test_openin_any_paranoid, which appears to be a CI environment issue related to the DISPLAY variable.

@rcomer
Copy link
Member

rcomer commented Jul 29, 2025

Hi @Aaratrika-Shelly it looks like your last push did not change anything. Did you forget to git add?

@rcomer rcomer removed their request for review July 29, 2025 12:12
@Aaratrika-Shelly Aaratrika-Shelly force-pushed the feature/tuple-borderpad branch from 2a5d41b to a43d64c Compare July 29, 2025 12:50
@Aaratrika-Shelly Aaratrika-Shelly requested a review from rcomer July 29, 2025 13:49
Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rcomer rcomer added this to the v3.11.0 milestone Jul 29, 2025
@rcomer rcomer moved this from Waiting for author to Needs review in First Time Contributors Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs review
Development

Successfully merging this pull request may close these issues.

[ENH]: inset_axes has borderpadding, but not x/y individually.
3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy