2016-05-14 103 views
1

(雖然也有類似我的問題的幾個問題,幾乎所有的人都到文本框,圖例和註解有關。)設定一個固定的位置matplotlib文本

使用一個循環,我想顯示具體32個屬性的信息:左邊的直方圖和右邊的統計。

只有一個屬性,它是非常簡單的處理,我設置的X,Y的文本位置,這就是它:

#Histogram 
sns.distplot(n1, kde=True)#fit=stats.beta) 
plt.title('Histogram') 
plt.xlabel('Duration') 
plt.xticks(np.arange(0, max(n1)+1, 0.5)) 

#Statistics (There are 13 in total) 
plt.text(1.85,5,'Standard Time: %6.4f'%(ST.iloc[j,0])) 
plt.text(1.85,5,'Median: %6.4f'%(medmed)) 
plt.text(1.85,4.65,'Variance from ST: %6.2f '%(Totvarval)) 
plt.text(1.85,4.3,'Standard Deviation: %6.4f'%(np.std(n1))) 

問題是,當我創建的所有屬性循環,賦予不同的範圍每一個,文本位置的改變相對造成了這樣的結果:

Example for 2 attributes

我知道應該有固定的協調方式,但在文檔中我找不到它。

+0

http://matplotlib.org/users/text_props.html可以幫助你 – daryl

+0

http://matplotlib.org/users/annotations_intro.html – tacaswell

回答

1

傳遞給pyplot.text座標是數據座標(如the documentation指出)。這意味着文本的絕對(像素)座標取決於繪圖軸的X和Y範圍。如果要指定相對於軸本身,而不是數據上的位置,你必須轉換你的座標,例如像這樣:

x0, xmax = plt.xlim() 
y0, ymax = plt.ylim() 
data_width = xmax - x0 
data_height = ymax - y0 
plt.text(x0 + data_width * 1.5, y0 + data_height * 0.5, 'Some text') 
+0

使用'annotate'和其中一個圖形或軸單位是好得多。這可以讓mpl爲你做數學運算,並且如果你平移/縮放,它將保持在同一個地方。 – tacaswell

+0

聽起來很不錯。你應該寫一個答案@ tcaswell! – tjollans