以兩個點為例,其中起點為點(1,2),終點為點(3,4)
1 import matplotlib.pyplot as plt 2 def drawArrow(A,B): 3 fig = plt.figure() 4 ax = fig.add_subplot(111) 5 """ 6 箭頭起始位置(A[0],A[1])和終點位置(B[0],B[1]) 7 length_includes_head = True:表示增加的長度包含箭頭部分 8 head_width:箭頭的寬度 9 head_length:箭頭的長度 10 fc:filling color(箭頭填充的顏色) 11 ec:edge color(邊框顏色) 12 """ 13 ax.arrow(A[0],A[1],B[0]-A[0],B[1]-A[1],length_includes_head = True,head_width = 0.25,head_length = 0.5,fc = 'r',ec = 'b') 14 ax.set_xlim(0,10) #設置圖形的范圍,默認為[0,1] 15 ax.set_ylim(0,10) #設置圖形的范圍,默認為[0,1] 16 ax.grid() #添加網格 17 ax.set_aspect('equal') #x軸和y軸等比例 18 plt.show() 19 plt.tight_layout() 20 21 A = [1,2,3,4,5,6,7] 22 B = [3,4,5,6,7,8,9] 23 drawArrow(A,B)
輸出情況: