FancyArrowPatch到已知大小的标记边缘?

时间:2013-01-30 00:38:50

标签: python matplotlib

我早些时候在matplotlib-user邮件列表上问过,所以对于交叉帖子道歉。

假设我有一个已知大小的标记,我想在这一点上画一个箭头。如何获得箭头的终点?正如您在下面看到的,它与标记重叠。我想走到边缘。我可以使用shrinkA和shrinkB来做我想做的事情,但我不知道它们与点数大小有什么关系**。或者我应该以某种方式使用两点之间的已知角度和点本身进行变换。我不知道如何转换数据坐标中的一个点,并按大小** .5点在某个方向上偏移它。任何人都可以帮忙解决这个问题吗?

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)

修改:我取得了一些进展。如果我正确地阅读Translations Tutorial,那么这应该给我一个关于标记半径的点。但是,只要您调整Axes的大小,转换就会关闭。我很难理解还有什么用。

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

1 个答案:

答案 0 :(得分:0)

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)

你还需要在点的边缘上包含关于轴的纵横比的一些内容(因为除非纵横比= 1,否则数据单位中的标记的形状为椭圆形)