问题描述
我正在尝试使用 matplotlib 的注释绘制一条两端带有箭头的线.但是,当我绘制它们时,箭头的尖端不会如图所示在指定的坐标处开始或结束.提示应该以0.6和0.8开头和结尾,但不是.
可重现的代码
将matplotlib.pyplot导入为pltfig = 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 ='轴分数',\xytext =(0.8,0.33),textcoords ='轴分数',fontsize = 7,\color ='#303030',arrowprops = dict(edgecolor ='black',arrowstyle ='<->'))plt.annotate('', xy = (0.33, 0.6), xycoords = '轴分数', \xytext =(0.33,0.8),textcoords ='轴分数',fontsize = 7,\color = '#303030', arrowprops=dict(edgecolor='black', arrowstyle = '<->'))fig.savefig('arrow_head.pdf')
为什么会发生这种情况?以及如何获取在各个坐标处开始或结束的提示?
根据文档
I am trying to plot a lines with arrow heads on both ends using matplotlib's annotation. But when I plot them, the arrow head tips do not start or end at the specified coordinates as shown in figure. The tips should start and end at 0.6 and 0.8 but they do not.
Reproducible code
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')
Why does this happen? And how to get the tips to start or end at the respective coordinates?
According to the documentation here, the path is shrunk by the parameters given in shrinkA
and shrinkB
, presumably to provide a little spacing when the arrow is pointing at something. The default value is 2, so if you set them to 0, the spacing should go away. Like so:
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))
这篇关于如何让箭头在 Python 中的指定坐标处开始/结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!