2014-01-28 2 views
1

text() 또는 annotate()에 대한 호출을 pyplot에서 "데이터"인 것처럼 처리하여 쉽게 축 경계를 확장하여 플롯 경계 내 텍스트를 포함하도록하는 쉬운 방법이 있습니까? , 마진에 없다)?pyplot 축 제한 자동 조정

# this creates a barplot with a significance bracket 
# but the p-value above the bracket overlaps the plot title 
import matplotlib.pyplot as plt 
p = plt.subplot(1, 1, 1) 
b = plt.bar((0, 1), (2, 3), yerr=(0.1, 0.2)) 
bracket = [[[0.4, 0.4], [2.5, 3.8]], 
      [[0.4, 1.4], [3.8, 3.8]], 
      [[1.4, 1.4], [3.4, 3.8]]] 
for x, y in bracket: 
    p.plot(x, y, 'k') 
txt = p.text(0.9, 4.0, 'p < 0.001', ha='center') 
plt.title('A significant MWE') 

matplotlib barplot with significance bracket

나의 현재 해결 방법은 텍스트의 bbox_patch 범위를 받고, draw()를 호출 데이터 좌표로 그 범위를 변환, 수동 축의 ybound을 변경, 텍스트를 생성하는 것은보다 큰 무언가에 반대 포함 변환 된 상위 범위. 이것은 나에게 다소 우회적 인 것처럼 보입니다. 더 쉬운 방법이 있는지 궁금합니다.

답변

0

이것은 내 일시적인 해결책입니다. 우아하지만 기능적입니다. 누구든지 더 좋은 방법이 있다면, 나는 아직도 그것을 듣고 싶습니다.

txt.set_bbox(dict(facecolor='w', alpha=0, boxstyle='round, pad=1')) 
plt.draw() 
txtbb = txt.get_bbox_patch().get_window_extent() 
ymax = p.transData.inverted().transform(txtbb).ravel()[-1] 
ybnd = p.get_ybound() 
if ymax > ybnd[-1]: 
    p.set_ybound(ybnd[0], ymax) 
plt.draw() 
plt.show() 

matplotlib barplot with adjusted axis limits