python arrow在图上绘制箭头

发布时间 2023-07-14 09:56:17作者: Littlefish-
import matplotlib.pyplot as plt
def drawArrow(A,B):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    """
    箭头起始位置(A[0],A[1])和终点位置(B[0],B[1])
    length_includes_head = True:表示增加的长度包含箭头部分
    head_width:箭头的宽度
    head_length:箭头的长度
    fc:filling color(箭头填充的颜色)
    ec:edge color(边框颜色)
    """
    ax.arrow(A[0],A[1],B[0]-A[0],B[1]-A[1],length_includes_head = True,head_width = 0.5,width=0.25,head_length = 0.5,fc = 'r',ec = 'r')
    ax.plot(3, 3,color='k',alpha=1,marker='o',markersize='1') #,markersize='1'
    ax.set_xlim(0,10) #设置图形的范围,默认为[0,1]
    ax.set_ylim(0,10) #设置图形的范围,默认为[0,1]
    ax.grid()  #添加网格
    ax.set_aspect('equal')  #x轴和y轴等比例
    plt.show()
    plt.tight_layout()

# A = [1,2,3,4,5,6,7]
# B = [3,4,5,6,7,8,9]
A=[3,1]
B=[3,4]
drawArrow(A,B)
另外一种 利用 annotate 也可以 打标签和画箭头
ax.annotate('', xy=(lon_max[i], lat_max[i]+0.08), xytext=(lon_max[i]-0.001, lat_max[i]), arrowprops=dict(arrowstyle="->", color="r", hatch='*'))