2012-05-10 5 views
1

matplotlib를 사용하여 우주선 궤도의 2D 뷰를 플로팅하고 있습니다. 이 궤도에서 특정 이벤트를 식별하고 표시 한 다음이 이벤트와 해당 날짜를 범례에 나열합니다. 그림을 파일로 저장하기 전에 필자의 궤도 음모에 자동 확대 기능이 적용되어 범례가 내 플롯 위에 직접 인쇄됩니다. 자동 스케일링을 수행 한 후 내 전설의 폭을 확인한 다음 내 xaxis를 확장하여 플롯의 오른쪽에있는 범례를위한 "공간을 확보하십시오"라고 말합니다. 개념적으로, 이것과 같은 것;matplotlib 자동 스케일 전설의 공간을 만드는 축

# ... code that generates my plot up here, then: 
ax.autoscale_view() 
leg = ax.get_legend() 
leg_width = # Somehow get the width of legend in units that I can use to modify my axes 
xlims = ax.get_xlim() 
ax.set_xlim([xlims[0], xlims[1] + leg_width]) 
fig.savefig('myplot.ps',format='ps') 

나는 데 가장 큰 문제는 ax.set_xlim()는 "데이터가"특정 값은 윈도우 픽셀 leg.get_window_extent 보고서 (나는 생각한다) 반면, 캔버스가 그려진 후에 만도 있다고한다, 그래서 난 것입니다 위와 비슷하게 사용할 수있는 방법으로 전설 "너비"를 얻을 수있는 방법이 확실하지 않습니다.

답변

0

그림을 한 번 저장하여 실제 범례 위치를 얻은 다음 transData.inverted()를 사용하여 화면 좌표를 데이터 좌표로 변환 할 수 있습니다.

import pylab as pl 
ax = pl.subplot(111) 
pl.plot(pl.randn(1000), pl.randn(1000), label="ok") 
leg = pl.legend() 

pl.savefig("test.png") # save once to get the legend location 

x,y,w,h = leg.get_window_extent().bounds 

# transform from screen coordinate to screen coordinate 
tmp1, tmp2 = ax.transData.inverted().transform([0, w]) 
print abs(tmp1-tmp2) # this is the with of legend in data coordinate 

pl.savefig("test.png") 
관련 문제