You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matplotlib中如何让ax.annotate双向箭头的箭头尖精准对齐指定端点?

解决方法

当arrowprops中指定arrowstyle参数时,生成的FancyArrowPatch对象使用shrinkA、shrinkB两个独立参数控制两端的收缩量,默认值不为0会导致箭头尖和指定坐标之间存在空隙,将两个参数均设为0即可实现精准对齐,还可通过linewidth调整线条粗细实现细线效果,完整代码如下:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot()
ax.plot([0],[0])
ax.grid()
ax.set_xlim([0,10])
ax.set_ylim([0,10])
ax.annotate("", (2, 1), (4, 1), arrowprops={
    'arrowstyle':'<->', 
    'shrinkA': 0, 
    'shrinkB': 0,
    'linewidth': 1 # 可自定义调整线条粗细
})
plt.show()

参数说明:

  • shrinkA:控制箭头起始端(annotate方法传入的第三个参数对应坐标)的收缩量,单位为磅,设为0即完全无收缩
  • shrinkB:控制箭头结束端(annotate方法传入的第二个参数对应坐标)的收缩量,单位为磅,设为0即完全无收缩

内容的提问来源于stack exchange,提问作者sdbbs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 17:06:03