3

我之前在 matplotlib-user 邮件列表中询问过,因此为交叉帖子道歉。

假设我有一个以点为单位的已知大小的标记,我想在这一点上画一个箭头。如何获得箭头的终点?正如您在下面看到的,它与标记重叠。我想去边缘。我可以使用shrinkA 和shrinkB 来做我想做的事,但我看不出它们与点大小**.5 有什么关系。或者我应该以某种方式使用两点与点本身之间的已知角度进行转换。我不知道如何在数据坐标中平移一个点,并将其在某个方向上偏移 size**.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)

编辑:我取得了更多进展。如果我正确阅读了翻译教程,那么这应该会给我一个关于标记半径的点。但是,一旦您调整轴的大小,转换就会关闭。我不知道还能用什么。

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')

例子

4

1 回答 1

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,否则椭圆中数据单元中的标记形状)

于 2013-01-31T04:38:57.517 回答