我之前在 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)
编辑:我取得了更多进展。如果我阅读 Translations Tutorial正确,那么这应该给我标记半径上的一个点。但是,一旦调整轴的大小,转换就会关闭。我不知道还能用什么。
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')
最佳答案
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)
关于python - FancyArrowPatch 到已知大小的标记边缘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14594786/