Skip to main content
 首页 » 编程设计

python之如何在 matplotlib 中使箭头长度更长

2024年10月24日14pengyingh

如下图,箭头的头长太短了,不好看,所以我想把头长拉长一点,如图所示。

当然,您可以通过以下链接将箭头变长来实现这一点: Python annotation arrows with same headlength

但是,由于图中空间有限,我不想更改此箭头的长度。

请您参考如下方法:

可以通过 frac 来做到这一点 arrowprops key , 这对应于头部占据的箭头长度的分数。下面给出了 MWE。

import matplotlib.pyplot as plt 
import numpy as np    
 
frac_val = 0.1 
for x in np.arange(0.1, 1, 0.2): 
 
    arrow_props = dict(fc='k', ec='k', frac=frac_val, headwidth=10, width=2, 
                       shrink=0.1) 
 
    text = '%d%%' % (frac_val * 100) 
 
    plt.annotate(text, xy=(x, 0.9), xytext=(x, 0.2), ha='center', 
                 arrowprops=arrow_props) 
 
    frac_val += 0.1 
 
plt.show() 

结果是: