Skip to content

Commit 9e9a3cd

Browse files
committed
Merge pull request #3194 from lkilcher/annotate_bbox_DArrow
Annotate bbox darrow
2 parents 1a87326 + 8e6aff2 commit 9e9a3cd

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

doc/users/annotations_guide.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ keyword arguments. Currently, following box styles are implemented.
4646
========== ============== ==========================
4747
Class Name Attrs
4848
========== ============== ==========================
49+
Circle ``circle`` pad=0.3
50+
DArrow ``darrow`` pad=0.3
4951
LArrow ``larrow`` pad=0.3
5052
RArrow ``rarrow`` pad=0.3
5153
Round ``round`` pad=0.3,rounding_size=None

examples/pylab_examples/fancybox_demo2.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
import matplotlib.pyplot as plt
33

44
styles = mpatch.BoxStyle.get_styles()
5+
spacing = 1.2
56

6-
figheight = (len(styles)+.5)
7+
figheight = (spacing * len(styles) + .5)
78
fig1 = plt.figure(1, (4/1.5, figheight/1.5))
89
fontsize = 0.3 * 72
910

10-
for i, (stylename, styleclass) in enumerate(styles.items()):
11-
fig1.text(0.5, (float(len(styles)) - 0.5 - i)/figheight, stylename,
11+
for i, stylename in enumerate(sorted(styles.keys())):
12+
fig1.text(0.5, (spacing * (float(len(styles)) - i) - 0.5)/figheight, stylename,
1213
ha="center",
1314
size=fontsize,
1415
transform=fig1.transFigure,

lib/matplotlib/patches.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,6 +2003,57 @@ def transmute(self, x0, y0, width, height, mutation_size):
20032003

20042004
_style_list["rarrow"] = RArrow
20052005

2006+
class DArrow(_Base):
2007+
"""
2008+
(Double) Arrow Box
2009+
"""
2010+
# This source is copied from LArrow,
2011+
# modified to add a right arrow to the bbox.
2012+
2013+
def __init__(self, pad=0.3):
2014+
self.pad = pad
2015+
super(BoxStyle.DArrow, self).__init__()
2016+
2017+
def transmute(self, x0, y0, width, height, mutation_size):
2018+
2019+
# padding
2020+
pad = mutation_size * self.pad
2021+
2022+
# width and height with padding added.
2023+
# The width is padded by the arrows, so we don't need to pad it.
2024+
height = height + 2. * pad
2025+
2026+
# boundary of the padded box
2027+
x0, y0 = x0 - pad, y0 - pad
2028+
x1, y1 = x0 + width, y0 + height
2029+
2030+
dx = (y1 - y0)/2.
2031+
dxx = dx * .5
2032+
# adjust x0. 1.4 <- sqrt(2)
2033+
x0 = x0 + pad / 1.4
2034+
2035+
cp = [(x0 + dxx, y0), (x1, y0), # bot-segment
2036+
(x1, y0 - dxx), (x1 + dx + dxx, y0 + dx),
2037+
(x1, y1 + dxx), # right-arrow
2038+
(x1, y1), (x0 + dxx, y1), # top-segment
2039+
(x0 + dxx, y1 + dxx), (x0 - dx, y0 + dx),
2040+
(x0 + dxx, y0 - dxx), # left-arrow
2041+
(x0 + dxx, y0), (x0 + dxx, y0)] # close-poly
2042+
2043+
com = [Path.MOVETO, Path.LINETO,
2044+
Path.LINETO, Path.LINETO,
2045+
Path.LINETO,
2046+
Path.LINETO, Path.LINETO,
2047+
Path.LINETO, Path.LINETO,
2048+
Path.LINETO,
2049+
Path.LINETO, Path.CLOSEPOLY]
2050+
2051+
path = Path(cp, com)
2052+
2053+
return path
2054+
2055+
_style_list['darrow'] = DArrow
2056+
20062057
class Round(_Base):
20072058
"""
20082059
A box with round corners.
Loading

lib/matplotlib/tests/test_arrow_patches.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ def test_fancyarrow():
3030
ax.tick_params(labelleft=False, labelbottom=False)
3131

3232

33+
@image_comparison(baseline_images=['boxarrow_test_image'], extensions=['png'])
34+
def test_boxarrow():
35+
36+
styles = matplotlib.patches.BoxStyle.get_styles()
37+
38+
n = len(styles)
39+
spacing = 1.2
40+
41+
figheight = (n * spacing + .5)
42+
fig1 = plt.figure(1, figsize=(4 / 1.5, figheight / 1.5))
43+
44+
fontsize = 0.3 * 72
45+
46+
for i, stylename in enumerate(sorted(styles.keys())):
47+
fig1.text(0.5, ((n - i) * spacing - 0.5)/figheight, stylename,
48+
ha="center",
49+
size=fontsize,
50+
transform=fig1.transFigure,
51+
bbox=dict(boxstyle=stylename, fc="w", ec="k"))
52+
53+
3354
if __name__ == '__main__':
3455
import nose
3556
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)