2013-02-15 84 views
2

我想注释一个matplotlib图中的某些长度。例如,点A和B之间的距离。在matplotlib中注释尺寸

为此,我想我可以使用annotate并找出如何提供箭头的开始和结束位置。或者,使用arrow并标出该点。

我试图用后者,但我无法弄清楚如何获得2箭头:

from pylab import * 

for i in [0, 1]: 
    for j in [0, 1]: 
     plot(i, j, 'rx') 

axis([-1, 2, -1, 2]) 
arrow(0.1, 0, 0, 1, length_includes_head=True, head_width=.03) # Draws a 1-headed arrow 
show() 

如何创建一个2箭头?更好的是,还有另一种(简单的)在matplotlib数字中标注尺寸的方法吗?

+1

[技术图纸绘制距离箭头(可能重复http://stackoverflow.com/questions/14612637/plotting-distance-箭头在技术绘图) – 2013-02-17 22:21:40

回答

6

您可以通过使用arrowstyle属性更改箭头的样式,例如

ax.annotate(..., arrowprops=dict(arrowstyle='<->')) 

给出了一个双箭头。

一个完整的例子可以找到here约三分之一的可能不同风格的页面。

至于在地块上标记尺寸的'更好'的方法,我想不出任何我的头顶上。

编辑:这里有一个完整的例子,如果它是有帮助的,你可以使用

import matplotlib.pyplot as plt 
import numpy as np 

def annotate_dim(ax,xyfrom,xyto,text=None): 

    if text is None: 
     text = str(np.sqrt((xyfrom[0]-xyto[0])**2 + (xyfrom[1]-xyto[1])**2)) 

    ax.annotate("",xyfrom,xyto,arrowprops=dict(arrowstyle='<->')) 
    ax.text((xyto[0]+xyfrom[0])/2,(xyto[1]+xyfrom[1])/2,text,fontsize=16) 

x = np.linspace(0,2*np.pi,100) 
plt.plot(x,np.sin(x)) 
annotate_dim(plt.gca(),[0,0],[np.pi,0],'$\pi$') 

plt.show() 
+0

但是对于注释,我如何控制箭头开始和结束的确切位置? – Dhara 2013-02-15 10:49:06

+0

使用属性'xy'和'xytext'(都是长度为2的元组)。 'annotate'假设你想添加一些文本,如果你不简单地传递一个空字符串作为第一个参数。 – Dan 2013-02-15 10:53:59

+0

很好的例子,谢谢! – Dhara 2013-02-15 13:06:58