Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: Add ellipse class for annotation box styles #24596

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 7 commits into from
Dec 8, 2022
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
1 change: 1 addition & 0 deletions doc/missing-references.json
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@
"matplotlib.patches.BoxStyle._Base": [
"lib/matplotlib/patches.py:docstring of matplotlib.patches.BoxStyle.Circle:1",
"lib/matplotlib/patches.py:docstring of matplotlib.patches.BoxStyle.DArrow:1",
"lib/matplotlib/patches.py:docstring of matplotlib.patches.BoxStyle.Ellipse:1",
"lib/matplotlib/patches.py:docstring of matplotlib.patches.BoxStyle.LArrow:1",
"lib/matplotlib/patches.py:docstring of matplotlib.patches.BoxStyle.Round4:1",
"lib/matplotlib/patches.py:docstring of matplotlib.patches.BoxStyle.Round:1",
Expand Down
15 changes: 15 additions & 0 deletions doc/users/next_whats_new/ellipse_annotation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
``ellipse`` boxstyle option for annotations
-------------------------------------------

The ``'ellipse'`` option for boxstyle can now be used to create annotations
with an elliptical outline. It can be used as a closed curve shape for
longer texts instead of the ``'circle'`` boxstyle which can get quite big.

.. plot::
:include-source: true

import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(5, 5))
t = ax.text(0.5, 0.5, "elliptical box",
ha="center", size=15,
bbox=dict(boxstyle="ellipse,pad=0.3"))
31 changes: 30 additions & 1 deletion lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import textwrap
from types import SimpleNamespace
from collections import namedtuple
from matplotlib.transforms import Affine2D

import numpy as np

Expand Down Expand Up @@ -2337,7 +2338,35 @@ def __call__(self, x0, y0, width, height, mutation_size):
# boundary of the padded box
x0, y0 = x0 - pad, y0 - pad
return Path.circle((x0 + width / 2, y0 + height / 2),
max(width, height) / 2)
max(width, height) / 2)

@_register_style(_style_list)
class Ellipse:
"""
An elliptical box.

.. versionadded:: 3.7
"""

def __init__(self, pad=0.3):
"""
Parameters
----------
pad : float, default: 0.3
The amount of padding around the original box.
"""
self.pad = pad

def __call__(self, x0, y0, width, height, mutation_size):
pad = mutation_size * self.pad
width, height = width + 2 * pad, height + 2 * pad
# boundary of the padded box
x0, y0 = x0 - pad, y0 - pad
a = width / math.sqrt(2)
b = height / math.sqrt(2)
trans = Affine2D().scale(a, b).translate(x0 + width / 2,
y0 + height / 2)
return trans.transform_path(Path.unit_circle())

@_register_style(_style_list)
class LArrow:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tutorials/text/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
# ========== ============== ==========================
# Circle ``circle`` pad=0.3
# DArrow ``darrow`` pad=0.3
# Ellipse ``ellipse`` pad=0.3
# LArrow ``larrow`` pad=0.3
# RArrow ``rarrow`` pad=0.3
# Round ``round`` pad=0.3,rounding_size=None
Expand Down