2011-08-12 7 views
33

pyplot.text()에 pyplot.legend()로 할 수있는 위치를 알려주는 방법이 있습니까? 전설 인수와 같은matplotlib에 자동으로 텍스트 상자 배치

뭔가 좋은 것 : 나는 문자 (예를 들어, "A", "B")를 사용하여 다른 축이 줄거리에 레이블을 시도하고

plt.legend(loc="upper left") 

. 수동으로 위치를 추정하는 것보다 나은 방법이 있어야한다고 생각합니다.

감사

답변

20

원래이 질문을 게시했지만 loc 매개 변수를 사용하여 실제로 사용할 수 있는지 여부는 확실하지 않습니다. 감사합니다 -

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.offsetbox import AnchoredText 

# make some data 
x = np.arange(10) 
y = x 

# set up figure and axes 
f, ax = plt.subplots(1,1) 

# loc works the same as it does with figures (though best doesn't work) 
# pad=5 will increase the size of padding between the border and text 
# borderpad=5 will increase the distance between the border and the axes 
# frameon=False will remove the box around the text 

anchored_text = AnchoredText("Test", loc=2) 
ax.plot(x,y) 
ax.add_artist(anchored_text) 

plt.show() 

enter image description here

+1

'AnchoredText'는'loc = "best"'를 처리하지 않는 것 같습니까? – zyxue

+0

@zyxue : https://github.com/matplotlib/matplotlib/issues/1313을 참조하십시오. – naught101

31

그냥 annotate를 사용하여 축 좌표를 지정합니다.

plt.annotate('Something', xy=(0.05, 0.95), xycoords='axes fraction') 

또한 애호가 얻을 수있는 포인트에 일정한 오프셋 지정 : : 예를 들어, "왼쪽 상단"될 예 here 및 더 자세한 예를 참조하십시오 더 설명은

plt.annotate('Something', xy=(0, 1), xytext=(12, -12), va='top' 
      xycoords='axes fraction', textcoords='offset points') 

here .

+0

@Garrett : 다음은 예입니다! 그들은 지금 고쳐야한다. –

관련 문제