2017-04-03 95 views
2

計劃的註釋框沒有出現在我的情節,但是,我已經嘗試了其座標值範圍廣泛。註釋框不出現在matplotlib

這是什麼問題?

import numpy as np 
from scipy.integrate import odeint 
import matplotlib.pyplot as plt 

def f(s,t): 
    a = 0.7 
    b = 0.8 
    Iext= 0.5 
    tau = 12.5 
    v = s[0] 
    w = s[1] 
    dndt = v - np.power(v,3)/3 - w + Iext 
    dwdt = (v + a - b * w)/tau 
    return [dndt, dwdt] 

t = np.linspace(0,200) 
s0=[1,1] 

s = odeint(f,s0,t) 

plt.plot(t,s[:,0],'b-', linewidth=1.0) 
plt.xlabel(r"$t(sec.)$") 
plt.ylabel(r"$V (volt)$") 
plt.legend([r"$V$"]) 

annotation_string = r"$I_{ext}=0.5$" 
plt.text(15, 60, annotation_string, bbox=dict(facecolor='red', alpha=0.5)) 

plt.show() 

回答

4

默認情況下,座標爲plt.text是數據座標。這意味着爲了存在於繪圖中,它們不應超過繪圖的數據限制(這裏,x方向~0..200,y方向〜-2..2)。

plt.text(10,1.8)應該工作。

問題是,一旦數據限制發生變化(因爲您繪製了不同的東西或添加了另一個繪圖),文本項目將位於畫布內的不同位置。

如果這是不需要的,可以在座標軸上指定文本(在兩個方向上從0到1)。爲了將文本始終放置在軸的左上角,與您在那裏繪製的內容無關,可以使用例如

plt.text(0.03,0.97, annotation_string, bbox=dict(facecolor='red', alpha=0.5), 
     transform=plt.gca().transAxes, va = "top", ha="left") 

這裏transform關鍵字告訴編譯器使用軸座標的文本,va = "top", ha="left"手段,該文本的左上角應錨點。

+0

Typo:ha ='left'。 – swatchai

+0

歡呼的隊友!更正它。 – ImportanceOfBeingErnest

+0

有一個更簡單的方法可以做到這一點,而不必(直接)使用'transform'http://matplotlib.org/users/annotations.html#basic-annotation use'textcoords'和'xycoords'。 – tacaswell

2

的註解,因爲你給了一個「Y」的60座標,而你的情節,在「2」(向上)結束的曲線中,出現遠。

這裏更改第二個參數:

plt.text(15, 60, annotation_string, bbox=dict(facecolor='red', alpha=0.5)) 

它需要< = 2,顯示了對情節本身。你可能也想改變x座標(從15到更少),這樣它就不會遮蓋你的線條。

例如

plt.text(5, 1.5, annotation_string, bbox=dict(facecolor='red', alpha=0.5)) 

不要被我的(5,1.5)的建議感到震驚,我會再添加下面一行到你的腳本的頂部(下您的進口):

rcParams['legend.loc'] = 'best' 

這將選擇爲你的傳奇「最適合」;在這種情況下,左上角(就在註釋上方)。兩者看起來相當整齊,然而,你的選擇雖然:)