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
相关产品推荐
相关产品推荐

