Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在matplotlib';注释?_Python_Matplotlib - Fatal编程技术网

Python 如何在matplotlib';注释?

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

我想用相同的注释注释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=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”
,只是打字很长。