2013-04-20 16 views
12

Metni eşit en boy oranının sağ alt köşesine yerleştirmek istiyorum. Figür ile ilgili pozisyonu ax.transAxes, ile ayarlıyorum fakat her bir rakamın yükseklik skalasına bağlı olarak rölatif koordinat değerini manuel olarak tanımlamalıyım.Python/Matplotlib - Metnin eşit kenarlık köşesine nasıl yerleştirileceği

Eksenlerdeki yükseklik ölçeğini ve yazı içindeki doğru metin konumunu bilmek için iyi bir yol ne olurdu?

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

cevap

37

kullanın annotate. Aslında, text'u neredeyse hiç kullanmıyorum. Veri koordinatlarına bir şeyler koymak istediğimde bile, genellikle annotate ile çok daha kolay olan noktalarda bazı sabit mesafelerle dengelemek istiyorum. Eğer biraz köşesinden offset isterseniz

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

, bir nasıl kontrol değerlerine textcoordsxytext kwarg yoluyla ofset (ve belirtebilirsiniz: Hızlı bir örnek olarak

xytext yorumlanır). Ofset Eğer eksen altına yerleştirin çalışıyorsanız

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

kullanabileceğiniz it a set yerleştirmek için: Ben de burada horizontalalignment ve verticalalignment için ha ve va kısaltmalar kullanıyorum noktaları aşağıda mesafe:

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

Daha fazla bilgi için Matplotlib annotation guide da bir göz atın.

+0

Bu harika bir cevap ve örnekler. Metin yerine not eklemeyi kullanmaya çalışacağım. Çok teşekkür ederim. – Tetsuro

+0

Harika cevap! Teşekkürler! – HyperCube