Python 如何在matplotlib';注释?
我想用相同的注释注释2个点,我想有2个箭头从1个注释指向这些点 我有以下代码:Python 如何在matplotlib';注释?,python,matplotlib,Python,Matplotlib,我想用相同的注释注释2个点,我想有2个箭头从1个注释指向这些点 我有以下代码: import matplotlib.pyplot as plt plt.plot( [1,2], [1,2], 'o' ) plt.xlim( [0,3] ) plt.ylim( [0,3] ) plt.annotate( 'Yet another annotation', xy=(1,1), xytext=(1.5, .5), arrowprops=dic
import matplotlib.pyplot as plt
plt.plot( [1,2], [1,2], 'o' )
plt.xlim( [0,3] )
plt.ylim( [0,3] )
plt.annotate( 'Yet another annotation', xy=(1,1),
xytext=(1.5, .5),
arrowprops=dict( arrowstyle="->" )
)
plt.annotate( 'Yet another annotation', xy=(2,2),
xytext=(1.5, .5),
arrowprops=dict( arrowstyle="->" )
)
plt.show()
正如您在结果图中所看到的,它是有效的(尽管这可能不是最优雅的方式,因为我在同一位置有两个注释)
然而,我对此不满意:我想让箭头从完全相同的位置开始,目前它们似乎从注释的中心开始,我更愿意让它们从注释边缘的某个地方开始,以便它们的起点连接起来
如何实现这一点?您可以注释箭头的起点。即,让两个箭头从同一位置开始,不带注释文本,并使用第三个不带箭头的注释用文本注释该起点
import matplotlib.pyplot as plt
plt.plot( [1,2], [1,2], 'o' )
plt.xlim( [0,3] )
plt.ylim( [0,3] )
plt.annotate( "", xy=(1,1), xytext=(1.3, 2.5),
arrowprops=dict( arrowstyle="->" ) )
plt.annotate( '', xy=(2,2), xytext=(1.3, 2.5),
arrowprops=dict( arrowstyle="->" ) )
plt.annotate( 'Some annotation', xy=(1.3, 2.5),
xytext=(1.3, 2.5) , va = "bottom", ha="center" )
plt.annotate( "", xy=(1,1), xytext=(1.5, .5),
arrowprops=dict( arrowstyle="->",shrinkA=0 ) )
plt.annotate( '', xy=(2,2), xytext=(1.5, .5),
arrowprops=dict( arrowstyle="->",shrinkA=0 ) )
plt.annotate( 'Yet another annotation', xy=(1.5, .5),
xytext=(1.5, .5) , va = "top", ha="left" )
plt.show()
您可以使用
arrowprops
键shrinkA
来调整箭头的收缩率,将其设置为零可以连接两个箭头。谢谢!va
是垂直对齐的缩写,而ha
是水平对齐的缩写?这是正确的。实际上,您也可以使用horizontalalignment=“left”
,只是打字很长。