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

Skip to content
Closed
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
28 changes: 28 additions & 0 deletions examples/pylab_examples/arrow_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import matplotlib.pyplot as plt
from matplotlib.testing.decorators import image_comparison

def draw_arrow(ax, t, r):
ax.annotate('', xy=(0.5, 0.5+r), xytext=(0.5, 0.5), size=30,
arrowprops=dict(arrowstyle=t,
fc="b", ec='k'))

def test_fancyarrow():
r = [0.4, 0.3, 0.2, 0.1]
t = ["fancy", "simple"]

fig, axes = plt.subplots(len(t), len(r), squeeze=False,
subplot_kw=dict(aspect=True),
figsize=(8, 4.5))

for i_r, r1 in enumerate(r):
for i_t, t1 in enumerate(t):
ax = axes[i_t, i_r]
draw_arrow(ax, t1, r1)
ax.tick_params(labelleft=False, labelbottom=False)


plt.show()

if __name__ == '__main__':
test_fancyarrow()

88 changes: 59 additions & 29 deletions lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def get_patch_transform(self):
"""
Return the :class:`~matplotlib.transforms.Transform` instance which
takes patch coordinates to data coordinates.

For example, one may define a patch of a circle which represents a
radius of 5 by providing coordinates for a unit circle, and a
transform which scales the coordinates (the patch coordinate) by 5.
Expand Down Expand Up @@ -2783,6 +2783,17 @@ def connect(self, posA, posB):
{"AvailableConnectorstyles": _pprint_styles(_style_list)}


def _point_along_a_line(x0, y0, x1, y1, d):
"""
find a point along a line connecting (x0, y0) -- (x1, y1) whose
distance from (x0, y0) is d.
"""
dx, dy = x0 - x1, y0 - y1
ff = d/(dx*dx+dy*dy)**.5
x2, y2 = x0 - ff*dx, y0 - ff*dy

return x2, y2


class ArrowStyle(_Style):
"""
Expand Down Expand Up @@ -3419,37 +3430,57 @@ def transmute(self, path, mutation_size, linewidth):
head_length = self.head_length * mutation_size
in_f = inside_circle(x2, y2, head_length)
arrow_path = [(x0, y0), (x1, y1), (x2, y2)]
arrow_out, arrow_in = \
split_bezier_intersecting_with_closedpath(arrow_path,
in_f,
tolerence=0.01)

from bezier import NonIntersectingPathException

try:
arrow_out, arrow_in = \
split_bezier_intersecting_with_closedpath(arrow_path,
in_f,
tolerence=0.01)
except NonIntersectingPathException:
# if this happens, make a straight line of the head_length long.
x0, y0 = _point_along_a_line(x2, y2, x1, y1, head_length)
x1n, y1n = 0.5*(x0+x2), 0.5*(y0+y2)
arrow_in = [(x0, y0), (x1n, y1n), (x2, y2)]
arrow_out = None

# head
head_width = self.head_width * mutation_size
head_l, head_r = make_wedged_bezier2(arrow_in, head_width/2.,
wm=.5)

head_left, head_right = \
make_wedged_bezier2(arrow_in, head_width/2.,
wm=.5)


# tail
tail_width = self.tail_width * mutation_size
tail_left, tail_right = get_parallels(arrow_out, tail_width/2.)
if arrow_out is not None:
tail_width = self.tail_width * mutation_size
tail_left, tail_right = get_parallels(arrow_out, tail_width/2.)

#head_right, head_left = head_r, head_l
patch_path = [(Path.MOVETO, tail_right[0]),
(Path.CURVE3, tail_right[1]),
(Path.CURVE3, tail_right[2]),
(Path.LINETO, head_right[0]),
(Path.CURVE3, head_right[1]),
(Path.CURVE3, head_right[2]),
(Path.CURVE3, head_left[1]),
(Path.CURVE3, head_left[0]),
(Path.LINETO, tail_left[2]),
(Path.CURVE3, tail_left[1]),
(Path.CURVE3, tail_left[0]),
(Path.LINETO, tail_right[0]),
(Path.CLOSEPOLY, tail_right[0]),
]
else:
patch_path = [(Path.MOVETO, head_right[0]),
(Path.CURVE3, head_right[1]),
(Path.CURVE3, head_right[2]),
(Path.CURVE3, head_left[1]),
(Path.CURVE3, head_left[0]),
(Path.CLOSEPOLY, head_left[0]),
]

head_right, head_left = head_r, head_l
patch_path = [(Path.MOVETO, tail_right[0]),
(Path.CURVE3, tail_right[1]),
(Path.CURVE3, tail_right[2]),
(Path.LINETO, head_right[0]),
(Path.CURVE3, head_right[1]),
(Path.CURVE3, head_right[2]),
(Path.CURVE3, head_left[1]),
(Path.CURVE3, head_left[0]),
(Path.LINETO, tail_left[2]),
(Path.CURVE3, tail_left[1]),
(Path.CURVE3, tail_left[0]),
(Path.LINETO, tail_right[0]),
(Path.CLOSEPOLY, tail_right[0]),
]
path = Path([p for c, p in patch_path], [c for c, p in patch_path])

return path, True
Expand Down Expand Up @@ -3498,10 +3529,9 @@ def transmute(self, path, mutation_size, linewidth):
tolerence=0.01)
except NonIntersectingPathException:
# if this happens, make a straight line of the head_length long.
dx, dy = x2 - x1, y2 - y1
ff = head_length/(dx*dx+dy*dy)**.5
x0, y0 = x2 - ff*dx, y2 - ff*dy
arrow_path = [(x0, y0), (x1, y1), (x2, y2)]
x0, y0 = _point_along_a_line(x2, y2, x1, y1, head_length)
x1n, y1n = 0.5*(x0+x2), 0.5*(y0+y2)
arrow_path = [(x0, y0), (x1n, y1n), (x2, y2)]
path_head = arrow_path
else:
path_head = path_in
Expand Down