2013-02-15 88 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