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

Skip to content

Commit 2bb68f6

Browse files
authored
Merge pull request #11780 from ImportanceOfBeingErnest/connectorpatch-coordinates
ENH: Allow arbitrary coordinates for ConnectionPatch
2 parents dd0e5f7 + c02ad36 commit 2bb68f6

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
:orphan:
2+
3+
`ConnectionPatch` accepts arbitrary transforms
4+
----------------------------------------------
5+
6+
Alternatively to strings like ``"data"`` or ``"axes fraction"``
7+
`ConnectionPatch` now accepts any `~matplotlib.transforms.Transform`
8+
as input for the ``coordsA`` and ``coordsB`` argument. This allows to
9+
draw lines between points defined in different user defined coordinate
10+
systems. Also see the :doc:`Connect Simple01 example
11+
</gallery/userdemo/connect_simple01>`.

examples/userdemo/connect_simple01.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
Connect Simple01
44
================
55
6+
A `ConnectionPatch` can be used to draw a line (possibly with arrow head)
7+
between points defined in different coordinate systems and/or axes.
68
"""
79
from matplotlib.patches import ConnectionPatch
810
import matplotlib.pyplot as plt
911

1012
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3))
1113

14+
# Draw a simple arrow between two points in axes coordinates
15+
# within a single axes.
1216
xyA = (0.2, 0.2)
1317
xyB = (0.8, 0.8)
1418
coordsA = "data"
@@ -19,6 +23,8 @@
1923
ax1.plot([xyA[0], xyB[0]], [xyA[1], xyB[1]], "o")
2024
ax1.add_artist(con)
2125

26+
# Draw an arrow between the same point in data coordinates,
27+
# but in different axes.
2228
xy = (0.3, 0.2)
2329
coordsA = "data"
2430
coordsB = "data"
@@ -27,6 +33,16 @@
2733
arrowstyle="->", shrinkB=5)
2834
ax2.add_artist(con)
2935

36+
# Draw a line between the different points, defined in differnt coordinate
37+
# systems.
38+
xyA = (0.6, 1.0) # in axes coordinates
39+
xyB = (0.0, 0.2) # x in axes coordinates, y in data coordinates
40+
coordsA = "axes fraction"
41+
coordsB = ax2.get_yaxis_transform()
42+
con = ConnectionPatch(xyA=xyA, xyB=xyB, coordsA=coordsA, coordsB=coordsB,
43+
arrowstyle="-")
44+
ax2.add_artist(con)
45+
3046
ax1.set_xlim(0, 1)
3147
ax1.set_ylim(0, 1)
3248
ax2.set_xlim(0, .5)

lib/matplotlib/patches.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4386,6 +4386,8 @@ def __init__(self, xyA, xyB, coordsA, coordsB=None,
43864386
system.
43874387
================= ===================================================
43884388
4389+
Alternatively they can be set to any valid
4390+
`~matplotlib.transforms.Transform`.
43894391
"""
43904392
if coordsB is None:
43914393
coordsB = coordsA
@@ -4518,6 +4520,11 @@ def _get_xy(self, x, y, s, axes=None):
45184520
#(0,0) is lower left, (1,1) is upper right of axes
45194521
trans = axes.transAxes
45204522
return trans.transform_point((x, y))
4523+
elif isinstance(s, transforms.Transform):
4524+
return s.transform_point((x, y))
4525+
else:
4526+
raise ValueError("{} is not a valid coordinate "
4527+
"transformation.".format(s))
45214528

45224529
def set_annotation_clip(self, b):
45234530
"""

lib/matplotlib/tests/test_patches.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,14 @@ def test_connection_patch():
394394
arrowstyle="->")
395395
ax2.add_artist(con)
396396

397+
xyA = (0.6, 1.0) # in axes coordinates
398+
xyB = (0.0, 0.2) # x in axes coordinates, y in data coordinates
399+
coordsA = "axes fraction"
400+
coordsB = ax2.get_yaxis_transform()
401+
con = mpatches.ConnectionPatch(xyA=xyA, xyB=xyB, coordsA=coordsA,
402+
coordsB=coordsB, arrowstyle="-")
403+
ax2.add_artist(con)
404+
397405

398406
def test_datetime_rectangle():
399407
# Check that creating a rectangle with timedeltas doesn't fail

0 commit comments

Comments
 (0)