FancyArrowPatch to edge of marker with known size?

I asked earlier on the matplotlib-user mailing list, so I apologize for the crossroads.

Let's say I have a marker with a known size in points and I want to draw an arrow up to that point. How can I get the arrow end points? As you can see below, it overlaps the markers. I want to go to the edge. I can use shrinkA and shrinkB to do what I want, but I can't see how they relate to the size of the dots **. Or should I transform somehow using the known angle between the two points and the point itself. I don't know how to translate a point in data coordinates and offset it in a specific direction by size **. 5 points. Can anyone help clarify this?

import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrowPatch

point1 = (138.21, 19.5)
x1, y1 = point1
point2 = (67.0, 30.19)
x2, y2 = point2
size = 700

fig, ax = plt.subplots()
ax.scatter(*zip(point1, point2), marker='o', s=size)

# if I need to get and use the angles
dx = x2 - x1
dy = y2 - y1
d = np.sqrt(dx**2 + dy**2)

arrows = FancyArrowPatch(posA=(x1, y1), posB=(x2, y2),
                            color = 'k',
                            arrowstyle="-|>",
                            mutation_scale=700**.5,
                            connectionstyle="arc3")

ax.add_patch(arrows)

      

Edit . I've made a little more progress. If I read the Translations Tutorial correctly , then this should give me a point within the radius of the markers. However, once you resize the axes, the transformation will be disabled. I don't understand what else to use.

from matplotlib.transforms import ScaledTranslation
# shift size points over and size points down so you should be on radius
# a point is 1/72 inches
dpi = ax.figure.get_dpi()
node_size = size**.5 / 2. # this is the radius of the marker
offset = ScaledTranslation(node_size/dpi, -node_size/dpi, fig.dpi_scale_trans)
shadow_transform = ax.transData + offset
ax.plot([x2], [y2], 'o', transform=shadow_transform, color='r')

      

Example

+3


source to share


1 answer


import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrowPatch
from matplotlib.transforms import ScaledTranslation

point1 = (138.21, 19.5)
x1, y1 = point1
point2 = (67.0, 30.19)
x2, y2 = point2
size = 700

fig, ax = plt.subplots()
ax.scatter(*zip(point1, point2), marker='o', s=size)

# if I need to get and use the angles
dx = x2 - x1
dy = y2 - y1
d = np.sqrt(dx**2 + dy**2)

arrows = FancyArrowPatch(posA=(x1, y1), posB=(x2, y2),
                            color = 'k',
                            arrowstyle="-|>",
                            mutation_scale=700**.5,
                            connectionstyle="arc3")

ax.add_patch(arrows)


# shift size points over and size points down so you should be on radius
# a point is 1/72 inches
def trans_callback(event):
    dpi = fig.get_dpi()
    node_size = size**.5 / 2. # this is the radius of the marker
    offset = ScaledTranslation(node_size/dpi, -node_size/dpi, fig.dpi_scale_trans)
    shadow_transform = ax.transData + offset
    arrows.set_transform(shadow_transform)


cid = fig.canvas.mpl_connect('resize_event', trans_callback)

      



You also need to include something about the aspect ratio of the axes, get the points at the edge of the point (since the marker shape is in data units in an ellipse if aspect ratio = 1)

0


source







All Articles