2015-08-26 19 views
5

我想繪製兩端帶箭頭的線條使用matplotlib的註釋。但是,當我繪製它們時,箭頭標記不會在指定的座標處開始或結束,如圖所示。提示應該開始和結束在0.6和0.8,但他們不。如何獲得箭頭頭提示開始/結束在Python中的指定座標?

enter image description here

重現代碼

import matplotlib.pyplot as plt 
fig = plt.figure(figsize = (5, 5)) 
plt = plt.subplot(111) 
plt.axvline(0.6) 
plt.axvline(0.8) 
plt.axhline(0.6) 
plt.axhline(0.8) 

plt.annotate('', xy = (0.6, 0.33), xycoords = 'axes fraction', \ 
    xytext = (0.8, 0.33), textcoords = 'axes fraction', fontsize = 7, \ 
    color = '#303030', arrowprops=dict(edgecolor='black', arrowstyle = '<->')) 

plt.annotate('', xy = (0.33, 0.6), xycoords = 'axes fraction', \ 
    xytext = (0.33, 0.8), textcoords = 'axes fraction', fontsize = 7, \ 
    color = '#303030', arrowprops=dict(edgecolor='black', arrowstyle = '<->')) 

fig.savefig('arrow_head.pdf') 

爲什麼會出現這種情況?以及如何讓提示開始或結束在相應的座標?

回答

9

根據文檔here,路徑是通過在shrinkAshrinkB給出,大概是爲了提供一個小間距當箭頭在一些指向參數收縮。默認值是2,所以如果將它們設置爲0,則間距應該消失。像這樣:

plt.annotate('', xy = (0.6, 0.33), xycoords = 'axes fraction', \ 
    xytext = (0.8, 0.33), textcoords = 'axes fraction', fontsize = 7, \ 
    color = '#303030', arrowprops=dict(edgecolor='black', arrowstyle = '<->', shrinkA = 0, shrinkB = 0)) 

Graph with lines and arrows

相關問題