Skip to content

DOC: Improve description of background/bbox handling for Text #29875

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

Merged
merged 1 commit into from
Apr 7, 2025
Merged
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
18 changes: 15 additions & 3 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,14 +516,21 @@ def _get_layout(self, renderer):

def set_bbox(self, rectprops):
"""
Draw a bounding box around self.
Draw a box behind/around the text.

This can be used to set a background and/or a frame around the text.
It's realized through a `.FancyBboxPatch` behind the text (see also
`.Text.get_bbox_patch`). The bbox patch is None by default and only
created when needed.

Parameters
----------
rectprops : dict with properties for `.patches.FancyBboxPatch`
rectprops : dict with properties for `.FancyBboxPatch` or None
Copy link
Member

Choose a reason for hiding this comment

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

Maybe one should update the typing as well? If not, feel free to self merge.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

The default boxstyle is 'square'. The mutation
scale of the `.patches.FancyBboxPatch` is set to the fontsize.

Pass ``None`` to remove the bbox patch completely.

Examples
--------
::
Expand Down Expand Up @@ -558,6 +565,8 @@ def get_bbox_patch(self):
"""
Return the bbox Patch, or None if the `.patches.FancyBboxPatch`
is not made.

For more details see `.Text.set_bbox`.
"""
return self._bbox_patch

Expand Down Expand Up @@ -981,7 +990,10 @@ def get_window_extent(self, renderer=None, dpi=None):

def set_backgroundcolor(self, color):
"""
Set the background color of the text by updating the bbox.
Set the background color of the text.

This is realized through the bbox (see `.set_bbox`),
creating the bbox patch if needed.

Parameters
----------
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/text.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Text(Artist):
def get_transform_rotates_text(self) -> bool: ...
def set_rotation_mode(self, m: None | Literal["default", "anchor", "xtick", "ytick"]) -> None: ...
def get_rotation_mode(self) -> Literal["default", "anchor", "xtick", "ytick"]: ...
def set_bbox(self, rectprops: dict[str, Any]) -> None: ...
def set_bbox(self, rectprops: dict[str, Any] | None) -> None: ...
def get_bbox_patch(self) -> None | FancyBboxPatch: ...
def update_bbox_position_size(self, renderer: RendererBase) -> None: ...
def get_wrap(self) -> bool: ...
Expand Down
Loading