2013-04-20 12 views
12

テキストを等アスペクトフィギュアの右下隅に配置したいと思います。 ax.transAxes、 で図形の相対位置を設定しましたが、各図形の高さの尺度に応じて相対座標値を手動で定義する必要があります。Python/Matplotlib - 等アスペクトフィギュアのコーナーにテキストを入れる方法

スクリプト内の軸の高さのスケールと正しいテキストの位置を知る良い方法はありますか?

ax = plt.subplot(2,1,1) 
ax.plot([1,2,3],[1,2,3]) 
ax.set_aspect('equal') 
ax.text(1,-0.15, 'text', transform=ax.transAxes, ha='right', fontsize=16) 
print ax.get_position().height 

ax = plt.subplot(2,1,2) 
ax.plot([10,20,30],[1,2,3]) 
ax.set_aspect('equal') 
ax.text(1,-0.15, 'text', transform=ax.transAxes, ha='right', fontsize=16) 
print ax.get_position().height            

enter image description here

答えて

37

使用annotate

実際、私は殆どtextを使用していません。物事をデータ座標に配置したい場合でも、私は通常一定の距離だけオフセットしたいのですが、それはannotateではるかに簡単です。簡単な例として、

import matplotlib.pyplot as plt 

fig, axes = plt.subplots(nrows=2, subplot_kw=dict(aspect=1)) 

axes[0].plot(range(1, 4)) 
axes[1].plot(range(10, 40, 10), range(1, 4)) 

for ax in axes: 
    ax.annotate('Test', xy=(1, 0), xycoords='axes fraction', fontsize=16, 
       horizontalalignment='right', verticalalignment='bottom') 
plt.show() 

enter image description here

あなたはそれが少し隅からのオフセットたい場合、あなたはどのような値を制御するためにxytext kwarg(およびtextcoordsを通じてオフセットを指定することができますが解釈されます)。私もここhorizontalalignmentverticalalignmentためhava略語を使用しています:

import matplotlib.pyplot as plt 

fig, axes = plt.subplots(nrows=2, subplot_kw=dict(aspect=1)) 

axes[0].plot(range(1, 4)) 
axes[1].plot(range(10, 40, 10), range(1, 4)) 

for ax in axes: 
    ax.annotate('Test', xy=(1, 0), xycoords='axes fraction', fontsize=16, 
       xytext=(-5, 5), textcoords='offset points', 
       ha='right', va='bottom') 
plt.show() 

enter image description here

をあなたは軸の下に配置しようとしている場合、あなたはそれをセットを配置するためにオフセットを使用することができます点で以下の距離:

import matplotlib.pyplot as plt 

fig, axes = plt.subplots(nrows=2, subplot_kw=dict(aspect=1)) 

axes[0].plot(range(1, 4)) 
axes[1].plot(range(10, 40, 10), range(1, 4)) 

for ax in axes: 
    ax.annotate('Test', xy=(1, 0), xycoords='axes fraction', fontsize=16, 
       xytext=(0, -15), textcoords='offset points', 
       ha='right', va='top') 
plt.show() 

enter image description here

さらに詳しい情報は、Matplotlib annotation guideをご覧ください。

+0

これは素晴らしい回答と例です。テキストの代わりに注釈を使用しようとします。どうもありがとうございました。 – Tetsuro

+0

すばらしい答え!どうも! – HyperCube

関連する問題