Python 如何在matplotlib中增加特定类型箭头的头部大小?
我正在尝试编辑matplotlib中的一些箭头属性。下面是一个我试图使用数据集的示例。我知道在处理“正常”箭头(test1中的那个)时如何使用arrowprops编辑一些属性,但是我需要指向直线上两点之间的间隔。为此,我需要在test2中增加箭头“头”的大小。基本上,我需要这个箭头的头部覆盖/指向点(5.0,3.5)和(4.0,3.0)之间的间隔 下面是我的代码及其输出Python 如何在matplotlib中增加特定类型箭头的头部大小?,python,matplotlib,jupyter-notebook,Python,Matplotlib,Jupyter Notebook,我正在尝试编辑matplotlib中的一些箭头属性。下面是一个我试图使用数据集的示例。我知道在处理“正常”箭头(test1中的那个)时如何使用arrowprops编辑一些属性,但是我需要指向直线上两点之间的间隔。为此,我需要在test2中增加箭头“头”的大小。基本上,我需要这个箭头的头部覆盖/指向点(5.0,3.5)和(4.0,3.0)之间的间隔 下面是我的代码及其输出 import matplotlib.pyplot as plt fig,ax = plt.subplots(figsize
import matplotlib.pyplot as plt
fig,ax = plt.subplots(figsize = (5,5))
plt.plot([1, 1.5, 2, 2.5, 3, 3.5, 4], marker='o')
ax.annotate('test1', (2, 2.1), xytext=(-15,15), color='r', size=13,
textcoords='offset points', arrowprops=dict(width=0.3,
headwidth=5,
headlength=4,
shrink=.18),
bbox=dict(pad=0.01, facecolor='none', edgecolor='none'))
ax.annotate('test2', (5, 3.3), xytext=(20,-40), color='r', size=13,
textcoords='offset points', arrowprops=dict(arrowstyle='-['),
bbox=dict(pad=0.01, facecolor='none', edgecolor='none'));
你说得对,如果arrowsprops
dict包含arrowstyle
,则某些键将被禁用。因此,若要增加箭头的头部大小,必须使用mutation\u scale
键
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize = (5, 5))
plt.plot([1, 1.5, 2, 2.5, 3, 3.5, 4], marker='o')
ax.annotate('test1', (2, 2.1), xytext=(-15, 15), color='r', size=13,
textcoords='offset points',
arrowprops=dict(width=0.3, headwidth=5, headlength=4,
shrink=.18),
bbox=dict(pad=0.01, facecolor='none', edgecolor='none'))
ax.annotate('test2', (4.8, 3.1), xytext=(10,-60), color='r', size=13,
ha='center', va='center', textcoords='offset points',
arrowprops=dict(arrowstyle='-[', mutation_scale=28,
connectionstyle='arc3,rad=0.38'),
bbox=dict(pad=0.01, facecolor='none', edgecolor='none'));
我还添加了connectionstyle
、horizontalalignment
和verticalalignment
参数,以便更容易地将箭头的头部与直线对齐。但请记住,它们是可选的
最终结果: